【发布时间】:2017-07-04 11:11:53
【问题描述】:
我是 C 的新手,我试图在结构中创建一个动态数组,我需要向其中添加动态数量的项目。
如果我在初始化容器时使用带有大数字(如 +100000)的 malloc,则程序可以正常工作。
但是,如果我只是分配sizeof(struct ...) 内存,然后尝试使用realloc 重新分配,当我尝试向数组中添加新项目时程序会崩溃。即使我在重新分配时添加了 +1000000 大小,它仍然会崩溃,而如果我在最初分配时使用它,则相同的大小仍然有效。
减少代码以显示我的问题:
struct Station {
char name[100];
};
struct StationContainer {
int numberOfStations;
struct Station *stations[];
};
struct StationContainer *makeContainer() {
struct StationContainer *new;
// If I add +1000000 in size here, the program works fine
new = (struct StationContainer *) malloc(sizeof(struct StationContainer));
if (new == NULL) {
fprintf(stderr, "makeContainer: out of memory\n");
exit(1);
}
new->numberOfStations = 0;
return new;
}
int main(int argc, int argv[]) {
// Initialize the container
struct StationContainer *container;
container = makeContainer();
// Add new station to container
struct Station *new;
new = (struct Station *) malloc(sizeof(struct Station));
if (new == NULL) {
fprintf(stderr, "makeStation: out of memory\n");
exit(1);
}
strcpy(new->name, name);
// Add to the container
// Even if I add +1000000 here, it still crashes below when I add new item
container = (struct StationContainer *) realloc(container, sizeof(struct StationContainer) + 1000000);
if (container == NULL) {
fprintf(stderr, "makeStation: container out of memory\n");
exit(1);
}
container->stations[container->numberOfStations] = new;
container->numberOfStations++;
}
EDIT1:
在我的精简版中发现了问题,但是,一旦我返回完整版,我仍然收到错误。我收到了container out of memory。那是因为我的container->numberOfStations 在第一次增加后由于某种原因变得非常大。
我的完整代码在: codepad
adjacencies.txt 文件内容:
Baker Street, Bond Street, Regent's Park
Bond Street, Marble Arch, Baker Street, Oxford Circus, Green Park
Marble Arch, Bond Street
Green Park, Bond Street, Oxford Circus, Piccadilly Circus
Regent's Park, Baker Street, Oxford Circus
Oxford Circus, Bond Street, Regent's Park, Warren Street, Tottenham Court Road, Piccadilly Circus
Piccadilly Circus, Green Park, Oxford Circus, Leicester Square, Charing Cross
Warren Street, Oxford Circus, Goodge Street
Goodge Street, Warren Street, Tottenham Court Road
Tottenham Court Road, Oxford Circus, Goodge Street, Holborn, Leicester Square
Leicester Square, Piccadilly Circus, Tottenham Court Road, Covent Garden, Charing Cross
Charing Cross, Piccadilly Circus, Leicester Square
Covent Garden, Leicester Square, Holborn
Holborn, Tottenham Court Road, Chancery Lane
Chancery Lane, Holborn
当我制作电台时,如果我保持container->numberOfStations不变,就没有问题。但是,当我至少增加一次时,结合realloc,它会变成巨大的数字。示例输出:
Number of stations: 0
!Station name:[Baker Street]
Number of stations: 1
!Station name:[Bond Street]
Number of stations: 4067560
makeStation: container out of memory
我觉得某处出现了一个小错误,导致我的 numberOfStation 发生了剧烈变化,即使它应该只增加 1?
【问题讨论】:
-
第一个注意事项:不要强制转换 malloc 返回值。第二点:不推荐调用变量
new。name声明在哪里?strcpy(new->name, name); -
会改变的,我只是在听我的 Uni 讲座的笔记
-
您能否使用
gdb提供一些输入和确切的错误行? -
sizeof(struct StationContainer) + 1000000在malloc()/realloc()中没有意义,因为您希望分配的内存量为struct StationContainer的多个大小 - 你是不是要写sizeof(struct StationContainer) * 1000000 -
@ChrisTurner no, + 1000000 仅用于调试,表明当我首先分配内存时它可以工作,但是当我尝试重新分配它时它不起作用。首先是的,它将是
sizeof(struct(Station)) * numberOfStations