C:访问违规的阅读位置与malloc
我想将内存分配给函数中的结构,并将给出的参数分配给函数。 我的代码:
linkedlist.c
FrameNode* createFrameNode(char name[MAX_NAME], int duration, char path[MAX_PATH])
{
Frame* newFrame = malloc(sizeof(Frame));
FrameNode* newNode = malloc(sizeof(FrameNode));
// Copy name
strcpy(newFrame->name, name);
// Add duration and path
newFrame->duration = duration;
strcpy(newFrame->path, path);
// Attach new frame to the new node
newNode->frame = newFrame;
newNode->next = NULL;
return newNode;
}
我会收回错误:
Exception thrown at 0x00007FFF18C4D215 (ucrtbased.dll) in C_Project.exe: 0xC0000005: Access violation reading location 0xFFFFFFFFFFFFFFFF.
如果我尝试获取与结构相关的任何内存。 您能告诉我的代码有什么问题吗?
linkedlist.h 中的结构
// Frame struct
typedef struct Frame
{
char* name;
unsigned int duration;
char* path;
} Frame;
// Link (node) struct
typedef struct FrameNode
{
Frame* frame;
struct FrameNode* next;
} FrameNode;
I want to allocate memory to structures in function and assign the values given as parameters to the function.
My code:
linkedList.c
FrameNode* createFrameNode(char name[MAX_NAME], int duration, char path[MAX_PATH])
{
Frame* newFrame = malloc(sizeof(Frame));
FrameNode* newNode = malloc(sizeof(FrameNode));
// Copy name
strcpy(newFrame->name, name);
// Add duration and path
newFrame->duration = duration;
strcpy(newFrame->path, path);
// Attach new frame to the new node
newNode->frame = newFrame;
newNode->next = NULL;
return newNode;
}
I get an error back:
Exception thrown at 0x00007FFF18C4D215 (ucrtbased.dll) in C_Project.exe: 0xC0000005: Access violation reading location 0xFFFFFFFFFFFFFFFF.
If I try to get any memory related to the structs.
Can you tell what is wrong from my code?
The structs in linkedList.h
// Frame struct
typedef struct Frame
{
char* name;
unsigned int duration;
char* path;
} Frame;
// Link (node) struct
typedef struct FrameNode
{
Frame* frame;
struct FrameNode* next;
} FrameNode;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
数据成员
name
结构结构框架
具有指针类型char *
在本内存分配后,用于类型
的对象结构框架
数据成员
名称
具有不确定的值。因此,
strcpy
的呼叫调用了未定义的行为。
您需要分配内存在哪里复制由指针
name
类似的字符串,例如,数据成员
path
存在相同的问题。也就是说,您需要分配一个字符阵列,其中要复制指针path
的字符串。通常,您应该检查所有内存分配是否成功。
请注意这些参数声明
char name [max_name]
和char Path [max_path]
由编译器调整为声明char *char *char *name
和char *路径
。这些参数应使用限定词const
声明,因为传递的字符串在功能中没有更改。The data member
name
of the structurestruct Frame
has the pointer typechar *
After this allocation of memory for an object of the type
struct Frame
the data member
name
has an indeterminate value.So this call of
strcpy
invokes undefined behavior.
You need to allocate memory where you are going to copy the string pointed to by the pointer
name
like for exampleThe same problem exists for the data member
path
. That is you need to allocate a character array where you are going to copy the string pointed to by the pointerpath
.In general you should check that all memory allocations were successful.
Pay attention to that these parameter declarations
char name[MAX_NAME]
andchar path[MAX_PATH]
are adjusted by the compiler to the declarationschar *name
andchar *path
. These parameters should be declared with qualifierconst
because the passed strings are not changed within the function.首先不将指针成员纳入
struct
来简化生活也不少见。尽管他们确实有自己的位置(如果不是绝对必要的话),但所需的内存分配和免费步骤将为代码增加不必要的并发症...因此,除非您被限制使用指针,否则只需使用
char> char
arrays反而。It is also not uncommon to simplify life by not including pointer members into a
struct
in the first place. Although they do have their place, if not absolutely necessary, the memory allocation and freeing steps required will add unnecessary complication to the code...So, unless you are constrained to use pointers, just use
char
arrays instead.