Matrix Dynamic Allocation

Trying to not enter in details, I need to allocate and free matrices of data in my analysis. This is the test program i’m using:

Explaining: allocate_matrix() allocate an matrix and return the double pointer for it; erase_matrix() receives the double pointer for matrix and free it; main() is the test program who allocates and then erases an matrix.

The program gone fine in GCC, but in ROOT some strange things happen. Sometimes I call main() from ROOT prompt and it goes fine, other times just end with strange errors:

I’m using recommended Windows binary version 5.16.00.

Any alternative for allocating data matrices that I can free later? Or I’m making something wrong?

Thank you for the help.

Hi,

You test fails for me when compiled with gcc and valgrind points out:Processing cal.C+... ==31018== Invalid write of size 8 ==31018== at 0x722E83B: allocate_matrix(unsigned, unsigned) (cal.C:10) ==31018== by 0x722E982: cal() (cal.C:25) ==31018== Address 0x68AB5F8 is 0 bytes after a block of size 1,600 alloc'd ==31018== at 0x4A04CBF: calloc (vg_replace_malloc.c:279) ==31018== by 0x722E801: allocate_matrix(unsigned, unsigned) (cal.C:7) ==31018== by 0x722E982: cal() (cal.C:25)
So your code works by accident when compiled on your platform.
For you internal loop the followingfor(i=0; i<columns; i++){ should probably readfor(i=0; i<rows; i++){

When I applied this patch, your code work both in compiled and interpreted mode.

Cheers,
Philippe

You’re right, I passed a lot of times by the same error and didn’t noticed it. Thank you very much for the help.