• Advertisement
Sign in to follow this  

Pointers, delete[ ]

This topic is 4371 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi all, I am new to this forum, so I am not sure if I am posting my question to the right place, but anyway ... Here is my code that gives me the error : "Debug Assertion Failed! Expression BLOCK_TYPE_IS_VALID(pHead->nBlockUse)" #include "stdafx.h" int _tmain(int argc, _TCHAR* argv[]) { int *intPtr = new int[10]; for (int x=0;x<10;x++) { *intPtr = x; intPtr++; } delete [] intPtr; // This line causes the error. return 0; } Is it happening because I increment the Pointer variable ? If this is the cause, how can I free the memory used by a pointer in such a case like this? Thank you for reading my complete newbie and boring question :) And one more question, when I declare an array of 10 integers with the new keyword, is it placed is stack or the heap ? And what determines the size of available stack and heap memory for an application ? I guess memory leaks cause consuming of all available space in heap memory only ? And one more question which memory is faster, and why ? Thanks again.. _EpcH_

Share this post


Link to post
Share on other sites
Advertisement
In a situation like this, you use array notation. This way the pointer is not altered.

ie:

intPtr[x] = x; // set the xth element to a value of x

Memory allocated through new is on the heap, and the maximum amount that you can allocate is limited by the amount of RAM available.

The stack size is allocated at compile time, and should be configurable through your IDE/compiler's options.

Both are equally as fast in terms of access, but it does take time to allocate memory on the heap, so if you do it frequently, that will end up becoming a speed concern. To get around this, allocate what you need once and only once.

Of course, if you place all of your arrays on the stack, they will be allocated for the entire runtime of the application, even if they're not being used. If the arrays are large, it can cut into the amount your application will be able to allocate on the heap -- the OS usually gives your app a finite amount of space in total for both.

Share this post


Link to post
Share on other sites
Quote:
Original post by _EpcH_
int *intPtr = new int[10];

for (int x=0;x<10;x++)
{
*intPtr = x;
intPtr++;
}
delete [] intPtr; // This line causes the error.

Is it happening because I increment the Pointer variable?

Yes. Copy the pointer to another variable, don't alter the original one.

int *intPtr = new int[10];
int *z = intPtr;

// work with z, change it as you like

delete[] intPtr;

Share this post


Link to post
Share on other sites
Ohh, Thank you guys..

I understand the method that taby explained. But I still do not understand the part that Fred304 told.

When I use a second pointer to my first pointer ( pointer to a pointer I guess ), wont I again face the problem when I try to delete this new pointer.

For example:

int _tmain(int argc, _TCHAR* argv[])
{

int *intPtr = new int[10];
int *z = intPtr;

for (x=0;x<10;x++)
{
//*intPtr = x;
//intPtr++;
intPtr[x]=x;
z++; //Just incrementing for testing
}

std::cout << *intPtr << std::endl ;
std::cout << intPtr[9] << std::endl ;

delete[] intPtr;
delete z; // Here the same problem. Cant free that altered pointer space?



return 0;
}

Share this post


Link to post
Share on other sites
z points to intPtr. Since you never allocated a brand "new" int for z to point at using new, it is incorrect to try use delete on it.

Basically, you are sharing the common memory between 2 pointers. The only thing you can pass back to delete [] is a pointer you allocated using new[]. Since you allocated once, you deallocate once.

Correct code would read:

int _tmain(int argc, _TCHAR* argv[])
{

int *intPtr = new int[10];
int *z = intPtr;

for (x=0;x<10;x++)
{
// this should be the same as intPtr[x] = x;
*z = x;
z++;
}

std::cout << *intPtr << std::endl ;
std::cout << intPtr[9] << std::endl ;

delete[] intPtr;

return 0;
}




Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement