C指针行为不明白

时间:2017-09-07 15:51:07

标签: c pointers arguments

我有一个简单的结构,loc

typedef struct
{
  long blk;
  int offset;
} loc;

在函数avl_isnadd中,它以:

传递
int
avl_isnadd (old_loc, old_isn, isn)
loc *old_loc;
int old_isn, isn;
  {
    int next_isn;
    loc *this_loc;
    printf("\n{avl_isnadd} - old_loc-> blk = %d, old_loc->offset = %d\n", old_loc->blk, old_loc->offset);
    this_loc->blk = old_loc->blk;
    this_loc->offset = old_loc->offset;
    printf("\n{avl_isnadd} - this_loc->blk = %d, this_loc->offset = %d\n", this_loc->blk, this_loc->offset);
     next_isn = avl_isnget (this_loc);
     return next_isn;
}

在avl_isnget中,我们有:

int
avl_isnget (myLoc)
loc *myLoc;
  {
    printf("\n{avl_isnget} - MyLoc->blk = %d, myLoc->offset = %d\n", myLoc->blk, myLoc->offset);
    return 0;
   }

控制台上的结果是:

{avl_isnadd} - old_loc-> blk = 1, old_loc->offset = 512

{avl_isnadd} - this_loc->blk = 1, this_loc->offset = 512


{avl_isnget} - MyLoc->blk = 1485457792, myLoc->offset = 512

我在这里缺少什么?我不明白为什么avl_isnget应该有 myLoc-> blk

的值不同

2 个答案:

答案 0 :(得分:0)

this_loc是一个指向无效内存地址的指针。事实上,这个程序应该崩溃。

this_loc = old_loc也应该有用。

答案 1 :(得分:0)

您没有为this_loc分配任何空间,因此只要您this_loc->blk = old_loc->blk;重新调用undefined behavior即可。您可以在自动存储中或从堆中为this_loc分配空间。我将演示自动存储选项,因为我发现考虑到所提供的代码(使用更新的语法)更好:

自动存储选项:

int
avl_isnadd (loc *old_loc, int old_isn, int isn)
{
    int next_isn;
    loc this_loc; // don't make it a pointer. this declaration will allocate space for
                  // the struct in automatic storage (in many implementations,
                  // the stack) whose scope exists only in this function
    printf("\n{avl_isnadd} - old_loc-> blk = %ld, old_loc->offset = %d\n", old_loc->blk, old_loc->offset);  // printf uses the %ld specifier for a signed long 
    this_loc.blk = old_loc->blk;  // change the accessor operator from -> to .
    this_loc.offset = old_loc->offset;
    printf("\n{avl_isnadd} - this_loc.blk = %ld, this_loc.offset = %d\n", this_loc.blk, this_loc.offset);
    next_isn = avl_isnget (this_loc); // Simply pass the entire struct to the avl_isnget function.
    return next_isn;
    // this_loc goes out of scope (pops off the stack) and you're done with it
}

然后将avl_isnget功能更改为

int
avl_isnget (loc myLoc)
{
    // myLoc is now a local copy of the this_loc struct that you passed in.
    // Since this function doesn't modify the struct loc passed in, there's
    //no real point in passing in a pointer. A local copy will do just fine for printing
    printf("\n{avl_isnget} - MyLoc.blk = %ld, myLoc.offset = %d\n", myLoc.blk, myLoc.offset);
    return 0;
}

另一种选择是将空间分配为loc* this_loc = malloc(sizeof(loc));并从那里开始,但是在提供代码的情况下,没有理由这样做。除非您有good reason to do so

,否则请避免使用内存管理