简体   繁体   English

C-传递结构成员的地址以返回错误的函数

[英]C - pass address of struct member to function wrong return

Answer : 答:

The culprit was the keyword restrict. 罪魁祸首是关键词restrict。 I missed a cast with it Thanks 我错过了一个演员

Context 语境

Linux. Linux。

Question

Could you help me find a classy solution to write my parameter ? 您能帮我找到一个经典的解决方案来写我的参数吗?

Why I fail 我为什么失败

I fail because I don't want to take the normal path (see code). 我失败了,因为我不想走正常的道路(请参见代码)。

My need 我的需要

Something quite classy like &(data->mmap) as a parameter to avoid creating a dummy variable like exposed in my way to circumvent the problem.. 像&(data-> mmap)这样非常经典的东西作为参数,可以避免创建虚拟变量,例如以我的方式公开以规避该问题。

This actually doesn't work. 这实际上是行不通的。

I have to do that way because that function may be called with different meanings. 我必须这样做,因为该函数可能具有不同的含义。 The struct is much more complex and according to my position in the code, the second parameter is not the same. 该结构要复杂得多,根据我在代码中的位置,第二个参数是不同的。

The code 编码

struct mystruct {
 char * mmap;
}
void test(mystruct * _input, char * my_char){

 int fd;
 ...
 // WORKS FINE. THE ADDRESS IS PRINTED IN mySecondFunction CORRECTLY.
 // THAT IS THE OBVIOUS WAY
 //_input->_mmap = (char*)mmap( 0 , 128 , PROT_READ | PROT_WRITE , MAP_SHARED , fd , 0 );

 // FAILS TO PRINT THE RIGHT ADDRESS IN mySecondFunction
  my_char = (char*)mmap( 0 , 128 , PROT_READ | PROT_WRITE , MAP_SHARED , fd , 0 );
 ...

 printf("in function address %p\n", my_char);

}
void mySecondFunction(mystruct * data){

 test(data, data->mmap);
 printf("data->mmap %p\n",data->mmap);

}
int main(void){

 mystruct test;

 mySecondFunction(&test);


 return 0;
}

My way to deal with this situation 我应对这种情况的方法

void test(mystruct * _input, char ** my_char){

   ...
   * my_char = (char*)mmap( 0 , 128 , PROT_READ | PROT_WRITE , MAP_SHARED , fd , 0 );
   ...
}
void mySecondFunction(mystruct * data){

 char * _tmp;
 // This is where i would like something like &data->mmap to replace &_tmp in order to get the right address directly.
 test(data, &_tmp);
 data->mmap = tmp;
 printf("data->mmap %p\n",data->mmap);

}

Thanks 谢谢

In test , you set the local my_char variable to the address you get from mmap . test ,将本地my_char变量设置为从mmap获取的地址。 This doesn't affect the _tmp variable in main . 这不会影响main_tmp变量。

If you want to change the variable you pass to the function you have to instead pass a pointer to it, like this: 如果要更改变量,则将其传递给函数,而必须传递一个指向它的指针,如下所示:

void test(mystruct * _input, char ** my_char){
   *my_char = (vhar*)mmap(...);
}

test(data, &data->mmap);

Here's a complete fix: 这是一个完整的修复程序:

void test(mystruct * _input, char ** my_char){
    //Update the reference that was passed
    *my_char = (char*)mmap( 0 , 128 , PROT_READ | PROT_WRITE , MAP_SHARED , fd , 0 );

    printf("in function address %p\n", my_char);
}

void mySecondFunction(mystruct * data){
    test(data, &(data->mmap));  // Pass by reference
    printf("data->mmap %p\n",&(data->mmap)); // Prints address of data->mmap
}

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

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