指针和所有[C]

发布于 2025-01-19 21:54:41 字数 1399 浏览 1 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

梦太阳 2025-01-26 21:54:41

在这些语句中:

    Classe[i].total_alunos.nome = (char *) malloc (sizeof(aluno.nome));
    Classe[i].total_alunos.nome = aluno.nome;

都产生了内存泄漏。

首先分配内存并将其地址分配给指针Classe[i].total_alunos.nome,然后使用for循环之前分配的内存地址重新分配指针:

    Classe[i].total_alunos.nome = aluno.nome;

至少你需要使用字符串函数 strcpy 如下:

    strcpy( Classe[i].total_alunos.nome, aluno.nome );

前提是您为指针 Classe[i].total_alunos.nome 指向的数组分配了足够的空间,因为在此分配中

Classe[i].total_alunos.nome = (char *) malloc (sizeof(aluno.nome));

您分配了记忆仅用于指针。即 sizeof(aluno.nome ) 等于 sizeof( char * )

但要注意,仍然会出现内存泄漏,因为指针aluno.nome指向的内存没有被释放。

没有多大意义

DADOS_ALUNOS aluno;

声明 DADOS_ALUNOS 类型的对象,然后动态分配内存

aluno.nome = (char *) malloc (sizeof(char) * 20);

:相反,您可以仅声明一个字符数组,如下所示:

char nome[20];

并在 for 循环中使用该数组:

scanf( " %19[^\n]", nome );
       ^^^^^^^^^^  

In these statements:

    Classe[i].total_alunos.nome = (char *) malloc (sizeof(aluno.nome));
    Classe[i].total_alunos.nome = aluno.nome;

there are produced memory leaks.

At first memory was allocated and its address was assigned to the pointer Classe[i].total_alunos.nome and then the pointer was reassigned with the address of the memory allocated before the for loop:

    Classe[i].total_alunos.nome = aluno.nome;

At least you need to use the string function strcpy like:

    strcpy( Classe[i].total_alunos.nome, aluno.nome );

provided that you allocated enough space for the array pointed to by the pointer Classe[i].total_alunos.nome because in this allocation

Classe[i].total_alunos.nome = (char *) malloc (sizeof(aluno.nome));

you allocating memory only for pointer. That is sizeof(aluno.nome ) is equal to sizeof( char * ).

However pay attention to that there will be still a memory leak because the memory pointed to by the pointer aluno.nome was not freed.

There is no great sense to declare an object of the type DADOS_ALUNOS:

DADOS_ALUNOS aluno;

and then dynamically allocate memory:

aluno.nome = (char *) malloc (sizeof(char) * 20);

Instead you could declare just a character array as for example:

char nome[20];

and use this array in the for loop:

scanf( " %19[^\n]", nome );
       ^^^^^^^^^^  
旧梦荧光笔 2025-01-26 21:54:41

几个问题。

首先,

Classe[i].total_alunos.nome = (char *) malloc (sizeof(aluno.nome));

从 C89 开始,对 malloc

Classe[i].total_alunos.nome = malloc( strlen( aluno.nome ) + 1 );

强制转换是不必要的,并且在 C89 标准下可以抑制有用的诊断。

表达式 aluno.nome 的类型为 char *,因此 sizeof( aluno.nome ) 为您提供指针的大小 ,而不是它指向的缓冲区的大小。使用 strlen 获取输入字符串的长度,然后加 1 以表示终止符。

接下来,更改

Classe[i].total_alunos.nome = aluno.nome;

strcpy( Classe[i].total_alunos.nome, aluno.nome );

= 运算符未定义将一个数组的内容复制到另一个数组 - 为此,您必须使用 strcpy 或 < code>memcpy,或在循环中单独复制元素。您在这里所做的就是将 aluno.nome 中存储的指针值分配给 Classe[i].total_alunos.nome,在此过程中您将丢失对使用 malloc 分配的内存,导致内存泄漏。

最后,不要使用 scanf 读取字符串输入,而是使用 fgets - 它可以更轻松地防止缓冲区溢出:

printf("Digite o nome do aluno\n");
fgets( aluno.nome, 20, stdin );

这将最多将 19 个字符读入 aluno .nome 并终止字符串。您也可以使用 scanf 来做到这一点:

scanf( "%19s", aluno.nome );

但长度必须在转换说明符中进行硬编码;您不能像使用 printf 那样在运行时使用 * 指定长度。

Several issues.

First, change

Classe[i].total_alunos.nome = (char *) malloc (sizeof(aluno.nome));

to

Classe[i].total_alunos.nome = malloc( strlen( aluno.nome ) + 1 );

As of C89 casts on malloc are unnecessary, and under the C89 standard can suppress a useful diagnostic.

The type of the expression aluno.nome is char *, so sizeof( aluno.nome ) gives you the size of the pointer, not the size of the buffer it points to. Use strlen to get the length of the input string, then add 1 to account for the terminator.

Next, change

Classe[i].total_alunos.nome = aluno.nome;

to

strcpy( Classe[i].total_alunos.nome, aluno.nome );

The = operator is not defined to copy the contents of one array to another - in order to do that you have to use a library function like strcpy or memcpy, or copy elements individually in a loop. All you are doing here is assigning the pointer value stored in aluno.nome to Classe[i].total_alunos.nome, and in the process you're losing the reference to the memory you allocated with malloc, leading to a memory leak.

Finally, instead of using scanf to read string input, use fgets - it makes it easier to prevent a buffer overrun:

printf("Digite o nome do aluno\n");
fgets( aluno.nome, 20, stdin );

This will read at most 19 characters into aluno.nome and terminate the string. You can do that with scanf as well:

scanf( "%19s", aluno.nome );

but the lengths have to be hardcoded in the conversion specifier; you can't use * to specify a length at runtime like you can with printf.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文