Thread: Dynamic arrays of const objects under VC++ 2005

  1. #1
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446

    Dynamic arrays of const objects under VC++ 2005

    Hi all,

    The following is not producing an error under VC++ 2005 Express.

    Code:
    const int *p = new const int[10];  // ();
    Under Dev-C++, I get what I'm expecting. A compile-time error about the fact I'm trying to create an unitialized array of const integers.

    Do you know what compiler option(s) should I set under VC++ 2005 in order for it to correctly flag this as an error?
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    maybe Dev-C++ compiler is wrong??

    >>Do you know what compiler option(s) should I set under VC++ 2005 in order for it to correctly flag this as an error?
    Probably none -- no compiler is 100% compilant with c++ standards.

  3. #3
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    I'm sorry... but this is confusing me...
    I tried the following after I read a related article on MSDN :

    Code:
    int val = 12;
    const int *p = new const int; //on purpose. *p is undefined here on VC++ 2005
    
    int *temp = &val;
    p = temp;
    
    std::cout << *p << std::endl; //outputs 12!
    The above also works on Dev-C++ provided I initialize p. But what is confusing me is that I'm apparently allowed to change the referenced value of a pointer to const.

    True, I cannot if I simply tried *p = 12. But can someone please help me understand why can I do this?
    Last edited by Mario F.; 06-04-2006 at 07:32 AM.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  4. #4
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Forget it. I found the answer... I'm not changing the contents of the pointer when I do p = temp. I'm changing the pointer itself. It is still following the rules for pointers to constant objects.

    However, I'm still curious...

    Even if undefined under VC++ 2005, wouldn't it make sense to have it at least issue a warning? After all, a uninitialized pointer to const is rather useless and seems (I think) a rather easy thing to catch.

    Maybe Dev-C++ is wrong. I couldn't find yet information on the ISO or Stroustrup's C++. But my guts tell me MinGW is closer to the truth...
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  5. #5
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    Posts
    594
    Even if undefined under VC++ 2005, wouldn't it make sense to have it at least issue a warning?
    It might be a warning that they turn off, but allow you to turn on.

    list of the warnings that are off by default:
    http://msdn2.microsoft.com/en-US/library/23k5d385.aspx

  6. #6
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Big list... Thanks Skorman. I thought that having warnings set at level 4 would be enough. Apparently not.

    I've set "/Wall" and it was a nightmare
    Lots of warnings. All related with the standard library. But not one about this issue.

    So, apparently VC++ 2005 treats uninitialized const objects as undefined and issues no warning. Maybe this is the way it should be as Ancient Dragon suggested.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  7. #7
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    VC++ 2005 has its quirks but overall is much better than previous generations of MS compilers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  2. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  3. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  4. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM
  5. oh me oh my hash maps up the wazoo
    By DarkDays in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 12:54 PM