gpt4 book ai didi

c - 了解结构中字符串的动态内存分配

转载 作者:行者123 更新时间:2023-11-30 16:35:17 24 4
gpt4 key购买 nike

我遇到过一个实例,其中内存被动态分配给结构中的 char 指针,这种方式对我来说没有多大意义,但当然是有效的。一个similar question之前已经发布过。然而,这些答案并没有帮助我理解分配过程中实际发生的情况。

这是我找到的代码示例:

struct a_structure {
char *str;
struct a_structure *next;
};

内存已按以下方式分配:

ptr_start=(struct a_structure *)malloc(sizeof (struct a_structure *));
...
char *some_words="How does this work?";
ptr_start->str=(char *)malloc(strlen(some_words)+1);
strcpy(ptr_start->str, some_words);
ptr_start->next=(struct a_structure *)malloc(sizeof(struct a_structure *));
...

我不明白为什么malloc在这里与指针的大小一起使用。 ptr_start 是一个struct a_struct 类型的指针。这意味着它需要大小为 sizeof(struct a_struct) + 结构声明中未指定的字符串大小的内存。然而,在上面的示例中,malloc 返回另一个指向 a_struct 类型结构的指针的地址,我说得对吗?

最佳答案

I don't understand why malloc is used with the size of a pointer here. ptr_start is a pointer of type struct a_structure. That would mean it needs memory of size sizeof(struct a_structure) + the size of my string that hasn't been specified in the structure declaration

你是对的。要创建结构 a_struct 以便操作它,我们需要为整个结构分配内存。(除非该对象已经创建,并且出于某种原因,我们需要一个动态分配的指针来保存指向该对象的指针)。

but - of course - works.

由于上述原因,所提供的程序片段无法正常工作。

In the above example, however, malloc returns the address of yet another pointer pointing to a structure of type a_structure, am I right?

是的,你说得对。

这也是有问题的:

ptr_start->next=(struct a_struct *)malloc(sizeof(struct a_struct *));

ptr_start->next 已经可以保存指针了。我们通常不需要在这里分配指针。我们将分配一个指向现有的指针结构,否则我们将为整个结构分配内存。

参见示例:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

struct a_structure {
char *str;
struct a_structure *next;
};

struct a_structure * allocatePointer(void)
{
// ptr ptrToObj1Allocated points to allocted memory which can hold a ponter
struct a_structure * ptrToObj1Allocated = malloc(sizeof (struct a_structure *));
return ptrToObj1Allocated;
}

int main(){
// 1.
struct a_structure obj1; // structure a_structure has been allocated on the stack

struct a_structure * ptrToObj1 = & obj1; // points to the existing structure

char *some_words = "How does this work?";
ptrToObj1->str = malloc(strlen(some_words)+1);
if(ptrToObj1->str == NULL){printf("No enough memory\n"); return -1;}

strcpy(ptrToObj1->str, some_words); // copy the string
// now obj1 has its own copy of the string.

// 2.
// dynamically allocate another structure on the heap
// we want to allocate memory for the structure not just a memory to keep the pointer to the structure.

struct a_structure *obj2 = malloc( sizeof (struct a_structure)); // memory has been allocated to hold struct a_structure with 2 pointers
if(obj2 == NULL){printf("No enough memory\n"); return -2;}

char *words = "More words..";
obj2->str = malloc(strlen(words)+1);
if(obj2->str == NULL){printf("No enough memory\n"); return -3;}

strcpy(obj2->str, words); // copy the string

obj2->next = ptrToObj1; // points to the already existing object

//----
printf("String in obj1 is: %s\n", ptrToObj1->str);
printf("String in obj2 is: %s\n", obj2->str);
printf("obj2->next points to structure with string: %s\n", obj2->next->str );

// free the allocated memory:
free(ptrToObj1->str);

free(obj2->str);
free(obj2);

return 0;
}

输出:

String in obj1 is: How does this work?
String in obj2 is: More words..
obj2->next points to structure with string: How does this work?

关于c - 了解结构中字符串的动态内存分配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48987038/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com