在他们知道多少之前,他们在我所见的任何地方分配内存
他们将填充结构。
是和不是。
struct Friends {
char *firstname;
char *lastname;
};
这完全取决于您打算如何使用您的结构。您可以通过多种方式使用struct Friends。您可以声明结构的静态实例,然后简单地将现有字符串的地址分配给您的 firstname 和 lastname 成员指针,例如:
int main (void) {
Struct Friends friend = {{Null}, {NULL]};
/* simple assignment of pointer address
* (memory at address must remain valid/unchanged)
*/
friend.firstname = argc > 1 ? argv[1] : "John";
friend.lastname = argc > 2 ? argv[2] : "Smith";
printf ("\n name: %s %s\n\n", friend.firstname, friend.lastname);
但是,在大多数情况下,您需要为firstname 和lastname 成员创建信息的副本并存储字符串的副本。在这种情况下,您需要为每个指针分配一个新的内存块,并将每个新块的起始地址分配给每个指针。在这里,您现在知道您的字符串是什么,您只需为每个字符串的长度分配内存(+1 用于 nul-terminating 字符)例如:
int main (int argc, char **argv) {
/* declare static instance of struct */
struct Friends friend = {NULL, NULL};
char *first = argc > 1 ? argv[1] : "John";
char *last = argc > 2 ? argv[2] : "Smith";
/* determine the length of each string */
size_t len_first = strlen (first);
size_t len_last = strlen (last);
/* allocate memory for each pointer in 'friend' */
friend.firstname = malloc (len_first * sizeof *friend.firstname + 1);
friend.lastname = malloc (len_last * sizeof *friend.lastname + 1);
然后您只需将每个字符串复制到每个成员的地址:
/* copy names to new memory referenced by each pointer */
strcpy (friend.firstname, first);
strcpy (friend.lastname, last);
最后,一旦你用完分配的内存,你必须用free释放内存。 注意:您只能free 之前使用malloc 或calloc 分配的内存。永远不要盲目地尝试释放尚未以这种方式分配的内存。要释放成员,您只需要:
/* free allocated memory */
free (friend.firstname);
free (friend.lastname);
将所有部分放在一起的简短示例是:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Friends {
char *firstname;
char *lastname;
};
int main (int argc, char **argv) {
/* declare static instance of struct */
struct Friends friend = {NULL, NULL};
char *first = argc > 1 ? argv[1] : "John";
char *last = argc > 2 ? argv[2] : "Smith";
/* determine the length of each string */
size_t len_first = strlen (first);
size_t len_last = strlen (last);
/* allocate memory for each pointer in 'friend' */
friend.firstname = malloc (len_first * sizeof *friend.firstname + 1);
friend.lastname = malloc (len_last * sizeof *friend.lastname + 1);
/* copy names to new memory referenced by each pointer */
strcpy (friend.firstname, first);
strcpy (friend.lastname, last);
printf ("\n name: %s %s\n\n", friend.firstname, friend.lastname);
/* free allocated memory */
free (friend.firstname);
free (friend.lastname);
return 0;
}
总是在编译时启用警告,例如:
gcc -Wall -Wextra -o bin/struct_dyn_alloc struct_dyn_alloc.c
(如果不使用gcc,那么你的编译器会有类似的选项)
每当您在代码中动态分配内存时,请通过内存错误检查程序运行,以确保您不会以某种方式滥用分配的内存块,并确认所有内存已被释放。做起来很简单。所有操作系统都有某种类型的检查器。 valgrind 是 Linux 上的正常选择。例如:
$ valgrind ./bin/struct_dyn_alloc
==14805== Memcheck, a memory error detector
==14805== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==14805== Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info
==14805== Command: ./bin/struct_dyn_alloc
==14805==
name: John Smith
==14805==
==14805== HEAP SUMMARY:
==14805== in use at exit: 0 bytes in 0 blocks
==14805== total heap usage: 2 allocs, 2 frees, 11 bytes allocated
==14805==
==14805== All heap blocks were freed -- no leaks are possible
==14805==
==14805== For counts of detected and suppressed errors, rerun with: -v
==14805== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)