Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Why does this dyn. array alloc. work?? it shouldn't!?!

Status
Not open for further replies.

Ixcaliber

Programmer
Apr 6, 2003
6
CA
I'm using viz studio 6 (shouldn't matter, but fyi), and am kind of curious why this works...



#include<iostream>
#using namespace std;

int main(int argc, char* argv[])
{
int *p;
p = new int[10];

for (int i=0;i<10;i++)
p = i;

// curiosity lies here, i increases out of the domain,
// but no error is given, and values are returned!!!

for(i=0; i<15; i++)
cout << p << &quot; &quot;;

cout<<endl;
return 0;
}

/* output:

0 1 2 3 4 5 6 7 8 9 -4.22017e+037 -1.9984e+018 -1.9984e+018 -1.9984e+018 1.35926e-043

*/


Is it actually creating a set of 10 pointers when I use the new, or is it just an infinite array, that only has values if I use it...

If anybody knows why this happens or has a better way to do dynamic array allocation, let me know!

-thx
 
this isn't quite the code you mean,
but I understand your meaning.

You have a standard int pointer p which
is made to point to the first int of an array of 10 ints.

You then tried to access beyond the 10 ints - for example
p[12] and your program does not crash.

Actually it will crash eventually when you try to access some location to which you do not have access.
instead of going up to 15 in the second for loop try
going up to say 500 - It will definitely crash.

I believe C++ (like c) does not do array boundary checks for you - that is up to you the programmer!
Otherwise You will only know there is an error when you try
to access a location to which you do not have rights.
 
Thanks, I guess I will just have to pay more attention so I avoid the problem by elimination...

cheers
 
To answer your questions:

>> increases out of the domain, but no error is given, and values are returned!!!

The reason is: the square-brackets simply mean, look in the memory following by the specified number of units.

For example, p[4] means, four memory locations beyond p[0], even if array was only declared to have 2 elements. More precisely, it means *(p+4).

BUT, OUT OF DOMAIN ARRAYS ARE DANGEROUS!!! You are just plain lucky that your application didn't have a memory fault. When you go out of range, you are attempting to access memory that wasn't allocated to you.

The values you were getting... -4.22017e+037 -1.9984e+018 -1.9984e+018 -1.9984e+018 1.35926e-043 ... those were just the random garbage that was in the memory locations following the array memory.

>> Is it actually creating a set of 10 pointers when I use the new, or is it just an infinite array, that only has values if I use it...

It is actually allocating memory to store 10 ints, and no more. It is NOT an infinite array: when you specify p[13], it is attempting to access memory beyond the 10 ints, which is incredibly dangerous.

Sorry if this was hard to understand-- pointers, arrays, and memory are difficult topics to explain!
 
Thanks, that clears alot up... I was thinking of the array as a list of pointers randomly thrown wherever there was space on the heap, but now that I see that the pointers are all in one memory block with consecutive addresses, it all makes sense... I will make sure that I keep an eye on the addresses from now on.

cheers
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top