heap - C: how to manage a large structure? -
in c:
i trying use structure containing large array, , have stack overflow error while declaring it. guess (correctly?) don't have enough memory in stack, , therefore, should use heap (i don't want change stack memory size, code used others). show me way simply? or should use else structure?
my code - definitions.h:
#define a_large_number 100000 struct std_calibrations{ double e[a_large_number]; };
my code - main.c:
int main(int argc, char *argv[]) { /* ... */ // stack overflows here: struct std_calibrations calibration; /* ... */ return (0); }
thank help!
a couple of options:
use
malloc(3)
,free(3)
dynamically allocate structure @ runtime. option you're talking when "should use heap."struct std_calibrations *calibration = malloc(sizeof *calibration);
and later,
free(calibration);
give structure static storage duration. either add
static
keyword or make global. option may change semantics how use structure, given example code, should fine.
Comments
Post a Comment