C:访问违规的阅读位置与malloc

发布于 2025-02-09 08:13:40 字数 1082 浏览 2 评论 0原文

我想将内存分配给函数中的结构,并将给出的参数分配给函数。 我的代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

对你而言 2025-02-16 08:13:40

数据成员name结构结构框架具有指针类型char *

// Frame struct
typedef struct Frame
{
    char* name;
    unsigned int duration;
    char* path;
} Frame;

在本内存分配后,用于类型的对象结构框架

Frame* newFrame = malloc(sizeof(Frame));

数据成员名称具有不确定的值。

因此,strcpy的呼叫

// Copy name
strcpy(newFrame->name, name);

调用了未定义的行为。

您需要分配内存在哪里复制由指针name类似的字符串,例如,

// Copy name
newFrame->name = malloc( strlen( name ) + 1 );
strcpy(newFrame->name, name);

数据成员path存在相同的问题。也就是说,您需要分配一个字符阵列,其中要复制指针path的字符串。

通常,您应该检查所有内存分配是否成功。

请注意这些参数声明char name [max_name]char Path [max_path]由编译器调整为声明char *char *char *namechar *路径。这些参数应使用限定词const声明,因为传递的字符串在功能中没有更改。

FrameNode* createFrameNode(const char name[MAX_NAME], int duration, const char path[MAX_PATH]);

The data member name of the structure struct Frame has the pointer type char *

// Frame struct
typedef struct Frame
{
    char* name;
    unsigned int duration;
    char* path;
} Frame;

After this allocation of memory for an object of the type struct Frame

Frame* newFrame = malloc(sizeof(Frame));

the data member name has an indeterminate value.

So this call of strcpy

// Copy name
strcpy(newFrame->name, name);

invokes undefined behavior.

You need to allocate memory where you are going to copy the string pointed to by the pointer name like for example

// Copy name
newFrame->name = malloc( strlen( name ) + 1 );
strcpy(newFrame->name, name);

The 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 pointer path.

In general you should check that all memory allocations were successful.

Pay attention to that these parameter declarations char name[MAX_NAME] and char path[MAX_PATH] are adjusted by the compiler to the declarations char *name and char *path. These parameters should be declared with qualifier const because the passed strings are not changed within the function.

FrameNode* createFrameNode(const char name[MAX_NAME], int duration, const char path[MAX_PATH]);
月牙弯弯 2025-02-16 08:13:40

首先不将指针成员纳入struct来简化生活也不少见。尽管他们确实有自己的位置(如果不是绝对必要的话),但所需的内存分配和免费步骤将为代码增加不必要的并发症...

因此,除非您被限制使用指针,否则只需使用char> char arrays反而。

// Frame struct
typedef struct Frame
{
    char name[80];
    unsigned int duration;
    char path[MAX_PATH_LEN];//or whatever define is provided on your system
} Frame; 

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.

// Frame struct
typedef struct Frame
{
    char name[80];
    unsigned int duration;
    char path[MAX_PATH_LEN];//or whatever define is provided on your system
} Frame; 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文