無處不看,他們分配內存,然後才知道他們將填補多少 。
是和否。
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終止字符),例如:
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
,那麼你的編譯器將有類似的選項)
只要在代碼中動態分配內存,運行內存錯誤檢查程序以確保您不會以某種方式濫用分配的內存塊,並確認所有內存已被釋放。這很容易做到。所有操作系統都有一些類型的檢查器。在Linux上,valgrind
是正常的選擇。例如:
$ 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)
'2倍的sizeof(char)的2倍=應1Byte'是'2倍的sizeof(字符*)' - 這取決於平臺 –
所有的假設都是錯誤的。 'malloc(sizeof(Friends))'會分配(至少)'2 * sizeof(char *)' - 介意'*'。它爲指針分配空間,而不是字符。如果你想填充一些東西,你將不得不執行額外的分配。 –
非常感謝,我現在明白了! – Scarh