【发布时间】:2014-11-13 19:48:22
【问题描述】:
我正在用 C 语言编写一个命令行待办事项列表程序,并且出现了一些奇怪的行为。 Todos 存储在包含int 优先级和char[128] 名称的结构中。它们是使用接收这些参数并返回具有这些值的已分配和初始化结构的函数创建的。
返回的结构体最初是正确的,但是在再初始化 8 个结构体变量后,第一个初始化的结构体的值开始发生变化。我的猜测是,由于分配的内存不足,以前的值被新的值覆盖。我通过将馈送到malloc() 的大小从sizeof(TaskP) 增加到sizeof(int) + sizeof(char[128]) 暂时解决了这个问题。但是,我确信有更好的方法,并且非常感谢您解释为什么 malloc(sizeof(TaskP) 没有分配足够的内存(如果是这样的话)和正确的约定。
这是我的代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Task {
char description[128]; /* description of the task */
int priority; /* task priority */
};
typedef struct Task* Task;
Task createTask (int priority, char *desc)
{
Task newTask = malloc(sizeof(Task));
if (newTask) {
newTask->priority = priority;
strcpy(newTask->description, desc);
}
return newTask;
}
int main(int argc, const char * argv[])
{
Task taskList[10];
Task task1 = createTask(9, "task 1");
printf("task1: %d\n", task1->priority); /* I put printfs */
Task task2 = createTask(3, "task 2");
printf("task1: %d\n", task1->priority); /* after every call */
Task task3 = createTask(2, "task 3");
printf("task1: %d\n", task1->priority); /* to createTask() */
Task task4 = createTask(4, "task 4");
printf("task1: %d\n", task1->priority); /* to watch the value */
Task task5 = createTask(5, "task 5");
printf("task1: %d\n", task1->priority); /* of task1 change */
Task task6 = createTask(7, "task 6");
printf("task1: %d\n", task1->priority);
Task task7 = createTask(8, "task 7");
printf("task1: %d\n", task1->priority);
Task task8 = createTask(6, "task 8");
printf("task1: %d\n", task1->priority);
Task task9 = createTask(1, "task 9");
printf("task1: %d\n", task1->priority);
Task task10 = createTask(0, "task 10");
printf("task1: %d\n", task1->priority);
return 0;
}
这是我的输出:
task1: 9
task1: 9
task1: 9
task1: 9
task1: 9
task1: 9
task1: 9
task1: 0
task1: 1802723700
task1: 1802723700
更新
我在发布到堆栈溢出后几秒钟就发现了问题。多么尴尬。问题是我只为大小为 8 字节的指针 (Task) 分配了足够的内存,而不是为大小为 132 字节的struct Task 分配了内存。感谢所有耐心回答的人,很抱歉浪费了您的时间。
【问题讨论】:
-
请注意,
typedef struct Task* Task;令人困惑,尤其是当您有struct Task时。请不要typedef指针。 -
我通常不会,但这是我的计算机科学教授预先编写的代码,我无法更改。不过谢谢。