Hi, I've written a rather simple(ish) stack implementation that would automatically grow its internal array buffer if needed.
For that, I'd naturally use realloc - It works, however, all array elements are ordered reverse after the realloc() call.
The code in question:
- Header: include/pd/stack.h
- Source: src/stack/stack.c
This example will trigger said behaviour:
#include "pd/strlib.h"
#include "pd/stack.h"
#include "pd/memory.h"
#include <stdlib.h>
#include <stdio.h>
int main()
{
int index = 0;
char* buffer;
pd_stack_t* stc = pd_stack_new();
pd_stack_push(stc, "blue");
pd_stack_push(stc, "green");
pd_stack_push(stc, "red");
pd_stack_push(stc, "yellow");
pd_stack_push(stc, "pink");
pd_stack_push(stc, "olive");
pd_stack_push(stc, "beige");
pd_stack_push(stc, "gold");
pd_stack_push(stc, "grey");
pd_stack_push(stc, "lime");
pd_stack_push(stc, "khaki");
while((index++) != 500)
{
pd_stack_push(stc, "__random_value__");
}
buffer = (char*)malloc(pd_stack_size(stc));
pd_stack_dump_tomem(stc, buffer, 1);
fprintf(stdout, "%s", buffer);
return 0;
}
I'm really clueless about this. Help please!