使用realloc和memcpy进行C编程问题

时间:2013-04-26 20:07:57

标签: c memory-management malloc memcpy realloc

我是这个论坛的新手。我事先感谢你的帮助。

每次我调用这个函数“agregar_segmento”我的全局指针名为“segment” 应调整大小以包含新数据值。数据被定义为名为“typeSeg”的“struct”。

问题是每次运行代码时我都会有不同的错误消息。 注意:我尝试使用valgrind,但不明白返回的内容。

我的代码是:

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));
}

在我的程序中,这个函数被许多部分多次调用(通常超过5000次)。

示例:

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

在获得的错误消息下面。

错误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

错误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

错误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

任何人都可以麻烦地查看我的功能代码并给我他的意见吗?

我打印调试“new_size”和“offset”的值,以确保没有损坏的内存,但有些错误。

谢谢!

2 个答案:

答案 0 :(得分:1)

表达式segmentos + offset对我来说似乎不对 由于segmentos属于typeSeg *类型,因此向其添加1会增加整个typeSeg的大小。
如果您想要第N个细分,则需要使用segmentos + N

答案 1 :(得分:1)

在第

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

您正在传递指向

的指针
offset * sizeof(typeSeg)
正如您所希望的那样,

字节位于segmentos之后,而不是offset个字节。指针算法会考虑指向对象的大小。

将要复制的索引传递给

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

或在添加偏移量

之前将segmentos转换为char*
memcpy(((char*)segmentos + offset), &aux,sizeof(typeSeg));