Search code examples
cmemory-managementmallocmemcpyrealloc

C programming problems with realloc and memcpy


I'm new to this forum. I thank you in advance for the help.

every time I call to this function "agregar_segmento" my global pointer named "segment" should resize to contain new data values​​. data are defined as a "struct" named "typeSeg".

the problem is that every time I run the code I have different error messages. note: I tried to use valgrind but do not understand what is returned.

my code is:

typedef struct {
    char cmd[2];int nEnt;
    float x1;float y1;float z1;
    float x2;float y2;float z2;
} typeSeg;

static typeSeg *segmentos;
static int posSeg=0;
static int cantSeg=0;

void agregar_segmento(char *cmd,int nEnt, float x1,float y1,float z1,float x2,float y2,float z2){
    typeSeg aux;
    long new_size;
    long offset;
    strcpy(aux.cmd,(char*)cmd);
    aux.nEnt = nEnt;
    aux.x1=x1; aux.y1=y1; aux.z1=z1;
    aux.x2=x2; aux.y2=y2; aux.z2=z2;
    posSeg++;cantSeg++;
    new_size = sizeof(typeSeg) * posSeg;
    offset = (new_size - sizeof(typeSeg));
    printf("new_size = %lu , offset = %lu, size of my struct = %d\n",new_size,offset,sizeof(aux));
    if(posSeg==1){
        segmentos = (typeSeg*) malloc(new_size);
    }else{
        segmentos = (typeSeg*) realloc(segmentos,new_size);
    }
    memcpy((segmentos + offset), &aux,sizeof(typeSeg));
}

in my program this function is called many times (usually more than 5000) from many parts.

the example:

agregar_segmento("P",nEntidad,(xant1 == 0?px1:xant1),((yant1 == 0?py1:yant1)),atof("0.0"),px1,py1,atof("0.0"));

below the error messages obtained.

error 1

new_size = 32 , offset = 0, size of my struct = 32
new_size = 64 , offset = 32, size of my struct = 32
new_size = 96 , offset = 64, size of my struct = 32
new_size = 128 , offset = 96, size of my struct = 32
new_size = 160 , offset = 128, size of my struct = 32
......
......
new_size = 13024 , offset = 12992, size of my struct = 32
new_size = 13056 , offset = 13024, size of my struct = 32
new_size = 13088 , offset = 13056, size of my struct = 32
Segmentation fault

error 2

new_size = 32 , offset = 0, size of my struct = 32
new_size = 64 , offset = 32, size of my struct = 32
new_size = 96 , offset = 64, size of my struct = 32
new_size = 128 , offset = 96, size of my struct = 32
new_size = 160 , offset = 128, size of my struct = 32
new_size = 192 , offset = 160, size of my struct = 32
......
......
new_size = 5440 , offset = 5408, size of my struct = 32
new_size = 5472 , offset = 5440, size of my struct = 32
Segmentation fault

error 3

new_size = 32 , offset = 0, size of my struct = 32
new_size = 64 , offset = 32, size of my struct = 32
new_size = 96 , offset = 64, size of my struct = 32
new_size = 128 , offset = 96, size of my struct = 32
......
......
new_size = 1216 , offset = 1184, size of my struct = 32
new_size = 1248 , offset = 1216, size of my struct = 32
new_size = 1280 , offset = 1248, size of my struct = 32
craster: malloc.c:4630: _int_malloc: Assertion `(unsigned long)(size) >= (unsigned long)(nb)' failed.
Aborted

Can anyone take the trouble to see the code of my function and giving me his opinion?

I print to debug the values ​​of "new_size" and "offset" to make sure no corrupt memory but something is wrong.

thanks!


Solution

  • In the line

    memcpy((segmentos + offset), &aux,sizeof(typeSeg));
    

    you are passing a pointer to

    offset * sizeof(typeSeg)
    

    bytes behind segmentos, and not offset bytes, as you intend. Pointer arithmetic takes the size of the pointed-to objects into account.

    Pass the index you want to copy to,

    memcpy((segmentos + posSeg-1), &aux,sizeof(typeSeg));
    

    or convert segmentos to a char* before adding the offset

    memcpy(((char*)segmentos + offset), &aux,sizeof(typeSeg));