开发者

Bizarre pointer error C++ MSVC6

开发者 https://www.devze.com 2023-02-15 10:02 出处:网络
This bug is killing me.I\'ve simplified the code greatly (these are virtual functions in a class, if that helps at all), but this is the jist of it.I pass a pointer and for whatever reason the calling

This bug is killing me. I've simplified the code greatly (these are virtual functions in a class, if that helps at all), but this is the jist of it. I pass a pointer and for whatever reason the calling function doesn't get the same value.

Thanks in advance.

bool getTable( int tableNum, DataTable* outputTable )  
{  
  // ... unrelated work ...
  outputTable = new DataTable();  
  cout << "outputTable= " << outputTable << endl;  
  return true;  
}  

bool storeTable( int tableNum )  
{  
  // ...
  DataTable* theTable;  
  bool isWorking = getTable( tableNum, theTable );  
  cout <<开发者_如何学编程 "theTable= " << theTable << endl;
  // ...  
  return isWorking;  
 }  

The output for this is:

outputTable= 01ED8C20  
theTable= CCCCCCCC

If I try to use theTable, I get an Access Violation Exception.

Thanks again.


Two things:

  1. You are passing the pointer by value. It is copied into the function but the change to the pointer is local. If you want to change the pointer you need to pass it by reference:

    bool getTable(int tableNum, DataTable*& outputTable)
    

    Better yet, return the newly created object and get rid of the (seemingly useless?) bool return value.

  2. Get rid of VS6. It is ancient, bug-ridden and essentially cannot cope with modern C++. In fact, it will force you to write bad C++ code because good, idiomatic C++ code requires features that don’t work in VC++6. Furthermore, the IDE is simply a decade behind modern usability standards.


You don't return outputTable from getTable. Instead, you return true. You assign the table you construct to a temporary variable which the caller never gets to see. Instead, its theTable variable remains unitialized.

You should either return it (the sensible option) or pass a pointer-to-pointer/reference-to-pointer (the hard option):

DataTable *getTable(int tableNum)  
{
    // ... unrelated work ...
    DataTable *outputTable = new DataTable();  
    cout << "outputTable= " << outputTable << endl;  
    return outputTable;
}

and return 0 (a null pointer) instead of false if something goes wrong. Be sure to check for that if it can occur. Throwing and catching an exception might be even better.


You're only new-ing a local copy of the pointer. Pass it as reference or pointer to pointer. E.g.:

bool getTable( int tableNum, DataTable*& outputTable )


bool getTable( int tableNum, DataTable* outputTable )  

outputTable is a pointer variable that exists only during the execution of this function.

Assigning a newly-created object to this pointer is basically just re-aiming the pointer at your new variable, it doesn't modify the memory that outputTable currently points to.

If you want to modify the 'outside world' in this function, you probably need to declare your function like this, and modify the code inside as appropriate.

bool getTable( int tableNum, DataTable** outputTable )

(Though C++ references are probably more idiomatic.)


C/C++ is pass by value, not reference.

You are passing the value of theTable into the function, and then updating it, that doesn't change the value in the calling function.

You need to do this:

getTable (tableNum, &theTable);

And in the getTable function you need to do:

*outputTable = new DataTable()

Alternatively you can change the getTable() function declaration to pass by reference:

bool getTable( int tableNum, DataTable* &outputTable )
0

精彩评论

暂无评论...
验证码 换一张
取 消