Monday, May 24, 2010

I think I'm misunderstanding this line of code in C++?

What does this line do?





int* addr = new int[num];





It is making an array with a pointer? Or something? I've been studying for a final and doing my last programs in a class all day and the code is starting to blend together...

I think I'm misunderstanding this line of code in C++?
addr is a pointer to an array of num ints





This lets your program dynamically allocate the memory for the array, instead of having a fixed amount of memory by defining int a[1000], for example. You can't say int a[num]; it's not valid.





You use this when you'll be determining the size of the array programmically. It's then also common to see delete a[ ] later on to free the memory.





Check out this site:


http://www.fredosaurus.com/notes-cpp/new...
Reply:It creates a new array of num integers, and sets addr to the address if the first element of the array.


No comments:

Post a Comment