繁体   English   中英

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

[英]Understanding dynamic memory allocation of a string within a struct

我遇到了一个实例,其中动态地将内存分配给结构中的char指针,这种方式对我来说意义不大,但可以工作。 之前已经发布过类似的问题 但是,答案并没有帮助我理解分配过程中实际发生的情况。

这是我发现的代码示例:

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_startstruct a_structure类型的指针。 这意味着它将需要大小为sizeof(struct a_structure)内存+结构声明中未指定的我的字符串的大小。 但是,在上面的示例中,malloc返回另一个指针的地址,该指针指向a_structure类型的结构,对吗?

假设您具有struct a_structure* ptr_start ,则此代码不正确,因此不起作用:

ptr_start=(struct a_structure *)malloc(sizeof (struct a_structure *));

应该是:

ptr_start = malloc(sizeof *ptr_start);

之所以“看起来可行”,是因为您调用了未定义的行为,任何事情都可能发生。 该程序似乎可以工作一会儿,然后在另一时间崩溃。

但是,这只是分配结构本身。 像所有指针一样,它内部的指针将指向其他地方分配的内存。 以下带有malloc的字符串和strcpy()是一种实现方法。

但是,由于与上述相同的原因,最后一行是不正确的。

我不明白为什么在这里将malloc与指针的大小一起使用。 ptr_start是类型为struct a_structure的指针。 这意味着它需要的内存大小为sizeof(struct a_structure)+在结构声明中未指定的我的字符串的大小

你是对的。 要创建structure a_structure以便对其进行操作,我们需要为整个结构分配内存。 (除非已经创建了对象,并且出于任何原因,我们都需要动态分配的指针来保存指向该对象的指针)。

但是-当然-可行。

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

但是,在上面的示例中,malloc返回另一个指针的地址,该指针指向a_structure类型的结构,对吗?

是的,你是对的。

这也是有问题的:

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

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?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM