C中的结构malloc有问题

C中的结构malloc有问题,c,pointers,malloc,C,Pointers,Malloc,我目前正开始学习C语言,但还不能想出一个解决方案 守则: #include <stdlib.h> #include <string.h> struct { char *name; int ID; [...] } example; int currentID = 1; int new_example(char *name){ char *the_name = malloc(strlen(name) * sizeof(char));

我目前正开始学习C语言,但还不能想出一个解决方案

守则:

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

struct {
   char *name;
   int ID;
   [...]
} example;  

int currentID = 1;

int new_example(char *name){

   char *the_name = malloc(strlen(name) * sizeof(char));
   example *test = malloc(sizeof(example));

   test->name = name;
   test->ID = currentID;
   currentID++;

   [...]

   return test->ID;
}
现在我知道我必须使用malloc和free作为该结构的名称成员,以及结构本身。我现在正在做的只是为_名称分配内存,但是test->name没有为它分配内存。所以我想我的问题是,如何将test->name写入以前的malloc内存?对不起,如果我的问题不够清楚,我真的不知道如何更好地解释它


提前感谢

无需将内存分配为单独的步骤,但如果必须,请记住为终止“\0”添加另一个字节。用于直接分配测试->名称,以及其自己的新名称副本:


你为什么不这样做:

  example *test = malloc(sizeof(example));

  test->name = malloc((strlen(name) + 1) * sizeof(char)); // +1 for null terminator
  test->ID = currentID;

  strcpy(test->name, name);//copy name contents


strdup将测量字符串,malloc一些内存,复制字符串并将malloced内存返回给您

将指针指向新内存

char *the_name = malloc((strlen(name)+1) * sizeof(char));
example *test = malloc(sizeof(example));

test->name = the_name ;
并将字符串复制到它

strcpy( test->name , name )  ;
请注意,您需要为空终止符再分配一个字符:


strlenname+1*sizeofchar

它应该类似于以下内容:

int new_example(char *name){

   example *test = malloc( sizeof *test ); // or calloc( 1, sizeof *test )
   if ( test ) // you should always check the result of malloc or calloc
   {   
     test->name = calloc( strlen( name ) + 1,   // + 1 for 0 terminator
                          sizeof *test->name );  
     if ( test->name )
     {
       strcpy( test->name, name );
       test->ID = currentID;
       currentID++;

       [...]
       return test->ID;
     }
     else
       free( test );
   }
   return -1; // error indication
}
void delete_example( example *ex )
{
  free( ex->name );
  free( ex );
}
一些注意事项:

与sizeof示例相比,我更喜欢使用sizeof*测试;如果我更改了测试的类型,我就不必担心在malloc或calloc调用中更改相应的类型。sizeof*测试->名称相同。根据定义,sizeof char==1,因此可以调用calloc 编写了calloc strlen name+1,1,但我仍然喜欢显式sizeof表达式,以防您决定将wchar用作name或其他宽字符类型

calloc将分配的内存归零。对于大多数类型来说,这并不重要,但我在为字符串分配空间时是有意义的

您应该始终检查malloc和calloc调用的结果。若不能为测试分配内存,那个么就不应该尝试为测试->名称分配内存。类似地,如果您不能为test->name分配内存,那么这可能是一种不好的迹象,因此您应该放弃目前为止所做的工作

我假设您将测试存储到[…]部分中的某个持久结构中;如果没有,则会出现内存泄漏,因为在函数退出时会丢失测试指针

释放对象时,首先要释放名称成员,如下所示:

int new_example(char *name){

   example *test = malloc( sizeof *test ); // or calloc( 1, sizeof *test )
   if ( test ) // you should always check the result of malloc or calloc
   {   
     test->name = calloc( strlen( name ) + 1,   // + 1 for 0 terminator
                          sizeof *test->name );  
     if ( test->name )
     {
       strcpy( test->name, name );
       test->ID = currentID;
       currentID++;

       [...]
       return test->ID;
     }
     else
       free( test );
   }
   return -1; // error indication
}
void delete_example( example *ex )
{
  free( ex->name );
  free( ex );
}

啊,太谢谢你了!如果你不介意的话,我有一个后续问题。我有一个再次释放内存的函数,free_example example*test。如何正确地释放结构?freetest->name;免费测试;给我一个分段错误。我是做错了还是在别处出错了?freetest->name;,免费测试;这是正确的。可能还有一个错误,+1应该应用于strlenname,而不是multiplication@MattMcNabb有乘法吗-并非所有实现都支持strdup。这不应编译。示例是变量,而不是类型-您可能希望编写结构示例{…};。而且,无论何时你想在C中使用类型,而不是C++,你必须编写Stult例子,比如在StultExcel *测试中。它确实使用gcc编译了fine-thoughtos:error:“test”未声明在此函数中的首次使用。我猜它试图将示例与测试相乘。对于编译错误,请参阅。谢谢!我所有的例子都保存在一个数组中。这里有一个例子[ID]=*这个;在[…]的某个地方,可以吗?根据我的逻辑,当我使用示例[ID of the structure I want]时,它应该总是给出我想要的结构,对吗?