- Function Name: expandStack
- Input: a pointer to a Stack type (Stack*)
- Output: none
- Function Operation: The function expands a stack
void expandStack(Stack* stack){
//Check the stack and the array are allocated
if (stack == NULL ||stack->content == NULL)
{
return;
}
//Allocating a new sized array (*2 from the previous)
Element* expandedStack = (Element*)malloc(2 * (stack->size) * sizeof(Element));
//Case malloc failed
if (expandedStack == NULL)
{
printf("Error! Malloc has failed in file 'stack.c', 'expandStack' function\n");
return;
}
//Updating size field
stack->size *= 2;
//Copy values from the previous array to the new array allocated
for (int i = 0; i <= stack->topIndex; i++)
{
expandedStack[i].c = stack->content[i].c;
}
//Free old array
free(stack->content);
//Point to the new array in the heap
stack->content = expandedStack;
}
In this line: expandedStack[i].c = stack->content[i].c; I get a "green warning" saying: "c6386 buffer overrun while writing to 'expandedStack': The writeable size is '2 * (stack->size) * sizeof(Element)' bytes, but '2' bytes might be written.
The thing is that the code works fine, it compiles.