freeing dynamic arrays from memory

So I'm trying to wrap my head around dynamic arrays, and I've finally figured out how to build one, using
1
2
int arraysize = randomnumber;
int* arrayname = new int[arraysize];

Now what I'm trying to figure out is how to delete the array. Can I free the whole array from any scope in my program using a simple line like
delete arrayname;?

edit : Also, since I'm working in heap, I can't rely on the debugger to show me when I'm doing something wrong, so I figured I'll ask this as well. If I create a struct such as :
struct twoints {int one, int two};
and then try to create a dynamic array of said struct using
1
2
int arraysize = 5;
twoints* arrayname = new twoints[arraysize];

Am I basically creating an array of 5 twoints?
1
2
//is it the same as doing this?
twoints arrayname[5];
Last edited on
closed account (o3hC5Di1)
Hi there,

Almost, because it's an array, you need to do the following:

delete [] arrayname;

Usual recommended practice is to also do:

1
2
delete [] arrayname;
arrayname = nullptr;


This "protects" you should you try to access the array again after it's deleted.
It prevents you from accessing whatever random values may have been written in the arrays former memory-space.
Instead, your program may segfault because you're dereferencing a nullptr.

Hope that helps.

All the best,
NwN
Prefer std::vector<> over arrays (if the size of the array is not a constant known at compile time).
http://www.mochima.com/tutorials/vectors.html
Well NwN pretty much covered it for you, just remember to always assign NULL to the pointer on the heap after using the delete operator to avoid having a dangling pointer.
Awesome, thanks for the info! does anyone know if my assumption in the edited portion is correct as well? (My concern is that I'm creating something like a multidimensional array, due to the type on the right side of the assignment operator)
closed account (o3hC5Di1)
Hi there,

As for your question in the edit: Yes and no. The result is the same, you will have an array with room for 5 of those structs. The dynamic one however will be on the heap, the other one on the stack.

Both have their advantages and disadvantages. I'm no expert, but generally items on the stack are faster to retrieve, but the stack is limited in size by the operating system, so it is not suitable for large (dynamic) containers.

The best advice is probably JLBorges - use std::vector, it takes away a lot of potential problems and debugging-nightmares.

Hope that answers your question.

All the best,
NwN
Oh, I understand the differences between the storage of data between heap and stack concerning the difference of the two statements. I just wanted to ensure that the array size was being determined by the integer variable array size, as opposed to the type twoint which is displayed before it.

Basically I got confused because I looked at it as though the left half of the statement was designating a pointer and the right half of it was designating the array size, and since the type (twoint) was two integers in size, it would be creating a multidimensional array, as opposed to the type just declaring the type of array, and the integer in brackets declaring the size.

I get kind of paranoid when dealing with things the debugger won't fix for me.
closed account (o3hC5Di1)
Hi,

No worries - pointers can be confusing. I just wrote this post to help someone else: http://www.cplusplus.com/forum/general/111857/#msg610699 Maybe it's interesting for you too - maybe not. Thought I'd mention it.

All the best,
NwN
Last edited on
So I followed your advice, but I ran into something rather interesting while doing some code.

I built a multi-dimensional dynamic array ([2][2]) of two integers.
1
2
3
4
5
6
7
arraysize_a = 2;
arraysize_b = 2;
twoint** arrayname = new twoint*[arraysize_a];
for(int i = 0; i < arraysize_a; i++)
{
    arrayname[i] = new twoint[arraysize_b]
}


then filled the variables with a bunch of random numbers to see if the array would work.

The array seemed to work just fine, up to the point of
1
2
3
4
5
for(int i = 0; i < arraysize_a; i++)
{
    delete [] arrayname[i];
}
delete [] arrayname;

at which point the debugger popped out a break point twice in free.c at line 51 :
1
2
3
4
if (retval == 0)
        {
            errno = _get_errno_from_oserr(GetLastError());
        }


I'm assuming this is something to be worried about? if so how would I fix it?

edit : sorry, original was skewed, and the issue didn't pertain to the mutlidimensional array in the first place, it belonged to something else i was trying at the same time, and I know why the break point/error occured.
Last edited on
Topic archived. No new replies allowed.