How to overload operator== for static array?

Pages: 12
Getting data now.

1
2
3
4
5
6
7
8
memcpy_s(SimConnectVars::get_buffer_index(), (int)pObjData->dwDefineCount * 8, x, (int)pObjData->dwDefineCount * 8); // multiple to size of double

for (int i = 0; i < SimConnectVars::buffer_counter; i++) {   
                   //SimConnectVars::cvalue[i]= Encode(*(SimConnectVars::get_data_index() + i));
                   SimConnectVars::cvalue[i] = Encode(SimConnectVars::dvalue[i]);
                  
               }
          


SimConnectVars needed a method to return the pinned ptr.

1
2
3
4
static double* get_buffer_index() {
         pin_ptr<double> dindex = &dvalue[0];
         return dindex;
    }


Instead of index and cindex being pointers, they are now ints.

I hope pinned_ptr doesn't change while I am writing to it. Isn't it out of scope after it is returned by get_buffer_index()?

Thanks,
Chris
no, not if you do it right.
this is bad:
int *foo()
{
int x{42};
return &x; //x is a local variable that is destroyed when the function returns, so the pointer it returns is invalid. It sometimes works, unfortunately.
}

this is fine:
int *foo()
{
int* sigh = new int;
return sigh; //sigh is destroyed, it is a local variable. but what you returned is a copy of the address that sigh contained, which you allocated with new and did not destroy with delete, so that still exists and is valid. to bork it up you could return int ** and return the address of sigh, that is on par with the bad example above. It can take reading this a couple of times to understand what I just said. Do you?
}

in your case you return the address of something external to the function. That is ok too. The problem is an address for a local variable. And, it is exactly the same as returning a cast on the address: just say
return (pin_ptr<double>)(dvalue); //or &dvalue[0] if its a vector or something. whatever. or use c++ casting if you want to be pure.
Last edited on
Thanks a lot guys. Here is the final method for OpenFiledialog. There are no more text conversions. And I don't need the unordered set anymore or std::find for substring search.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
private: System::Void button4_Click(System::Object^ sender, System::EventArgs^ e) {
	String^ fileContent = gcnew String("");
	String^ filePath = gcnew String("");

	
	openFileDialog1->InitialDirectory = Environment::CurrentDirectory;

	openFileDialog1->Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
	openFileDialog1->FilterIndex = 2;
	openFileDialog1->RestoreDirectory = true;

	if (openFileDialog1->ShowDialog() == System::Windows::Forms::DialogResult::OK)
	{
		//Get the path of the user configuration file for SimVars
		filePath = openFileDialog1->FileName;
			
		String^ filePathSimVars = IO::Path::GetFullPath(Environment::CurrentDirectory+"/Data/SimVars.txt");
		System::IO::FileStream^ fsvars = gcnew System::IO::FileStream(filePathSimVars, System::IO::FileMode::Open, System::IO::FileAccess::Read);
		System::IO::StreamReader^ srvars = gcnew System::IO::StreamReader(fsvars, System::Text::Encoding::UTF8);
		String^ contentSimVars = srvars->ReadToEnd();

		String^ filePathSimUnits = IO::Path::GetFullPath(Environment::CurrentDirectory + "/Data/SimUnits.txt");
		System::IO::FileStream^ fsunits = gcnew System::IO::FileStream(filePathSimUnits, System::IO::FileMode::Open, System::IO::FileAccess::Read);
		System::IO::StreamReader^ srunits = gcnew System::IO::StreamReader(fsunits, System::Text::Encoding::UTF8);
		String^ contentSimUnits = srunits->ReadToEnd();
		
		cli::array<String^>^ aLines = System::IO::File::ReadAllLines(filePath);
		
		for (uint32_t i = 0; i < aLines->Length; ++i) {
			cli::array<String^>^ aSubStrings = aLines[i]->Split(',');
			
			if (aSubStrings->Length == 3) {
				bool first = false, second = false;

				int result = contentSimVars->IndexOf(aSubStrings[0], 0, contentSimVars->Length);
				if (result >= 0)  //Found
					first = true;
				result = contentSimUnits->IndexOf(aSubStrings[1], 0, contentSimUnits->Length);
				if (result >= 0) //Found
					second = true;

				int third = Convert::ToInt16(aSubStrings[2]);

				if ((first) && (second)) {
					
					SimRecs.push_back(gcnew SimConnectVars(aSubStrings[0], aSubStrings[1], third));	
				}
				else
				{

					// Initializes the variables to pass to the MessageBox.Show method.
					String^ message = "Wrong SimVar or Unit: Line ?" + i;
					String^ caption = "Error in SimVar file";
					MessageBoxButtons buttons = MessageBoxButtons(); //OK only

					// Displays the MessageBox.
					MessageBox::Show(message, caption, buttons);

				}


			}
			else if (aSubStrings->Length <3)  { //FIXME
					// Initializes the variables to pass to the MessageBox.Show method.
					String^ message = "Too few items in a line: " + i;
					String^ caption = "Error in SimVar file";
					MessageBoxButtons buttons = MessageBoxButtons(); //OK only

					// Displays the MessageBox.
					MessageBox::Show(message, caption, buttons);
			}

			
			

		}
		load_SimRecs();

		
		
	}

}


Thanks for pulling me out of the rabbit hole.

Chris
Last edited on
Registered users can post here. Sign in or register to post.
Pages: 12