[英]Working with linked list + struct C
Here is the linked list and the struct: 这是链接列表和结构:
#define MAX_PATH_SIZE (256)
#define MAX_NAME_SIZE (50)
struct Frame
{
char *name;
unsigned int duration;
char *path; // may change to FILE*
};
typedef struct Frame frame_t;
struct Link
{
frame_t *frame;
struct Link *next;
};
typedef struct Link link_t;
And here's my function: 这是我的功能:
link_t* createFrame(char name[], int duration, char path[]){
frame_t * temp = (frame_t*)malloc(sizeof(frame_t));
temp->duration = duration;
strncpy(temp->name, name,MAX_NAME_SIZE);
strncpy(temp->path, path,MAX_PATH_SIZE);
link_t* newFrame = (link_t*)malloc(sizeof(link_t));
newFrame->frame = temp;
return newFrame;
}
The problem is that the function stop working in the line "strncpy(temp->name)..", the weird thing is that the temp->duration is working but it doesn't work with strings. 问题在于该函数停止在“ strncpy(temp-> name)..”行中工作,奇怪的是,temp-> duration在起作用,但不适用于字符串。 Error: "Unhandled exception at 0x0F744645 (msvcr120d.dll)"
错误:“ 0x0F744645(msvcr120d.dll)出现未处理的异常”
You didn't allocate memory for your name, now they point to unknown location and is undefined behaviour. 您没有为您的名字分配内存,现在它们指向未知的位置并且是未定义的行为。
temp->name = malloc((MAX_NAME_SIZE + 1) * sizeof(*temp->name));
temp->path = malloc((MAX_PATH_SIZE + 1) * sizeof(*temp->path));
temp->name[MAX_NAME_SIZE] = 0; //Manually add null termination
temp->name[MAX_PATH_SIZE] = 0; //Manually add null termination
strncpy(temp->name, name,MAX_NAME_SIZE);
Now, memory is allocated for name and path and you are able to copy data for name and path. 现在,已为名称和路径分配了内存,并且您能够复制名称和路径的数据。
Or, if you want, you can define your structure like this: 或者,如果需要的话,可以这样定义结构:
struct Frame {
char name[MAX_NAME_SIZE + 1];
unsigned int duration;
char path[MAX_PATH_SIZE + 1];
};
Then you won't need to call malloc
for name and path separatelly as memory will be allocated on first malloc
for structure already. 然后,您将不需要分别为名称和路径调用
malloc
,因为已经在结构的第一个malloc
上分配了malloc
。
You need to allocate memory to store strings with strncpy(), in your malloc you allocated only enough to store sizeof(struct Frame) bytes. 您需要分配内存以使用strncpy()存储字符串,在malloc中,您分配的内存仅足以存储sizeof(struct Frame)字节。
You may want to try this instead of strncpy... 您可能要尝试此方法,而不是strncpy ...
temp->name = strndup(name, MAX_NAME_SIZE);
temp->path = strndup(path,MAX_PATH_SIZE);
...if you are insisting on restricting the max size of strings. ...如果您坚持限制字符串的最大大小。
Try this : 尝试这个 :
#define MAX_PATH_SIZE (256)
#define MAX_NAME_SIZE (50)
struct Frame
{
char name[MAX_NAME_SIZE];
unsigned int duration;
char path[MAX_PATH_SIZE]; // may change to FILE*
};
typedef struct Frame frame_t;
struct Link
{
frame_t *frame;
struct Link *next;
};
typedef struct Link link_t;
link_t* createFrame(char name[MAX_NAME_SIZE], int duration, char path[MAX_PATH_SIZE]){
frame_t * temp = (frame_t*)malloc(sizeof(frame_t));
temp->duration = duration;
strncpy(temp->name, name, MAX_NAME_SIZE);
strncpy(temp->path, path, MAX_PATH_SIZE);
link_t* newFrame = (link_t*)malloc(sizeof(link_t));
newFrame->frame = temp;
return newFrame;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.