1

我是这个论坛的新手。我提前感谢您的帮助。

每次我调用这个函数“agregar_segmento”时,我名为“segment”的全局指针都应该调整大小以包含新的数据值​​。数据被定义为一个名为“typeSeg”的“结构”。

问题是每次我运行代码时都会收到不同的错误消息。注意:我尝试使用 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”的值,以确保没有损坏的内存但有问题。

谢谢!

4

2 回答 2

1

这个表达segmentos + offset对我来说似乎是错误的。
由于segmentos是 类型typeSeg *, 向 它 加 1 会 增加 一个 整体 的 大小typeSeg.
如果你想要第 N 段,你需要使用segmentos + N.

于 2013-04-26T20:15:08.613 回答
1

在行

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

你正在传递一个指针

offset * sizeof(typeSeg)

后面的字节segmentos,而不是offset你想要的字节。指针算法考虑了指向对象的大小。

传递要复制到的索引,

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

或在添加偏移量之前转换segmentos为 achar*

memcpy(((char*)segmentos + offset), &aux,sizeof(typeSeg));
于 2013-04-26T20:15:33.950 回答