13008

Stucture values not staying, values changed to -858993460

Question:

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?

<strong>Structure Defined with it's function(even though not it's not currently be used)</strong>

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); }

<strong>Attempting to transfer those values to another variable</strong>

bool Serial::Temperature(CString) { int Max,Min,SampleTime,Points; Max = Set1.Max; Min = Set1.Min; SampleTime = Set1.Sample; Points = Set1.Points; }

Answer1:

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; }

Answer2:

<blockquote>

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

</blockquote>

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.

Recommend

  • MySQL - Perl: How to get array of zip codes within submitted “x” miles of submitted “zipcode” in Per
  • MongoDB for BI use
  • XML - XSLT - Using two XML files - Additions to XML file consulting another XML file
  • I want to perform a multi-set intersection using C++
  • Can't stop animation fro cycling
  • How to assign same color to factors across plots in a nested loop for ggplot?
  • Compare two files and write to a new file but only output a few lines?
  • Resizing ToolStripButtons to fit complete BackGround image
  • Delphi: Form becomes Frozen while assigning strings in thread
  • javascript variables, What does var x = a = {} do?
  • how to solve invalid conversion specifier warning in iphone app
  • Android Studio Can't Find tools.jar
  • How to detect interior vertices in groups of 2d polygons? (E.g. ZIP Codes to determine a territory)
  • JqueryMobile Popup menu is not working
  • Reading a file into a multidimensional array
  • RxJava debounce by arbitrary value
  • Android application: how to use the camera and grab the image bytes?
  • D3 get axis values on zoom event
  • C: Incompatible pointer type initializing
  • Thread safety of a fluent like class using clone() and non final fields
  • WPF - CanExecute dosn't fire when raising Commands from a UserControl
  • How to clear text inside text field when radio button is select
  • Scrapy recursive link crawler
  • How reduce the height of an mschart by breaking up the y-axis
  • 'TypeError' while using NSGA2 to solve Multi-objective prob. from pyopt-sparse in OpenMDAO
  • Convert array of 8 bytes to signed long in C++
  • How to get next/previous record number?
  • Traverse Array and Display in markup
  • How get height of the a view with gone visibility and height defined as wrap_content in xml?
  • Understanding cpu registers
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • Linking SubReports Without LinkChild/LinkMaster
  • EntityFramework adding new object to nested object collection
  • Checking variable from a different class in C#
  • Recursive/Hierarchical Query Using Postgres
  • Running Map reduces the dimensions of the matrices
  • Sorting a 2D array using the second column C++
  • failed to connect to specific WiFi in android programmatically
  • java string with new operator and a literal
  • How can I use threading to 'tick' a timer to be accessed by other threads?