简体   繁体   English

指向C中结构的指针

[英]pointer to a structure within a structure in C

I have a structure (let's call it structure1 ) which holds a pointer to another structure ( structure2 ), this way. 我有一个结构(我们称它为structure1 ),它以这种方式持有一个指向另一个结构( structure2 )的指针。

typedef struct structure{
    structure2 *pointer
}structure;

structure structVariable;
structVariable.pointer = functionThatReturnsAPointerToStructure2Variable();

The thing is, as the program changes context (for example, when calling functions), the return value of the following code changes 问题是,随着程序更改上下文(例如,在调用函数时),以下代码的返回值也会更改

structVariable.pointer->someAttribute

Any idea of why this might be happening? 知道为什么会这样吗? If you need more info please ask. 如果您需要更多信息,请询问。 Thanks! 谢谢!

MORE INFO 更多信息

This is the real-deal 这是实打实的

structure would be this 结构就是这个

typedef struct CPU{
    int variableCounter;
    int instructionPointer;
    char *currentInstruction;
    t_list *dataDictionary_list;
    u_int32_t currentContext;
    PCB *assignedPCB;
    CPU_STATUS status;
}CPU;

And this is how I assign the pointer (PCB *pointer) 这就是我分配指针(PCB *指针)的方式

PCB *pcb_createFromScript(const char *script){
    t_medatada_program *metadata = metadatada_desde_literal(script);
    PCB *pcb = malloc(sizeof(PCB));

pcb->instructionCount = metadata->instrucciones_size;
pcb->tagCount = metadata->cantidad_de_etiquetas;
pcb->functionCount = metadata->cantidad_de_funciones;

int codeSegmentSize = strlen(script);
int tagIndexSize = 0;

if(metadata->etiquetas != 0){
    tagIndexSize = strlen(metadata->etiquetas);
}

int instructionIndexSize = metadata->instrucciones_size * sizeof(t_intructions);

pcb_getSegments(pcb,1024,codeSegmentSize,tagIndexSize,instructionIndexSize);

pcb->currentContext = pcb->stackSegment;

pcb->variableCounter = 0;

memory_write(pcb->codeSegment,0,codeSegmentSize,script);
memory_write(pcb->tagIndexSegment,0,tagIndexSize,metadata->etiquetas);
memory_write(pcb->instructionIndexSegment,0,instructionIndexSize,(void *)metadata->instrucciones_serializado);

pcb->uniqueId = (int) random();
return pcb;

} }

And then I assign it this way (myCPU is global), that's why I call it inside cpu_getPCB without passing it as a parameter 然后我以这种方式分配它(myCPU是全局的),这就是为什么我在cpu_getPCB调用它而不将其作为参数传递的原因

cpu_getPCB(*dummyPCB);

void cpu_getPCB(PCB myPCB){
    myCPU.currentContext = myPCB.currentContext;
    myCPU.assignedPCB = &myPCB;
}

Here is some speculation. 这是一些猜测。

If you are modifying the object that structVariable.pointer points to in some function, then when you try to read structVariable.pointer->someAttribute , that value will change to reflect to modification to the object. 如果您正在某个函数中修改structVariable.pointer指向的对象,那么当您尝试读取structVariable.pointer->someAttribute ,该值将更改以反映对该对象的修改。

Another possibility, as the other answer mentioned, is that structVariable.pointer is pointing to local memory (stack memory for a function) which can easily be overwritten on a new function call. 正如其他答案所述,另一种可能性是structVariable.pointer指向本地内存(函数的堆栈内存),该内存可以在新的函数调用上轻松覆盖。 That can be corrected by using malloc to do heap allocation instead of stack allocation. 这可以通过使用malloc进行堆分配而不是堆栈分配来纠正。


Here is the first and most obvious issue. 这是第一个也是最明显的问题。 You are taking the address of a parameter and assigning it to myCPU.assignedPCB . 您正在获取参数的地址并将其分配给myCPU.assignedPCB

Since C is pass-by-value, you have copied it instead of capturing the original. 由于C是传递值,因此您已复制它而不是捕获原始值。 Moreover, the parameter has the same lifetime as a local variable, and will go away when the function returns. 而且,该参数与局部变量具有相同的生存期,并且在函数返回时将消失。

void cpu_getPCB(PCB myPCB){
    myCPU.currentContext = myPCB.currentContext;
    myCPU.assignedPCB = &myPCB;
}

You can fix it by passing a pointer instead, since you are in C and do not have access to the reference type. 您可以通过传递指针来解决它,因为您使用的是C ,因此无法访问引用类型。

void cpu_getPCB(PCB* myPCB){
    myCPU.currentContext = myPCB->currentContext;
    myCPU.assignedPCB = myPCB;
}

The "structure2 *pointer" will be pointing at a piece of memory that will disappear when you change context. “ structure2 * pointer”将指向一块内存,当您更改上下文时,该内存将消失。 Allocate the Structure2 variable and free it when it's no longer needed 分配Structure2变量并在不再需要它时将其释放

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM