Dynamic arrays

M

meyousikmann

The following code just sets up and fills a dynamic array of integers.

#include <cstdlib>

int main()
{
int* intArray = NULL;
int count;

count = 20;
intArray = new int[count];

for(int i=1; i <= count; i++)
intArray = i;

delete [] intArray;
}

Now, notice the last line "delete [] intArray". If I leave that line in, I
get a program error window when I run it that says:

****************************************
Debug Error!

Program: [program location and name]

DAMAGE: after Normal block (#41) at 0x00431C00

(Press Retry to debug the application)
****************************************

I don't understand why simply de-allocating the memory created by the
earlier new statement should cause a problem. Am I not supposed to delete
what I new'd? If I comment out the "delete...." line, the program runs to
completion perfectly. Can anyone shed some light on this one? I am using
MSVC++ 6.0. Any help would be greatly appreciated.
 
R

Rob Williscroft

meyousikmann wrote in
The following code just sets up and fills a dynamic array of integers.

#include <cstdlib>

int main()
{
int* intArray = NULL;
int count;

count = 20;
intArray = new int[count];

for(int i=1; i <= count; i++)

Array (pointer) aritmatic is 0 based in C++, so the above should be:

for ( int i = 0; i < counr; ++i )
intArray = i;

delete [] intArray;
}

Now, notice the last line "delete [] intArray". If I leave that line
in, I get a program error window when I run it that says:

****************************************
Debug Error!

Program: [program location and name]

DAMAGE: after Normal block (#41) at 0x00431C00

(Press Retry to debug the application)
****************************************

I don't understand why simply de-allocating the memory created by the
earlier new statement should cause a problem.


The problem is that you wrote byond the end of the array, you're
fortunate that you compiler inserted code to check that you hadn't
done this and reported the error.

[snip]

Rob.
 
C

Catalin Pitis

Use vector class for dynamic arrays! You can prevent memory leaks, for
example, if an exception is thrown, because destroying the vector object
implies deallocating the vector's memory for elements. You can work with
vector objects the same as with plain, C style arrays, but much safer.

The code will look like this (draft code, not tested, not compiled)

#include <vector>

int main()
{
int count = 20;
std::vector< int> intArray( count);

// NOTE: here is the problem you have:
// the arrays/vectors use zero-based indexing
// So, use i from 0 to count - 1
for(int i=0; i < count; i++)
intArray = i;

// no need to delete it explicitly:
// vector::~vector is called, which will deallocate the memory
// used for the elements
}

Catalin
 
R

Rolf Magnus

meyousikmann said:
The following code just sets up and fills a dynamic array of integers.

#include <cstdlib>

int main()
{
int* intArray = NULL;
int count;

count = 20;
intArray = new int[count];

for(int i=1; i <= count; i++)
intArray = i;

delete [] intArray;
}

Now, notice the last line "delete [] intArray". If I leave that line
in, I get a program error window when I run it that says:

****************************************
Debug Error!

Program: [program location and name]

DAMAGE: after Normal block (#41) at 0x00431C00

(Press Retry to debug the application)
****************************************

I don't understand why simply de-allocating the memory created by the
earlier new statement should cause a problem. Am I not supposed to
delete what I new'd?


You are supposed to do that. But as others have noted, your indexes are
incorrect, which results in an attempt to write one past the end of the
array. On some (or even most) operating systems (Windows is apparently
one of them, too), new writes meta information about the allocated
memory (e.g. the block size) before or after the allocated block. You
probably overwrote some of that information, so memory management got
corrupted. The typical result is that the next allocation or
deallocation crashes. In bigger programs, such bugs are nasty and hard
to find, since the crash happens not at the place where the actual
error is.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,146
Messages
2,570,832
Members
47,374
Latest member
EmeliaBryc

Latest Threads

Top