1

I've defined in my foo.h the following variables

#define APILONG          long
#define APIDOUBLE        double                   
#define APISTRING        const char*

And also the following struct

struct SetupData
{
  APISTRING customerName;
  APIDOUBLE quantity;
  APILONG startDate;

};

Now, in my foo.cpp i have the following method where I need to assign values to the members of the struct that are being pulled from a .config file.

APILONG doSomething(APISTRING* setupOrder, APILONG* setupActive, struct SetupData** setupData)
{
//load config file
Config config("rommel.config");

//assign a string value to APISTRING* (No issue here.)
*setupOrder= config.pString("setupOrder").c_str();

//assign a value (No issue here, atleast not that I know of..)
*setupActive = config.pDouble("setupActive");

//assign values to members of struct**
(*setupData)->customerName = config.pString("setupDataCustomerName").c_str();
(*setupData)->quantity = config.pDouble("setupDataQuantity");
(*setupData)->startDate = config.pDouble("setupDataStartDate");

//do other stuff..
}

When I compile and rebuild it does not give me any error messages. But when I try to run the actual program it crashes. (Am using Dev-C++, Visual Studio was causing problems..) I do however get a chance to see the values assigned before it crashes and it looks like the values are not being assigned (Null or strange characters).

I've tried variations of the (*setupData)->startDate .. line and also I have tried declaring the struct in the method like the following, but to no avail.

    struct SetupData stpData;
    *setupData = &stpData;

Any help would be greatly appreciated. I previously posted another question which is related to this, it contains some pretty useful information aswell. I'll leave the link in case it helps. "C++ Set value for a char**"

Community
  • 1
  • 1
David A.
  • 83
  • 1
  • 15
  • What does `config.pString` return? I bet it is a temporary `std::string`. – Dark Falcon Sep 23 '15 at 16:01
  • First, you need to correct your spelling of "ApiLongstocking". – Jerry Coffin Sep 23 '15 at 16:09
  • 2
    Why use `#define`? What does it add? Just makes code less readable – Ed Heal Sep 23 '15 at 16:16
  • @EdHeal I cant really modify it, this is a legacy code ive been given to work with. @DarkFalcon config.pString is defined in config.cpp as: `string Config::pString(string name) {..}` @JerryCoffin lol i cant find the typo.. – David A. Sep 23 '15 at 16:31

1 Answers1

0

The config object is local to the doSomething function.

The setupOrder pointer points to what appears to be a member of the config object.

The config object will go out of scope at the end of the doSomething function and can be de-allocated at that point.
At that point, what the setupOrder pointer "points" to might no longer be valid.

It is a good idea to make sure that after the call to doSomething ends, that all the pointers still point to objects that still exist.

You may have already done this, but I just wanted to check.

A B
  • 4,068
  • 1
  • 20
  • 23
  • Yes, well I've assigned values to other objects in other methods like I'm doing in this method without any problems, the problem is when i try to assign values to members of a struct** – David A. Sep 23 '15 at 16:53
  • @DavidA. - No, if A B's description is correct, the problem is that the code *seems to work* sometimes. The value of a `c_str()` is only valid as long as the `std::string` that supplied it is unchanged. If the string is modified or destroyed, you end up with a dangling pointer. – Bo Persson Sep 23 '15 at 17:19
  • @BoPersson I believe I understand what your saying, it's just that what throws me off is that this only happens with members of data structs. Any suggestions on how to work around this? Maybe assign values to other variables within the scope first and then assign them to the struct members? – David A. Sep 23 '15 at 19:54