I wrote the code below to dynamically allocate memory for the nested struct: Product **product;
The purpose of my code is for me to learn the right or better way to dynamically allocate memory for using double pointer to a struct inside another pointer to struct. The code runs fine.
Question: Any corrections or improvements for the code? Thanks in advance for sharing your experiences.
typedef struct {
int price;
} Product;
typedef struct {
Product **product;
int id;
} Inventory;
int main() {
int i, j, k, count=0;
int n1=2, n2=3, n3=2;
Inventory *inventory = malloc(n1 * sizeof *inventory);
for (i=0; i<n1; i++) {
inventory[i].product = malloc(n2 * sizeof *inventory[i].product);
for (j=0; j<n2; j++) {
inventory[i].product[j] = malloc(n3 * sizeof *inventory[i].product[j]);
}
}
for (i=0; i<n1; i++) {
for (j=0; j<n2; j++) {
for (k=0; k<n3; k++) {
inventory[i].product[j][k].price = count++;
printf("%d " , inventory[i].product[j][k].price);
}
}
}
return 0;
}
Output: 0 1 2 3 4 5 6 7 8 9 10 11
I have tried to use larger n1
, n2
and n3
, and your code works fine, too.
But here are two points needed to be noticed:
1. You need to add the free()
after you use the malloc()
to allocate memory.
2. If you want to use a c++ compiler (for example, g++) to compile this code, you need to cast the pointers' type returned by the malloc()
function.
The following is the code I tested. It will cost some time to run it:
#include "stdlib.h"
#include "stdio.h"
typedef struct {
int price;
} Product;
typedef struct {
Product **product;
int id;
} Inventory;
int main() {
int i, j, k, count=0;
int n1=525, n2=33, n3=141;
Inventory *inventory = (Inventory*)malloc(n1 * sizeof *inventory);
for (i=0; i<n1; i++) {
inventory[i].product = (Product**)malloc(n2 * sizeof *inventory[i].product);
for (j=0; j<n2; j++) {
inventory[i].product[j] = (Product*)malloc(n3 * sizeof *inventory[i].product[j]);
for (k=0; k<n3; k++) {
inventory[i].product[j][k].price = k*i*j;
}
}
}
for (i=0; i<n1; i++) {
for (j=0; j<n2; j++) {
for (k=0; k<n3; k++) {
printf("%d\n", inventory[i].product[j][k].price);
}
}
}
for (i=0; i<n1; i++) {
for (j=0; j<n2; j++) {
free(inventory[i].product[j]);
}
free(inventory[i].product);
}
free(inventory);
return 0;
}
Hope it helps.
The problem is product
is a pointer to a pointer, so if you intended to only an array of product
under Inventory
, you must use ->
notation instead of .
notation for price
. (See example 1 below) If however, you intended to actually nest an array of struct Product
under each pointer-to-Product
you allocate (ie subproducts
), see example 2 :
Note: after each allocation, you must check the return from malloc
to insure the allocation succeeded. (omitted in example 1 below for brevity, shown in full in example 2 )
example 1
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int price;
} Product;
typedef struct {
Product **product;
int id;
} Inventory;
int main () {
int i, j;
int n1 = 2, n2 = 3;
/* allocated / initiaize Inventory and Product */
Inventory *inventory = malloc (n1 * sizeof *inventory);
for (i = 0; i < n1; i++)
{
inventory[i].id = i;
inventory[i].product = malloc (n2 * sizeof *inventory[i].product);
for (j = 0; j < n2; j++)
{
inventory[i].product[j] = malloc (sizeof *inventory[i].product[j]);
inventory[i].product[j]->price = (j + 1) * 2;
}
}
/* print the inventory / procduct price */
for (i = 0; i < n1; i++)
{
printf ("\n product id : %d\n", inventory[i].id);
for (j = 0; j < n2; j++)
{
printf (" price : %d\n", inventory[i].product[j]->price);
}
}
/* free all memory */
for (i = 0; i < n1; i++)
{
for (j = 0; j < n2; j++)
free (inventory[i].product[j]);
free (inventory[i].product);
}
free (inventory);
return 0;
}
Output
$ ./bin/allocate_ptr_in_struct
product id : 0
price : 2
price : 4
price : 6
product id : 1
price : 2
price : 4
price : 6
Memory Use Error Check
$ valgrind ./bin/allocate_ptr_in_struct
==10167== Memcheck, a memory error detector
==10167== Copyright (C) 2002-2012, and GNU GPL'd, by Julian Seward et al.
==10167== Using Valgrind-3.8.1 and LibVEX; rerun with -h for copyright info
==10167== Command: ./bin/allocate_ptr_in_struct
==10167==
product id : 0
price : 2
price : 4
price : 6
product id : 1
price : 2
price : 4
price : 6
==10167==
==10167== HEAP SUMMARY:
==10167== in use at exit: 0 bytes in 0 blocks
==10167== total heap usage: 9 allocs, 9 frees, 104 bytes allocated
==10167==
==10167== All heap blocks were freed -- no leaks are possible
==10167==
==10167== For counts of detected and suppressed errors, rerun with: -v
==10167== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 2 from 2)
example 2
Per the comment that you intended to allocate a nested struct under **product
the change is straightforward:
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int price;
} Product;
typedef struct {
Product **product;
int id;
} Inventory;
int main () {
int i, j, k;
int n1 = 2, n2 = 3, n3 = 2;
/* allocated / initiaize Inventory and Product */
Inventory *inventory = malloc (n1 * sizeof *inventory);
if (!inventory) {
fprintf (stderr, "error: virtual memory exhausted.\n");
exit (EXIT_FAILURE);
}
for (i = 0; i < n1; i++)
{
inventory[i].id = i;
if (!(inventory[i].product = malloc (n2 * sizeof *inventory[i].product))) {
fprintf (stderr, "error: virtual memory exhausted.\n");
exit (EXIT_FAILURE);
}
for (j = 0; j < n2; j++)
{
if (!(inventory[i].product[j] = malloc (n3 * sizeof *inventory[i].product[j]))) {
fprintf (stderr, "error: virtual memory exhausted.\n");
exit (EXIT_FAILURE);
}
for (k = 0; k < n3; k++)
inventory[i].product[j][k].price = (j + 1) * 2 * (k + 1);
}
}
/* print the inventory / procduct price */
for (i = 0; i < n1; i++)
{
printf ("\n Inventory id : %d\n", inventory[i].id);
for (j = 0; j < n2; j++)
{
printf ("\n Product[%d]\n", j);
for (k = 0; k < n3; k++)
printf (" subproduct[%d][%d] price : %d\n",
j, k, inventory[i].product[j][k].price);
}
}
/* free all memory */
for (i = 0; i < n1; i++)
{
for (j = 0; j < n2; j++)
free (inventory[i].product[j]);
free (inventory[i].product);
}
free (inventory);
return 0;
}
Output
$ ./bin/allocate_ptr_in_struct
Inventory id : 0
Product[0]
subproduct[0][0] price : 2
subproduct[0][1] price : 4
Product[1]
subproduct[1][0] price : 4
subproduct[1][1] price : 8
Product[2]
subproduct[2][0] price : 6
subproduct[2][1] price : 12
Inventory id : 1
Product[0]
subproduct[0][0] price : 2
subproduct[0][1] price : 4
Product[1]
subproduct[1][0] price : 4
subproduct[1][1] price : 8
Product[2]
subproduct[2][0] price : 6
subproduct[2][1] price : 12
Memory Use Error Check
$ valgrind ./bin/allocate_ptr_in_struct
==23024== Memcheck, a memory error detector
==23024== Copyright (C) 2002-2012, and GNU GPL'd, by Julian Seward et al.
==23024== Using Valgrind-3.8.1 and LibVEX; rerun with -h for copyright info
==23024== Command: ./bin/allocate_ptr_in_struct
==23024==
Inventory id : 0
Product[0]
subproduct[0][0] price : 2
subproduct[0][1] price : 4
Product[1]
subproduct[1][0] price : 4
subproduct[1][1] price : 8
Product[2]
subproduct[2][0] price : 6
subproduct[2][1] price : 12
Inventory id : 1
Product[0]
subproduct[0][0] price : 2
subproduct[0][1] price : 4
Product[1]
subproduct[1][0] price : 4
subproduct[1][1] price : 8
Product[2]
subproduct[2][0] price : 6
subproduct[2][1] price : 12
==23024==
==23024== HEAP SUMMARY:
==23024== in use at exit: 0 bytes in 0 blocks
==23024== total heap usage: 9 allocs, 9 frees, 128 bytes allocated
==23024==
==23024== All heap blocks were freed -- no leaks are possible
==23024==
==23024== For counts of detected and suppressed errors, rerun with: -v
==23024== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 2 from 2)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.