Note that there are some explanatory texts on larger screens.

plurals
  1. POC programming problems with realloc and memcpy
    text
    copied!<p>I'm new to this forum. I thank you in advance for the help.</p> <p>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".</p> <p>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.</p> <p>my code is:</p> <pre><code>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), &amp;aux,sizeof(typeSeg)); } </code></pre> <p>in my program this function is called many times (usually more than 5000) from many parts.</p> <p>the example:</p> <pre><code>agregar_segmento("P",nEntidad,(xant1 == 0?px1:xant1),((yant1 == 0?py1:yant1)),atof("0.0"),px1,py1,atof("0.0")); </code></pre> <p>below the error messages obtained.</p> <p>error 1</p> <pre><code>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 </code></pre> <p>error 2</p> <pre><code>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 </code></pre> <p>error 3</p> <pre><code>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) &gt;= (unsigned long)(nb)' failed. Aborted </code></pre> <p>Can anyone take the trouble to see the code of my function and giving me his opinion?</p> <p>I print to debug the values ​​of "new_size" and "offset" to make sure no corrupt memory but something is wrong.</p> <p>thanks!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload