Re: realloc and Segmentation Fault

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

 



Thanks your explanation of realloc helped.

I made a simple test program which worked, then I made a program which passed 'valid' pointer around some functions and it did not compile with error: conflicting types.

This is the simplest code which exhibits the error:

struct bucket
{
	char *hostname;
	char *description;
};
int bucketsize = 1;
void func1(struct bucket *valid)
{
	func2(valid);
}
void func2(struct bucket *valid)
{
	/*this code causes errors*/
	valid = insert_to_bucket(valid);
}
struct bucket *insert_to_bucket(struct bucket *valid)
{
	valid[bucketsize - 1].hostname = malloc (sizeof(char) * 20);
	strncpy(valid[bucketsize - 1].hostname, "test hostname", 20);
	valid[bucketsize - 1].hostname[19] = '\0';
	printf("hostname is %s \n", valid[bucketsize - 1].hostname);

	valid[bucketsize - 1].description = malloc (sizeof(char) * 50);
	strncpy(valid[bucketsize - 1].description, "test buf", 50);
	valid[bucketsize - 1].description[49] = '\0';
	printf("description is %s \n", valid[bucketsize - 1].description);

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);
  }
  struct bucket *insert_to_bucket();

  /*the line directly below works*/
  //	valid = insert_to_bucket(valid);

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

  return 0;
}

On Wed, 19 Jul 2006, Vladimir Vassilev wrote:

Hello Jonathan,

There is bug in your program. The 'valid' pointer in 'main' has the same value before and after the call to function1. The realloc call in function1 deallocates this memory allocates new one and returns the new address. However in main you continue to use the now invalid address and use it in the next call.

Probably you assume realloc just extends the already allocated memory and the ptr remains the same which is not the case.

Regards,
Vladimir

Jonathan Shan wrote:
Hello all,

The goal of my program is to create a dynamic array of a struct type. The struct type has two strings inside it. From time to time I want to copy strings into the strings in the dynamic array. Then increase the size of the dynamic array. I have narrowed the problem down to the realloc function but I still don't know why it says segmentation fault or hangs (sometimes).

Here is the simplified code which exhibits the problem:

/*libraries here */

#define SIZEOF_BUCKETSTRUCT 70
#define SIZEOF_HOSTNAME 20
#define SIZEOF_DESCRIPTION 50

int bucketsize = 1;
struct bucket
{
    char *hostname;
    char *description;
};
void function1(struct bucket *valid)
{
    valid[bucketsize - 1].hostname = malloc (sizeof(char) * 20);
    strncpy(valid[bucketsize - 1].hostname, "test hostname", 20);
    valid[bucketsize - 1].hostname[19] = '\0';
    printf("hostname is %s \n", valid[bucketsize - 1].hostname);

    valid[bucketsize - 1].description = malloc (sizeof(char) * 50);
    strncpy(valid[bucketsize - 1].description, "test buf", 50);
    valid[bucketsize - 1].description[49] = '\0';
    printf("description is %s \n", valid[bucketsize - 1].description);
/* problem is here */
    valid = (struct bucket *) realloc (valid, 70 * ((bucketsize) + 1));
    bucketsize++;
}
int main()
{
  struct bucket *valid;
  valid = (struct bucket *) malloc (SIZEOF_BUCKETSTRUCT);
  if (valid == NULL)
  {
    printf("error in allocating memory \n");
    exit(1);
  }

  function1(valid);
  function1(valid);

  return 0;
}

Thank you,

Jonathan Shan


--
=========================================================================================
Vladimir Vassilev                      |
Logicom Consult EOOD                   | E-mail:
Mladost 1A, bl. 501, apt. 68           | vladimir@xxxxxxxxxxxxxx
Sofia, Bulgaria                        | Web:
Tel:+359 886979158                     | http://www.logicom-bg.com




[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