[英]Why I this address is being changed after initialization in main function? (C)
I'm new to C and I'm facing some problems with a project I am working on.我是 C 的新手,我正在处理的项目遇到一些问题。
In vm.h在 vm.h
#define STACK_MAX 256
typedef double Value;
typedef struct {
Value values[STACK_MAX];
Value* top;
}
typedef struct {
Chunk* chunk;
uint8_t* ip; // Instruction pointer
Stack stack;
} VM;
VM initVm();
void resetStack(Stack* stack);
In vm.c在 vm.c
void resetStack(Stack* stack) {
stack->top = stack->values
}
VM initVM() {
VM vm;
resetStack(&vm.stack);
return VM;
}
In main.c在main.c
int main() {
VM vm = initVM();
...
}
Ok, here's the problem.好的,这就是问题所在。 After I initialize vm, vm.stack.top is pointing to vm.stack.values[0] (which I don't know why is filled with garbage values instead of zeroes), but one instruction later, no matter what, vm.stack.top is changed and starts pointing something else or sometimes the address of the vm.stack.values[0] is changed to another.
在我初始化 vm 之后,vm.stack.top 指向 vm.stack.values[0] (我不知道为什么用垃圾值而不是零填充),但是无论如何,一条指令之后,vm。 stack.top 已更改并开始指向其他内容,或者有时 vm.stack.values[0] 的地址更改为另一个。
The problem was VM was declared in the local scope.问题是在本地 scope 中声明了 VM。 Allocating VM in the heap solved it.
在堆中分配 VM 解决了它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.