我一直在为 LeetCode 上的 Min Stack 编写代码。我遇到的问题是当我尝试重新分配内存(在推送方法上)时,它告诉我“地址清理程序:堆缓冲区溢出”。
这是什么原因造成的,我该如何解决这个问题?谢谢
另外,解决这个问题的更好方法是什么?
typedef struct {
int top;
int *arr;
int min;
int size;
} MinStack;
/** initialize your data structure here. */
MinStack* minStackCreate() {
MinStack* stack = (MinStack*)malloc(sizeof(MinStack));
stack->size = 10;
stack->top = -1;
stack->min = INT_MAX;
stack->arr = (int*) malloc(sizeof(int)*(stack->size));
return stack;
}
void minStackPush(MinStack* obj, int x) {
//if top+1 is equal to the size of the stack(when stack is full),
//I want to multiply the size by 2
//so more …Run Code Online (Sandbox Code Playgroud)