必要に応じて内部配列バッファーを自動的に拡張する、かなり単純な(っぽい)スタック実装を作成しました。
そのために、私は当然reallocを使用します-それは機能しますが、すべての配列要素はrealloc()呼び出しの後に逆の順序になります。
問題のコード:
この例では、次の動作がトリガーされます。
#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;
}
私はこれについて本当に無知です。助けてください!