call to realloc() inside a function: memory problem

This is a discussion on call to realloc() inside a function: memory problem within the C Programming forums, part of the General Programming Boards category; Hi there, I am writing a code composed of a main function calling another function (called CSR in the wrapped ...

call to realloc() inside a function: memory problem

Hi there, I am writing a code composed of a main function calling another function (called CSR in the wrapped code) whose arguments are pointers to 1D arrays.
The pointers are to be modified in length inside the function by means of realloc(), and returned to the main program with the new size and new elements.
The problem is that, although the function fills the arrays correctly ( I verify this by printing the array inside the function), such arrays are not passed back to the main correctly, in fact if I print the array from inside main(), the elements are not correct. It seems, from the output, that the memory allocated for the arrays is freed before the arrays are returned to main.

I don't have "nrutil.h" so I can't try this, but you are passing values (not pointers) to a function that doesn't return anything. If you want to change the values in place, you should pass your function pointers.

What do your variables look like before you call CSR? Are they correct? Are they the same inside main as they were before? If so, this is definately the problem.

ps. regarding anon's problem about realloc: if realloc returns a NULL pointer everything is pretty much all over anyway, so you can just go:

Hi Anon, thanks for replying so quickly. I am not sure on what you are suggesting: if I want to fill in a previously empty array Jc of defined of type int *Jc in main(), and passed to CRS as CRS(Jc), why does it not work that way? I mean, why Jc is filled in correctly inside CRS, but when I print it back from main(), I get a different array?

From the code that I wrapped, what part is the one that is causing memory leak?
Thanks a lot again
Simone

I mean, why Jc is filled in correctly inside CRS, but when I print it back from main(), I get a different array?

The same reason why when you pass say, an int to a function and assign it a value, the int that you had in the caller of that function does not change. You passed the pointer by value, so the change to the pointer in the called function merely changes the local copy of the pointer. You need to pass a pointer to a pointer.

I don't have "nrutil.h" so I can't try this, but you are passing values (not pointers) to a function that doesn't return anything. If you want to change the values in place, you should pass your function pointers.[/B]

about nrutil.h you shouldn't worry; I forgot to erase the line from the wrapped code, but there is no call to the nrutil.h functions in this code. I am only using stdlib.h calls

Originally Posted by MK27

What do your variables look like before you call CSR? Are they correct? Are they the same inside main as they were before? If so, this is definately the problem.
[/B]

The pointers Jc and Ac in main are defined as arrays of int and double respectively, and they are of size 1 when firstly assigned in main. The need for CSR is due to the fact that I need to count the nonzero elements of the matrix A before knowing how many elements Ac and Jc will have. From here the need for realloc.
So, before I call CSR, Ac and Jc have only 1 element each, and the value is zero.
The I call CSR inside which, by means of realloc, I increase their size with the increasing value of a counter defined in CSR itself, and start filling them up with the needed elements.
Within CSR Ac and Jc, if printed to screen, are being correctly filled, but when I print them from main once returned there, their elements are just gone.

Originally Posted by MK27

ps. regarding anon's problem about realloc: if realloc returns a NULL pointer everything is pretty much all over anyway, so you can just go:

The pointers Jc and Ac in main are defined as arrays of int and double respectively, and they are of size 1 when firstly assigned in main. The need for CSR is due to the fact that I need to count the nonzero elements of the matrix A before knowing how many elements Ac and Jc will have. From here the need for realloc.

It might be better to write a function to perform that count. Call that function, and only then do you malloc() for the Jc and Ac arrays. At this point, your initial malloc() is wasted since you are going to realloc() with the correct sizes anyway. More generally, try to declare/create variables near first use (though this is somewhat harder in C, as per the C90 standard, than with the C99 standard or C++).

It might be better to write a function to perform that count. Call that function, and only then do you malloc() for the Jc and Ac arrays. At this point, your initial malloc() is wasted since you are going to realloc() with the correct sizes anyway. More generally, try to declare/create variables near first use (though this is somewhat harder in C, as per the C90 standard, than with the C99 standard or C++).

HI Laserlight, thanks a lot for you reply. I was thinking of doing that first, but I wanted to avoid to have too many "for" loops and especially, I'd like to have only one function that does it all and in order to keep the function more usable.

I may go for your suggestion for the time being so that I can proceed with my code, but if any suggestion on what is causing the current error comes out, I'd be happy to know about it and solve it

HI Laserlight, thanks a lot for you reply. I was thinking of doing that first, but I wanted to avoid to have too many "for" loops and especially, I'd like to have only one function that does it all and in order to keep the function more usable.

A function should do one thing and do it well. You could have a function that does a series of things as one unit, but that function would call other functions that each do a particular thing in the series.

Originally Posted by simone.marras

I may go for your suggestion for the time being so that I can proceed with my code, but if any suggestion on what is causing the current error comes out, I'd be happy to know about it and solve it

Alarmingly, I found these errors when checking the code (yes, I combined them inside one file with CSR first).

They are all warnings, actually. The first one is not necessarily an issue since // is a valid comment marker in C99, and the "'realloc' might return null pointer" warning has already been covered by anon, while I am guessing that the "Dereferencing NULL pointer" are directly related to the current problem that simone.marras brought up.

Yeah, they are reported as warnings by the compiler, but such things as dereferencing a NULL pointer is an error - perhaps not compile error, but my definition of error is not always just limited to that.

The first one is not necessarily an issue since // is a valid comment marker in C99

Indeed. VS only supports C90, and it was in a MS header, so I removed it... You probably saw it before I edited it away.

and the "'realloc' might return null pointer" warning has already been covered by anon, while I am guessing that the "Dereferencing NULL pointer" are directly related to the current problem that simone.marras brought up.

Not to rain on anyone's parade, but a little evidence or compiler warnings to back it up doesn't hurt, eh?
Besides, it shows that you should not do this. It also shows that fixing these problems does not raise any flags when using code analysis with VS, which is a good thing, as well!