I am developing a large scale code. In order to save the space, when I declare any pointer, I allocate it in another subroutine. Now it seems that it does not work when I use an array of pointers. I can summarize the code as:

#include "headers.h"

/** Object of extrapolation coefficients **/

typedef struct{
  double *zero[2];
  double *one [2];

int main(){
  /** Declare functions **/
  double       *interp(const int, const int );
  /** Decalre variables **/
  const int length = 4;
  int       i;
  double    y;

  /** Declare pointer   **/
  prt_interpln *interp_coeff;
  interp_coeff->zero[0] = interp(length, 0);
  interp_coeff->zero[1] = interp(length, 1);
  interp_coeff->one [0] = interp(length, 2);
  interp_coeff->one [1] = interp(length, 3);
  for(i = 0; i < length; i++){
    y = interp_coeff->one [1][i];

    printf("Y(%d) = %f\n", i, y);


  free( interp_coeff );
  return 0;


double       *interp( const int length, const int agent ){
  int         i;
  double      x;    
  double       *y = malloc(length * sizeof(double)) ;
  for(i = 0; i < length; i++){
    x = (double) agent * i;
    y[i] = x ;
  /** End **/
  return y;

if I do so, I get the following warning:

pointer.c: In function ‘main’:
pointer.c:39: warning: ‘interp_coeff’ is used uninitialized in this function

If I run it I get segmentation fault. If I allocate memory for interp_coeff inside main, then it works.

But I think then I am doing somthing wrong! Because the interp_coeff should be allocated once inside main and another time inside the routine

double *interp( const int length, const int agent )

It seems like a conflict!!

Please help!

Thanks in advance

6 Years
Discussion Span
Last Post by Banfa

Hi I think allocating space using malloc should be gud enough
prt_interpln *interp_coeff;
interp_coeff=(prt_interpln *)malloc(sizeof(prt_interpln));

Hope this solves the problem


Hi I think allocating space using malloc should be gud enough
prt_interpln *interp_coeff;
interp_coeff=(prt_interpln *)malloc(sizeof(prt_interpln));

Hope this solves the problem

Thanks indeed, but I think you did not get my point.

If I do so, then I allocate twice, one in the main (as you proposed) and one in function

double *interp( const int length, const int agent )

where I create a pointer and allocate it and send it back to be place in the array of pointers!

I hope the explanation is enough!


> inside the routine double *interp( const int length, const int agent )

That is correct, but you never call this routine. It is just sitting idle as good as a dummy.

interp_coeff = interp(..., ...);

will do.


I do call the function, please look at the lines 33, 34, 36, 37.

You meant that?



The problem is that when you declare the pointer to the structure (prt_interpln *interp_coeff) the memory for its fields is not allocated yet and you can't refer them. That is why you get the segmentation fault.
The solution, I think, is to change your interp function to allocate the whole structure in it and to call the function at the initialization of the pointer to the structure.
Hope the explanation is clear and that it helps!

Edited by Silen: n/a


Thanks, your explanation is very clear! However, I have to cal the functions many times, in the real code it calculates something four times for four different parameters.

What happens if I allocation the structure once inside the main, and I allocate again for the members of the sttruct while I calculate them inside the routine?

Thanks in advance!


Once you allocated the memory for the struct and its members you don't need to allocate them again. You use the same memory for different calculations.
After you allocated the memory you can use another function for the calculations as many times as you want, as long as you don't allocate memory there.

If you insist to use interp as it is now for each member, you have to allocate the struct in the main, and remove the allocation of the memory from interp.


You have a structure, prt_interpln which contains 2 arrays of pointers size 2 and you declare a pointer to that structure interp_coeff.

So you have 5 pointers, 1 to to the structure itself and 4 inside the structure to arrays of double.

All of these pointers need memory allocated and assigned to them before they are used so your code should be performing 5 allocations.

For every allocation it should be calling free, otherwise you have a memory leak. If in the course of the program you re-allocate the memory for one of the pointers you need to be sure that you have freed the memory it pointed to previously.

You must deallocate the memory that the pointers contained in prt_interpln point to before you deallocate the memory that the pointer interp_coeff points to.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.