与malloc,C终端奇怪的打印

时间:2013-09-26 07:58:53

标签: c malloc

我正在尝试使用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);
}

我使用它错了吗?

6 个答案:

答案 0 :(得分: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;

答案 1 :(得分:3)

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

答案 2 :(得分:1)

是。您的sizeof(graph)函数中的addStation()并不意味着graph的大小,struct station *realloc(),常规指针(很可能是4或8)个字节)。

您需要一个单独的变量来计算图表中的工作站数量,然后每次都{{1}}到适当的大小。

答案 3 :(得分:1)

你需要解决一些问题。

  • 检查malloc是否成功。

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

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

    代替传递指针,以防您想要修改数据。

    loadStations(graph);
    
  • 更容易保留count的{​​{1}}并传递到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
    

答案 4 :(得分:0)

使用malloc初始化图形时会出现错误,然后用函数返回擦除它。

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

这也很糟糕,因为它会重新调整sizeof(指针)+ sizeof(结构)所以4 + struct size

sizeof(graph)+sizeof(struct station)

您无法使用sizeof检索前一个malloc分配的大小。你需要将它存储在某个地方或存储多个元素。

答案 5 :(得分: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);
}