RE: realloc and Segmentation Fault

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Ok I realize what the problem is. I should pass the address of the address of the pointer 'valid' to the function.

struct bucket *valid;
*valid = actual data containing strings
valid = address of data
&valid = address of address of data

Should use pass by reference using &valid in order to change the address without using return.

But the compiler still complains of conflicting types.

Here is a simpler code that exhibits the problem:

int bucketsize = 1;
void func1(struct bucket *(*pvalid))
{
	/*this code causes errors*/
	*pvalid = insert_to_bucket(*pvalid);
}

/* in the function above **pvalid is the data, *pvalid is the address of 'valid' pointer. The function call insert_to_bucket has parameter "address of 'valid' pointer". The function insert_to_bucket then will allocate new memory to 'valid' pointer, return an address to pointer. In func1 *pvalid is an address to 'valid pointer. */

struct bucket *insert_to_bucket(struct bucket *valid)
{
valid = (struct bucket *) realloc (valid, 70 * ((bucketsize) + 1));
	bucketsize++;
	return valid;
}
int main()
{
  struct bucket *valid;
  valid = (struct bucket *) malloc (70);
  if (valid == NULL)
  {
	printf("error in allocating memory \n");
	exit(1);
  }

  /*the code directly below causes errors*/
  func1(&valid);

  return 0;
}

Thanks to all

[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux