带有malloc,C的终端中的奇怪打印

带有malloc,C的终端中的奇怪打印,c,malloc,C,Malloc,我试图使用malloc首先为数组分配一些空间,然后使用realloc扩展数组。程序可以编译,但当我运行程序时,终端中会出现一些奇怪的内存打印。终端会显示这样的信息:============================,然后是一堆数字和其他东西 在我的程序中,我使用malloc如下: struct station *addStation(struct station *graph, struct station newStation){ graph = realloc(graph, s

我试图使用
malloc
首先为数组分配一些空间,然后使用
realloc
扩展数组。程序可以编译,但当我运行程序时,终端中会出现一些奇怪的内存打印。终端会显示这样的信息:
============================
,然后是一堆数字和其他东西

在我的程序中,我使用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);
}

我用错了吗?

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

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


您需要一个单独的变量来记录图形中的站数,然后每次将
realloc()
设置为适当的大小。

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

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;

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

graph = malloc(145*sizeof(struct station));
graph = loadStations();
这也很糟糕,因为它将返回sizeof(指针)+sizeof(结构)所以4+结构大小

sizeof(graph)+sizeof(struct station)

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

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

通过执行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);
}

你需要解决一些问题

  • 检查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);
    
  • 保持
    站点的
    计数
    更容易,传递到
    添加站点

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

main()
中,内存几乎立即泄漏。如果不知道loadStations()
是什么,我就说不出这种行为是什么。
“一堆数字和东西”
?那不是很具体。您应该将精确的输出粘贴到此处。您需要按地址传递
图形
。C仍然是一种传递值语言。+1他还可以使用带计数器和灵活数组成员的结构。不管怎样,他都需要把它放在某个地方。求你了。大小做什么?@theva
size\u t
是无符号整数类型,保证能够保存任何可能的数组索引。可以在stddef.h中找到它。