0

I'm clearly doing something wrong or forgetting something. I have defined a structure in a header file with four variables. I then assign values to those variables in a function located in a different .cpp then I try and take the new values from the structure and assign to different variable in another function. Problem is, I'm able to assign values to the variables in the structure but when I try and transfer those values to other variables they become something like -858993460(this is according to the debugger and watching the threads). How do I fix this?

Structure Defined with it's function(even though not it's not currently be used)

struct Setting {
    int Max;
    int Min;
    int Sample;
    int Points;
} Set1, Set2;

** Assigning Values to Structure variables**

void Settings::OnBnClickSet() {
    GetDlgItemText(ID_Points,str4);
    CW2A buf3((LPCWSTR)str4);
    Serial Value;
    Value.Set1.Points = atoi(buf3);
}

Attempting to transfer those values to another variable

bool Serial::Temperature(CString) {
    int Max,Min,SampleTime,Points;
    Max = Set1.Max;
    Min = Set1.Min;
    SampleTime = Set1.Sample;
    Points = Set1.Points;
}
andre
  • 7,018
  • 4
  • 43
  • 75
user1704863
  • 394
  • 1
  • 6
  • 19
  • 1
    You probably need to show us a more complete, but perhaps smaller example program of what you are doing. – Mats Petersson Feb 12 '13 at 20:49
  • 1
    What is a `Serial`? Can you reduce this to a http://sscce.org/? You're setting `Value.Set1.*` but attempting to get the data out of `Set1.*` (i.e. no `Value.`)? Is this your real code? – Chad Feb 12 '13 at 20:49
  • Serial is the main Class that the struct is defined at. – user1704863 Feb 12 '13 at 20:51
  • As in, `Setting` is defined within the enclosing scope of `Serial`? You see how leaving out some information makes the question confusing, and how it extends the amount of time until anyone can actually _help_? – Chad Feb 12 '13 at 20:53
  • The ReturnValues() function returns a struct but doesn't need to. It's writing values to a struct defined outside it's scope. Why return something that the caller already has direct access to? – Jay Feb 12 '13 at 20:54
  • I'm sorry if the codes confusing to look at what should I include to make it make more sense – user1704863 Feb 12 '13 at 20:57
  • 1
    Just read this link: http://sscce.org/ It can be extremely difficult to reduce a problem like this, especially if it's in a large program. However, usually the exercise of _trying_ to do so gives enough information that you find the source of the bug you were looking for in the first place. – Chad Feb 12 '13 at 20:59
  • 2
    The 32-bit two's-complement represenation of `-858993460` is `0xcccccccc`. – Keith Thompson Feb 12 '13 at 21:07
  • 1
    What's the relationship between `struct Setting` (no trailing 's') and `Settings`? Is one of them a typo? – Keith Thompson Feb 12 '13 at 21:08
  • [0xCCCCCCCC means uninitialized memory access](https://stackoverflow.com/q/370195/995714) – phuclv Aug 18 '18 at 11:14

2 Answers2

2

You're setting values on a local (automatic) variable. Those changes are local to the function in which the variable is declared (OnBnClickSet()).

If you want to use a single instance of Serial, you will need to either pass it in to the OnBnclickSet() function, or make it available via some other means (using a global variable or singleton for instance).

void Settings::OnBnClickSet() {
    GetDlgItemText(ID_Points,str4);
    CW2A buf3((LPCWSTR)str4);

    // This creates a Serial object with automatic storage duration
    Serial Value;

    // You're setting the value on the local (automatic) variable.
    Value.Set1.Points = atoi(buf3);

    // Value will now go out of scope, all changes to it were
    // local to this function
}


bool Serial::Temperature(CString) {
    int Max,Min,SampleTime,Points;

    // The member (?) variable Set1 was never modified on this instance of `Serial`
    Max = Set1.Max;
    Min = Set1.Min;
    SampleTime = Set1.Sample;
    Points = Set1.Points;
}
Chad
  • 18,706
  • 4
  • 46
  • 63
2

but when I try and transfer those values to other variables they become something like -858993460

That's a Magic Number. Convert it to Hex to get 0xcccccccc. Which is the value that's used to initialize variables when you build your program with the Debug configuration settings.

So whenever you see it back in the debugger, you go "Ah! I'm using an uninitialized variable!" All we can really see from the snippet is that your Set1 struct never got initialized. Focus on the code that's supposed to do this, with non-zero odds that you just forgot to write that code or are using the wrong structure object.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536