Another dynamic memory allocation bug.

Posted by m4design on Stack Overflow See other posts from Stack Overflow or by m4design
Published on 2010-06-02T15:56:38Z Indexed on 2010/06/02 16:04 UTC
Read the original article Hit count: 222

I'm trying to allocate memory for a multidimensional array (8 rows, 3 columns).

Here's the code for the allocation (I'm sure the error is clear for you)

char **ptr = (char **) malloc( sizeof(char) * 8);

for (i = 0; i < 3; i++)
    ptr[i] = (char *) malloc( sizeof(char) * 3);

The crash happens when I reference this:

ptr[3][0];

Unhandled exception at 0x0135144d in xxxx.exe: 0xC0000005: Access violation writing location 0xabababab.

Are there any recommended references/readings for this kind of subject?

Thanks.

© Stack Overflow or respective owner

Related posts about c

    Related posts about beginner