Sisyphus <[EMAIL PROTECTED]> writes:
>Hi,
>
>I'm building an n x n 2D array.
>
>I've declared the structure:
>int ** aoa;
>
>I've allocated the memory:
>Newz(100, aoa, n, int);

Observation you asked for n 'int's there but you are 
stuffing it with n 'int *'s

That should have been 

 Newz(100, aoa, n, int *);

>if(aoa == NULL) croak("Failed to allocate rows");
>for(i = 0; i < n; ++i) {
>  Newz(i, aoa[i], n, int);
>  if(aoa[i] == NULL) croak("Failed to allocate columns");
>  }
>
>I've successfully placed (and accessed) data in the structure, so I guess
>I've done it correctly so far.
>
>To free the memory I've done:
>for(i = 0; i < n; ++i) Safefree(aoa[i]);
>
>But there's still some more memory to Safefree() isn't there ?
>I tried adding:
>Safefree(aoa);
>
>But that crashes the program. 

It shouldn't 

>What do I need to do ?

You did it correctly as far as I can see,
my guess is that you have corrupted memory around aoa.

>
>Also, as regards the first argument supplied to New() - is there any reason
>that it should be different (ie unique) for each 'New()' call within a
>program ?

It is not normally used - it is legacy of really old allocation checker.
The number is a tag which describes "why" that chunk was allocated
in some vague way.

>
>I'm working on Win2k, perl5.6.1, and writing code to be used with Inline C
>.... if any of that makes any difference. (I don't think it does :-)

AFAIK int and int * are same size there, so I am at a loss as to 
what is wrong.

On linux you could use valgrind to check this stuff...


>
>Cheers,
>Rob

Reply via email to