![](/img/trans.png)
[英]how to allocate memory for array of pointers to struct and then free it?
[英]How to allocate memory for a int array in a struct
我有一個結構
typedef struct
{
int size; //size of array
int array*
}
如何使用size變量和malloc
為int數組分配內存?
int *ptr;
ptr = malloc(sizeof *ptr * size);
//can now reference ptr[0], ptr[1], ... ptr[size-1].
上面的代碼做什么:為int數組分配內存時,您為所需的每個元素分配4個字節的內存(sizeof(int))。 因此,當您分配malloc時,您將分配元素的大小乘以所需元素的數量。
您可以創建一個用於初始化該結構的函數,可以創建一個如下所示的函數:
typedef struct {
int size; //size of array
int *array;
} mystruct;
mystruct * create_mystruct(int size) {
mystruct * st = (mystruct*) malloc(sizeof(mystruct));
st->size = size;
st->array = (int *) malloc(sizeof(int) * size);
return st;
}
這是一個工作的例子。
問題的答案將取決於您是聲明struct
還是指向 struct
的指針 。 在事件中,你聲明一個結構(如mystruct s
),那么你只需要為分配內存s.size
中的元素s.array
。 例如:
typedef struct mystruct {
int size;
int *array;
} mystruct;
...
mystruct s;
s.size = 5;
/* allocating array based on size */
s.array = malloc (s.size * sizeof *s.array);
if (!s.array) {
fprintf (stderr, "error: virtual memory exhausted.\n");
return 1;
}
注意:您必須驗證分配成功,並且每次調用malloc
時malloc
實際上都返回了一個有效地址。 (您可以創建一個函數來減少輸入)。 完成后,您僅需要免費的s.array
。
但是,如果聲明一個指向 struct
的指針 (例如, mystruct *msp = NULL;
),則現在必須為msp
和msp->array
分配內存。 例:
mystruct *msp = NULL;
msp = malloc (sizeof *msp);
if (!msp) {
fprintf (stderr, "error: virtual memory exhausted.\n");
return 1;
}
msp->size = 5;
/* allocating array based on size */
msp->array = malloc (msp->size * sizeof *msp->array);
if (!msp->array) {
fprintf (stderr, "error: virtual memory exhausted.\n");
return 1;
}
完成后,您必須同時free
兩者。
下面是一個簡單的示例,顯示了在兩種情況下array
的內存分配,使用和釋放:
#include <stdio.h>
#include <stdlib.h>
typedef struct mystruct {
int size;
int *array;
} mystruct;
int main (void) {
size_t i = 0;
size_t nelements = 0;
int rd = 0;
/*
* declaring a struct mystruct
*/
mystruct s;
s.size = 5;
/* allocating array based on size */
s.array = malloc (s.size * sizeof *s.array);
if (!s.array) {
fprintf (stderr, "error: virtual memory exhausted.\n");
return 1;
}
/* fill array */
for (rd = 0; rd < s.size; rd++)
if (scanf ("%d", &s.array[nelements]) == 1)
nelements++;
for (i = 0; i < nelements; i++)
printf (" s.array[%zu] = %d\n", i, s.array[i]);
/* free allocated memory */
free (s.array);
putchar ('\n');
/*
* declaring a pointer to mystruct
*/
mystruct *msp = NULL;
/* allocate memory for msp (mystruct pointer) */
msp = malloc (sizeof *msp);
if (!msp) {
fprintf (stderr, "error: virtual memory exhausted.\n");
return 1;
}
msp->size = 5;
/* allocating array based on size */
msp->array = malloc (msp->size * sizeof *msp->array);
if (!msp->array) {
fprintf (stderr, "error: virtual memory exhausted.\n");
return 1;
}
/* fill array */
rd = 0;
nelements = 0;
for (rd = 0; rd < msp->size; rd++)
if (scanf ("%d", &msp->array[nelements]) == 1)
nelements++;
for (i = 0; i < nelements; i++)
printf (" msp->array[%zu] = %d\n", i, msp->array[i]);
/* free allocated memory */
free (msp->array);
free (msp);
return 0;
}
使用/輸出示例
$ printf "2\n4\n6\n8\n10\n12\n14\n16\n18\n20\n" | ./bin/struct_alloc_int
s.array[0] = 2
s.array[1] = 4
s.array[2] = 6
s.array[3] = 8
s.array[4] = 10
msp->array[0] = 12
msp->array[1] = 14
msp->array[2] = 16
msp->array[3] = 18
msp->array[4] = 20
內存錯誤檢查
在您編寫的任何可以動態分配內存的代碼中,必須使用內存錯誤檢查程序。 對於Linux, valgrind
是通常的選擇。 濫用內存塊的微妙方法有很多,它們可能導致真正的問題,沒有任何借口不這樣做。 每個平台都有類似的內存檢查器。 它們易於使用。 只需通過它運行程序即可。
$ printf "2\n4\n6\n8\n10\n12\n14\n16\n18\n20\n" | valgrind ./bin/struct_alloc_int
==20515== Memcheck, a memory error detector
==20515== Copyright (C) 2002-2012, and GNU GPL'd, by Julian Seward et al.
==20515== Using Valgrind-3.8.1 and LibVEX; rerun with -h for copyright info
==20515== Command: ./bin/struct_alloc_int
==20515==
s.array[0] = 2
<snip>
msp->array[4] = 20
==20515==
==20515== HEAP SUMMARY:
==20515== in use at exit: 0 bytes in 0 blocks
==20515== total heap usage: 3 allocs, 3 frees, 56 bytes allocated
==20515==
==20515== All heap blocks were freed -- no leaks are possible
==20515==
==20515== For counts of detected and suppressed errors, rerun with: -v
==20515== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 2 from 2)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.