3

If I want to dynamically create an int array in C++ and then delete it later, do I need to null terminate the int array? If so, how do I do it? If not, how does the computer know how much memory to free?

int *array = new int[x];
..do some stuff..
delete array;

I know for a char array it deletes up to a null character ('\0') and if you don't have a terminating null character in your string you'll probably get memory violation errors but how does this apply to arrays of ints (and other arrays, like arrays of structs)?

8
  • "If I want to dynamically create an int array in C++ and then delete it later" You shouldn't want to do this. You should either use std::vector<int>, which manages the memory for you, or use a std::unique_ptr<int[]> / std::shared_ptr<int[]> / boost::shared_array<int> / boost::scoped_array<int>, which will destroy the array at the correct time. Commented Aug 17, 2011 at 23:10
  • 2
    "I know for a char array it deletes up to a null character ('\0')" That is not true at all; why do you think this? Commented Aug 17, 2011 at 23:10
  • Mmm, I think its a little strong to say that you shouldn't want to do this ever. I agree with you in general, but sometimes you want to use low-level C - for example speed or interacting with libraries that use an array as a function parameter. Yes, use class libraries where possible - convenience and safety trump over-optimizing speed in most cases, but not all. Commented Aug 17, 2011 at 23:13
  • 1
    @iandotkelly: C-arrays are not faster than vectors. And using &v[0], you can have direct access to the array contained within for functions that accept pointers. Commented Aug 17, 2011 at 23:28
  • @Benjamin Lindley - just out of curiosity (genuine question from a C, C# and Objective-C programmer) can you pass the underlying array by reference to a function which will change its contents? If so, and if that's still safe, I'm impressed. Still think its worth knowing how a C array works even in this day and age ;-) Commented Aug 17, 2011 at 23:43

5 Answers 5

6

The block of memory used by your array has some extra data in there, that isn't directly visible to your program, that tells delete how big the block is.

What you say about char arrays is not true, by the way - the presence of a NUL terminator isn't relevant to delete - it uses the hidden block size just like any other block of memory.

(Incidentally, you should say delete [] array; when you're deleting an array.)

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks! What's the difference between delete array and delete [] array?
@Sam: delete [] knows how to call the destructor for each member of the array, whereas plain delete doesn't.
Delete[] will also clean up the element count. Regular delete is just wrong.
3

If you allocate an array with new[], you just need to free it with delete[].

The compiler is required to track the size and count of the allocation if the type you are allocating has a non-trivial destructor.

As for strings, the null terminator is for determining the length of the string, not the size of the underlying memory allocation.

2 Comments

The compiler doesn't track the size of arrays. See Keith's comment under my answer
@Shredder, the compiler generates code that adds tracking information that lets it generate code later to call the right number of destructors when calling delete[] on a pointer returned by new[]. I didn't feel like being that verbose so I shortened the explanation to 'the compiler is required to track the size and count'. And if you looked into the return value of operator new[] and the pointer that is a result of new type[...], you would find that it is not always the same value. The difference is where the compiler puts the count of the array.
1

If you allocate an array of any kind using operator new[], such as int array[5] = new int[5];, then you will always be able to delete it using operator delete[] (note: not delete), such as delete[] array;.

The compiler achieves this by hiding some extra information regarding the length of the array, often just before its start. Using this information, operator delete[] can tell exactly how much memory to delete.

Note also that NUL terminators have nothing to do with this - the process works in exactly the same way for char arrays as any other type of array.

In summary - you don't need a terminator for an array (of any kind, int, char or otherwise). If you allocate it with new[], you can deallocate it with delete[].

Comments

1

The size of the array is always known. In your code, the size of the array is whatever the value of x is, therefore, the program knows how much memory to free.

2 Comments

This answer is wrong in several ways. The compiler transforms C++ code into machine code and then it's done. It doesn't free memory because it's not running when the program is. Also, unless x is a constant, then the compiler has no idea what x is since x doesn't get a value until the program is run.
I didn't mean compiler, I meant program. I am speaking of run-time. Sorry, I edited
0

I know for a char array it deletes up to a null character ('\0') and if you don't have a terminating null character in your string you'll probably get memory violation errors but how does this apply to arrays of ints (and other arrays, like arrays of structs)?

That's probably because the C++ internals happens to crash at the '\0' character.

For arrays, use:

delete [] array;

C++ handles the size of the array and how much to delete.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.