Dynamic memory and undeclared variables

This is a discussion on Dynamic memory and undeclared variables within the C++ Programming forums, part of the General Programming Boards category; Why does this code work? Code: #include <iostream> using namespace std; int main() { int * p; p = new ...

  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
    Portugal
    Posts
    7,412
    > Why does this code work?

    Because it is undefined behavior accessing memory outside the boundaries of the variable. It's not a guaranteed crash.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,471
    In most cases it will overwrite memory used by the next new. And crash will occure during delete of that memory for example...
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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, 01:53 AM
  5. Another Dynamic Memory Question
    By SirCrono6 in forum C++ Programming
    Replies: 6
    Last Post: 03-02-2005, 11:10 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21