Thread: question about "new" with pointers

  1. #1
    Registered User Boomba's Avatar
    Join Date
    Jun 2003
    Posts
    89

    question about "new" with pointers

    hi

    i'm trying to understand how to use new and while i was experimenting I came accross something i dont understand...

    here is a quick example:
    Code:
    int main()
    {
    	int* var1;
    	int* var2;
    	
    	var1 = new int[1];
    	var2 = new int[1];
    	
    	var1[1] = 2;
    	var2[100] = 3;
    	var1[100] = 3;
    
    	printf("var1[1] = %d, var2[100] = %d\n", var1[1], var2[100]);
    	printf("var1[100] = %d\n", var1[100]);
    	return 0;
    }
    why does the compiler let me give var1 and var2 values at array index 100 if I only made a new integer the size of 2 array indexes ([1])? I thought the "new" was like calling malloc just a little nicer

    here is the output f the program aswell
    var1[1] = 2, var2[100] = 3
    var1[100] = 3
    Press any key to continue
    whats even more weird is that its actually being set to the right values 1 even tho i didnt allow that much space memory to be used when i used new!!!...now I really don get it..

    thanx for your help in advance,
    Boomba
    Last edited by Boomba; 08-06-2004 at 04:14 PM.

  2. #2
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    You create two arrays of size 1, not 2. That means that all of your code exhibits undefined behavior because it accesses data past the bounds of the array.

    var1 = new int[1]; // Array of size 1
    var1[0] = 2; // OK.
    var1[1] = 2; // Undefined behavior.
    var1[100] = 2; // Undefined behavior.

    The reason it appears to work is because undefined behavior means the compiler can make the code do anything - maybe crash, maybe work, maybe print garbage. In reality, new only allocates the number of objects that you specify when calling it. In this case, that is 1.

    P.S. Don't forget:
    delete [] var1;
    delete [] var2;

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > now I really don get it
    Sooner or later, you'll find yourself in this mess
    http://cboard.cprogramming.com/showthread.php?t=55450

    Small simple programs seldom fall over with undefined behaviour. It's only much later when your programs reach some kind of critical mass that all hell breaks loose and you have problem after problem trying to figure out what is actually going on.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A question about pointers in functions
    By occams razor in forum C Programming
    Replies: 3
    Last Post: 05-15-2007, 12:16 AM
  2. Newbie question about pointers in function parameters.
    By sojurn in forum C++ Programming
    Replies: 14
    Last Post: 01-20-2007, 09:21 PM
  3. simple pointers question
    By euphie in forum C Programming
    Replies: 4
    Last Post: 05-25-2006, 01:51 AM
  4. Very stupid question involving pointers
    By 7smurfs in forum C Programming
    Replies: 6
    Last Post: 03-21-2005, 06:15 PM
  5. Quick Question Regarding Pointers
    By charash in forum C++ Programming
    Replies: 4
    Last Post: 05-04-2002, 11:04 AM