Thread: Dynamic memory and undeclared variables

  1. #1
    Registered User divineleft's Avatar
    Join Date
    Jul 2006
    Posts
    158

    Dynamic memory and undeclared variables

    Why does this code work?
    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	int * p;
    	p = new int;
    
    	p[0] = 3;
    	p[1] = 7;
    	p[2] = 4;
    	cout << p[0] << endl;
             cout << p[1] << endl;
             cout << p[2] << endl;
    
    	return 0;
    }
    It has access to p[1 and 2] even though I never declared

    Code:
    p = new int [3];
    If it's valid code, whats the point of declaring how many new you want?

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    In general, it doesn't work. You just lucked out. If you compile or run it again under different conditions, it will eventually crash.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    Just to hammer the point home, try this:

    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
            int * p;
            p = new int;
    
            for (int i=0; i<1000000; i++) {
              p[i] = i;
              cout << p[i] << endl;
            }
    
            return 0;
    }
    and see how far it runs before you get a segmentation fault. In my case it got up to 33789. In general it depends on the exact conditions when you run it, but there's no guarantee it will get any farther than exactly what is allocated, which is just the first element.

  4. #4
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    > Why does this code work?

    Because it is undefined behavior accessing memory outside the boundaries of the variable. It's not a guaranteed crash.
    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
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    In most cases it will overwrite memory used by the next new. And crash will occure during delete of that memory for example...
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #6
    Registered User divineleft's Avatar
    Join Date
    Jul 2006
    Posts
    158
    Thanks for the responses, I understand it now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. An error is driving me nuts!
    By ulillillia in forum C Programming
    Replies: 5
    Last Post: 04-04-2009, 09:15 PM
  2. Replies: 7
    Last Post: 10-15-2008, 03:38 AM
  3. Dynamic Memory Allocation for a Class Attribute
    By clkaiser in forum C++ Programming
    Replies: 2
    Last Post: 09-11-2006, 09:41 AM
  4. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  5. Another Dynamic Memory Question
    By SirCrono6 in forum C++ Programming
    Replies: 6
    Last Post: 03-02-2005, 12:10 PM