# Segmentation fault with realloc



## SIFE (Oct 24, 2010)

I have this snipest code:

```
.
.
.
  while(i<=nchunks)
   {
    [B]if(i==nchunks)
     {
      nbytes=lbytes;
      buffer=(char *)realloc(buffer, nbytes);
     }[/B]
    else
     buffer = (char *)malloc(sizeof(char)*nbytes);
    //printf("%i-0x%X\n", i, buffer);
    out = (char*)malloc(sizeof(char)*strlen(fname)+3);
    memset(buffer, '\0', nbytes);
    memset(out, '\0', strlen(fname)+3);
.
.
.
```
When I get in the block if and I realloc memory, I end with this(output from gdb):

```
Program received signal SIGSEGV, Segmentation fault.
0x00000008006f100d in _pthread_mutex_init_calloc_cb () from /lib/libc.so.7
```
I have FreeBSD 8.1 stable with custom kernel.


----------



## expl (Oct 24, 2010)

Paste rest of the "while(i<=nchunks)" loop code. Also its strange to see you using "sizeof(char) * ..." since size of char is always equal to 1 so no need to multiply.


----------



## Alt (Oct 24, 2010)

If you writing threaded prog, you should lock mutex before memset/alloc/realloc/any chaning for your `buffer` variable.
And for more informative debug info you can do `bt` command in gdb, it gives much more info


----------



## SIFE (Oct 24, 2010)

Mr expl's, the problem appear when I realloc and give new pointer to buffer.


> Also its strange to see you using "sizeof(char) * ..." since size of char is always equal to 1 so no need to multiply.


For portability in future.
expl
I don't use any threading.
This is what I get when I execute bt in gdb:

```
#0  0x00000008006f100d in _pthread_mutex_init_calloc_cb () from /lib/libc.so.7
#1  0x00000008006f4c77 in realloc () from /lib/libc.so.7
#2  0x00000008006f4bf9 in realloc () from /lib/libc.so.7
#3  0x00000008006f4ae1 in realloc () from /lib/libc.so.7
#4  0x0000000000400b1c in main () at test.c:26
```
Line 26 is:

```
buffer=(char *)realloc(buffer, nbytes);
```
What I understand is when the program try to allocate new memory with realloc fail.


----------



## expl (Oct 24, 2010)

SIFE said:
			
		

> Mr expl's, the problem appear when I realloc and give new pointer to buffer.
> 
> For portability in future.
> expl
> ...



Portability to what? Its going to be equal to 1 on all modern compilers and platforms.

Error is most likely not in realloc call but somewhere else in the code thus why I asked for rest of the loop code.


----------



## SIFE (Oct 24, 2010)

```
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

main()
 {
  char *fname="MSDN.rar";
  FILE *src ,*dest;
  char *buffer, *out;
  int nchunks, nwrite, rest, readf, i=1;
  long int lbytes, nbytes=1506061;
  src = fopen(fname ,"rb");
  if(src == NULL)
   printf("opening error");
  nchunks = file_size(fname)/nbytes;
  if(file_size(fname)%nbytes != 0)
   {
    lbytes= file_size(fname)-(nchunks*nbytes);
    nchunks++;
   }
  while(i<=nchunks)
   {
    if(i==nchunks)
     {
      nbytes=lbytes;
      buffer=(char *)realloc(buffer, nbytes);
     }
    else
     buffer = (char *)malloc(sizeof(char)*nbytes);
    //printf("%i-0x%X\n", i, buffer);
    out = (char*)malloc(sizeof(char)*strlen(fname)+3);
    memset(buffer, '\0', nbytes);
    memset(out, '\0', strlen(fname)+3);
    if(buffer == NULL || out == NULL)
     printf("allocation error\n");
    fread(buffer , 1, nbytes ,src);
    sprintf(out, "%s.%.3d", fname, i);
    dest = fopen(out , "wb");
    if(dest == NULL)
     printf("allocation error\n");
    fwrite(buffer , 1, nbytes ,dest);
    fclose(dest);
    free(buffer);
    i++;
   }
  fclose(src);
  return 0;
 }
```


----------



## expl (Oct 24, 2010)

Can you explain me why are you calling realloc() if you free the 'buffer' at the end of every loop cycle? Set 'buffer = NULL' after you free it or just call malloc() instead.


----------



## SIFE (Oct 24, 2010)

I think I didn't read man pages very carefully, I remove some lines too and now every think ok:

```
.
.
if(i==nchunks)
 nbytes=lbytes; 
buffer = (char *)malloc(sizeof(char)*nbytes);
out = (char*)malloc(sizeof(char)*strlen(fname)+3);
.
.
```


----------

