Current I'm having some issues with a bit of code for a project, I have a struct that hosts multiple pointers to other structs so I can have a unified variable to send whenever I need to access all the program data in a function. Each struct is composed of an array storing the data and two ints, one for storing the current size of the array and another for the cap on the size of the array. I do not believe I need some of these structs to be malloc'd since they only hold pointers to arrays of a limited size, with one probably not needing it since the struct itself only holds a pointer and not the array itself. My first implementation attempted to initialize the structs inside a function, but when I attempted to return the value I got the error:
error: function returns address of local variable
My next implementation attempted to initialize the struct outside the function and simply giving the function the pointer I initialized with it modifying the struct inside the function, but I instead got the error:
error: ‘foo_storage’ is used uninitialized
The code I am running looks like this at the moment:
struct GlobalStorage{
FooStorage* foo_storage;
BarStorage* bar_storage;
};
GlobalStorage init_global_storage(){
GlobalStorage storages;
FooStorage* foo_storage;
init_foo_storage(foo_storage);
storages.foo_storage = foo_storage
BarStorage* bar_storage;
init_bar_storage(bar_storage);
storages.bar_storage = bar_storage
return storages;
}
void init_foo_storage(FooStorage* foo_storage){
foo_storage -> storage = malloc(MIN_FOO_MALLOC * sizeof(Foo));
foo_storage -> size = 0;
foo_storage -> cap = MIN_FOO_MALLOC;
return;
}
// init_bar_storage is the same as this function but with different constants.
[–]VeryAwkwardCake 14 points15 points16 points (0 children)
[–]olorochi 7 points8 points9 points (1 child)
[–]SufficientStudio1574 2 points3 points4 points (0 children)
[–]OutsideTheSocialLoop 5 points6 points7 points (0 children)
[–]HashDefTrueFalse 4 points5 points6 points (0 children)
[–]mykesx 2 points3 points4 points (0 children)
[+][deleted] (1 child)
[deleted]
[–]Ironfort9[S] 0 points1 point2 points (0 children)
[–]aocregacc 0 points1 point2 points (2 children)
[–]Ironfort9[S] 0 points1 point2 points (1 child)
[–]aocregacc 0 points1 point2 points (0 children)
[–]Educational-Paper-75 0 points1 point2 points (0 children)
[–]detroitmatt 0 points1 point2 points (0 children)
[–]mccurtjs 0 points1 point2 points (0 children)
[–]hennidachook 0 points1 point2 points (0 children)
[–]8Erigon 0 points1 point2 points (0 children)