我有一个结构体
typedef struct
{
int size; //size of array
int array*
}
如何使用大小变量和 malloc
为 int 数组分配内存?
您的问题的答案将取决于您声明的是struct
还是指向struct
的指针。如果您声明了一个结构(例如 mystruct s
(,那么您只需要为 s.array
中的s.size
元素分配内存。例如:
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] = %dn", 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] = %dn", i, msp->array[i]);
/* free allocated memory */
free (msp->array);
free (msp);
return 0;
}
示例使用/输出
$ printf "2n4n6n8n10n12n14n16n18n20n" | ./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 "2n4n6n8n10n12n14n16n18n20n" | 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)
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;
}
下面是一个工作 idone 作为示例。