Here’s the code:
// allocation
void allocateSymbolStorage(char **pepperShakerList, char **pepperList)
{
// allocate storage for an array of pointers
pepperShakerList = (char **) malloc(MAX_PEPPER_SHAKERS * sizeof(char *));
for (int i = 0; i < MAX_PEPPER_SHAKERS; i++)
{
if ((pepperShakerList[i] = (char *) malloc(MAX_SHAKERNAME_LENGTH * sizeof(char))) == NULL)
fatalError("failed pepperShakerList alloc");
}
// allocate storage for an array of pointers
pepperList = (char **) malloc(MAX_PEPPERS * sizeof(char *));
for (int i = 0; i < MAX_PEPPERS; i++)
{
if ((pepperList[i] = (char *) malloc(MAX_PEPPER_LENGTH * sizeof(char))) == NULL)
fatalError("failed pepperList alloc");
}
}
void buildPepperShakers(void)
{
char **pepperShakerList, **pepperList;
allocateSymbolStorage(pepperShakerList, pepperList);
// ....
freeSymbolStorage(pepperShakerList, pepperList);
}
Here’s the VS 2010 error:
: warning C4700: uninitialized local variable ‘pepperList’ used
Here’s the confusion:
Why the error if the char ** is being allocated in the allocate function? Is it a matter of the thing falling out of scope?
Assuming it’s pepperList and not symbolList that you are talking about, AND assuming that your code in the allocationSymbolStorage reflects what you want to do, then VC is complaining correctly.
As it stands your code would crash because in buildPepperShakers() you are NOT getting any values back from allocateSymbolStorage.
So your allocateSymbolStorage should be declared as:
THEN you pass the addresses of local pointer-holder variables in buildPepperShakers, namely
pepperListandpepperShakerListto the allocation function, so that it can THEN do allocations as per TJD’s answer. That is:of course your allocateSymbolStorage body now becomes:
and now VC should not complain. Although this is an ugly way of doing memory management of your objects 🙂