-1

我试图使用malloc首先为数组分配一些空间,然后realloc扩展数组。该程序可以编译,但是当我运行该程序时,我在终端中得到了一些奇怪的内存打印。终端说一些类似的东西:======== Memory map =========然后是一堆数字和东西。

在我的程序中,我使用 malloc 如下:

struct station *addStation(struct station *graph, struct station newStation){
    graph = realloc(graph, sizeof(graph)+sizeof(struct station));
// Count the rows of graph[] here and add newStation to the end.
}

int main(){
    struct station *graph;
    graph = malloc(145*sizeof(struct station));
    graph = loadStations();
    newStation = graph[27];

    graph = addStation(graph, newStation);
}

我用错了吗?

4

6 回答 6

4

您正在覆盖内存所在的指针:

graph = malloc(145*sizeof(struct station));
graph = loadStations(); // malloc'd memory is lost here

如果您希望将一些数据添加到内存中,则需要将其作为指针传递:

loadStations(graph);

此外,您sizeof(graph)+sizeof(struct station)仅将数据保留到 1 个指针和 1 个站,这不是您想要的。您需要传递现有的尺寸信息:

struct station * addStation(struct station * graph, size_t * count, struct station newStation){
    size_t newCount = *count + 1;
    graph = realloc(graph, newCount * sizeof(struct station));
    if(!graph)
        return 0;
    *count = newCount;
    // Copy new station here
    return graph;
}

并主要调用:

    temp = addStation(graph, &graphCount, newStation);
    if(temp)
        graph = temp;
于 2013-09-26T08:05:38.973 回答
3

sizeof(graph)返回图形指针的大小,例如 4 个字节。它不返回先前分配的大小。

于 2013-09-26T08:04:46.660 回答
1

你需要解决一些问题。

  • 检查是否malloc成功。

    graph = malloc(145*sizeof(struct station));
    if(graph == NULL) {
        // your failure code here
    }
    
  • 不要覆盖指向您分配的内存的指针。

    graph = loadStations();   // Strict NO, this will cause a leak.
    

    而是传递指针以防您要修改数据。

    loadStations(graph);
    
  • 它更容易保持count你的stations通行证是addStation

    struct station *addStation(struct station *graph, struct station newStation, size_t *count) {
        graph = realloc(graph, (*count + 1) * sizeof(struct station));
        if(graph == NULL) {
            // your failure code here
        }
    
        ++(*count);
    
        // your code here
    

    }

  • 使用后释放分配的内存。

    free(graph);
    
于 2013-09-26T08:18:31.467 回答
1

是的。你的意思不是sizeof(graph)在你的addStation()函数中——这将是 的大小graph,它是struct station *一个常规指针(很可能是 4 或 8 个字节)。

您需要一个单独的变量来计算图表中的站数,然后realloc()每次都保持适当的大小。

于 2013-09-26T08:05:32.603 回答
0

这看起来是错误的,您使用 malloc 初始化图形,然后使用函数返回擦除它。

graph = malloc(145*sizeof(struct station));
graph = loadStations();

这也很糟糕,因为它会返回 sizeof(pointer)+sizeof(structure) 所以 4+struct size

sizeof(graph)+sizeof(struct station)

您无法使用 sizeof 检索先前 malloc 分配的大小。您需要将其存储在某个地方或存储元素的数量。

于 2013-09-26T08:06:52.247 回答
0

这是我的评论,这看起来是错误的。

通过执行 malloc,您有一个指向图形的指针。现在您已经用下面的行覆盖了这个指针值。

graph = loadStations();

/** 请看我的内联评论 **/

struct station *addStation(struct station *graph, struct station newStation){
    graph = realloc(graph, sizeof(graph)+sizeof(struct station));
// Count the rows of graph[] here and add newStation to the end.
}

int main(){
    struct station *graph;
    graph = malloc(145*sizeof(struct station));

    /** graph is a pointer, why are u manipulating the graph pointer to some other value **/
    /** The following line is bad **/
    graph = loadStations();



    newStation = graph[27];

    /** This is right **/
    graph = addStation(graph, newStation);
}
于 2013-09-26T08:07:08.217 回答