[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Char array overwriting values C++

Posted on 2009-12-21
13
Medium Priority
?
824 Views
Last Modified: 2013-12-14
Im writing an application that uses a char* array. its formed using the following struct:

struct MqlStr // MQL String Array
  {
   int               len;
   char             *string;
  };

this array is passed into a function from an external function, which is also where it originates.

the DLL im writing gets data from the server then assigns it to parts of the array. the problem i have is that the data is being over written.

for example, i get one value normally a long one and put into arr1[20].string using strcpy.

i then repeat with new data into arr1[30].string and arr1[40].string.

what i find is the value i put into arr1[40].string is now in all three, 40,30,and 20.

any ideas?
0
Comment
Question by:GreatSamps
  • 6
  • 6
13 Comments
 
LVL 17

Assisted Solution

by:CSecurity
CSecurity earned 2000 total points
ID: 26096967
Do you allocate enough space for your destination char array?

For making sure, create all of arrays with size of MAX_PATH, see if it again gets overwritten..

Like char string[MAX_PATH] and same for all other arrays... See how it works...
0
 

Author Comment

by:GreatSamps
ID: 26097161
The array is actually created in another program, that not C++ so to create an array there, i simply use String Arr1[40], 40 being the number of elements it holds.

it is possible to specify the size of it in the C++ part, as i dont think its possible in the other program (MetaTrader)
0
 
LVL 17

Expert Comment

by:CSecurity
ID: 26097231
So in C++ allocate larger space and give it a try
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:GreatSamps
ID: 26097379
so would i do that like this:

struct MqlStr // MQL String Array
  {
   int               len;
   char[MAX_PATH]             *string;
  };
0
 
LVL 17

Expert Comment

by:CSecurity
ID: 26097579
no

struct MqlStr // MQL String Array
  {
   int               len;
   char             string[MAX_PATH];
  };
0
 

Author Comment

by:GreatSamps
ID: 26097625
ok it does not like that, the values have all gone crazy ...
0
 
LVL 17

Expert Comment

by:CSecurity
ID: 26097666
Could you show more code and exactly location of crash?
0
 

Author Comment

by:GreatSamps
ID: 26097890
i dont have a problem in principal, but the DLL its self is fairly big, what makes it worse is that the values that are being passed in and out are coming from either MQ4 or ASP.NET / SQL. this dll on its own would not be enough to work out whats going where. i will post the function where the values are being overiden.


string	value,
			input,
			ver;
	
	size_t	pos,
			pos1;
	
	input = app_id;
	input += ",";
	input += arr1[20].string;

	ver = XPostHttpsSendExt("https://..../getneworders.aspx", input.c_str());
		
	pos = ver.find_first_of(",",0); // get the trade ID
	value = ver.substr(0, pos);
	strcpy(arr1[31].string, value.c_str());
	pos1 = pos;
	
	pos = ver.find_first_of(",",pos1+1); // get the open time
	value = ver.substr(pos1+1, (pos-pos1-1));
	//std::stringstream ss_otm (value);
	//ss_otm >> arr2[32];
	pos1 = pos;

	pos = ver.find_first_of(",",pos1+1); // get the order type
	value = ver.substr(pos1+1, (pos-pos1-1));
	std::stringstream ss_ot (value);
	ss_ot >> arr2[33];
	pos1 = pos;

	pos = ver.find_first_of(",",pos1+1); // get the order symbol
	value = ver.substr(pos1+1, (pos-pos1-1));
	strcpy(arr1[34].string, value.c_str());
	pos1 = pos;

	pos = ver.find_first_of(",",pos1+1); // get the order size
	value = ver.substr(pos1+1, (pos-pos1-1));
	std::stringstream ss_os (value);
	ss_os >> arr2[35];
	pos1 = pos;

	pos = ver.find_first_of(",",pos1+1); // get the open price
	value = ver.substr(pos1+1, (pos-pos1-1));
	std::stringstream ss_op (value);
	ss_op >> arr2[36];
	pos1 = pos;

	pos = ver.find_first_of(",",pos1+1); // get the order revision
	value = ver.substr(pos1+1, (pos-pos1-1));
	std::stringstream ss_or (value);
	ss_or >> arr2[37];
	pos1 = pos;
	myfile << "\n";
	myfile << arr2[33];
	myfile.close();

	if(arr2[33] == -1) return(0);
	if(arr2[33] == 0) return(2);
	if(arr2[33] == 1) return(1);

Open in new window

0
 
LVL 17

Expert Comment

by:CSecurity
ID: 26098603
Where is source of problem? I mean when you do the MAX_PATH to variable in struct, what errors you get?
0
 

Author Comment

by:GreatSamps
ID: 26099258
the error is that a value i have set an array element is altered.

for example i set arr1[2] = "test123" in the metaquotes, i then print it to file in the DLL and it has been modified to lots of alphanumeric characers ... similar to it being encoded or encrypted
0
 
LVL 17

Expert Comment

by:CSecurity
ID: 26102399
This is not visual basic or delphi, it's C++. You can't do

arr1[2] = "test123";

you should do strcpy(arr1, "test123");

You should have enough space when calling and passing variable to MT4, see one example DLL has been created for MT4 and learn from it, which API you are going to use? Is it DLL injection or calling some functions of MT4?
0
 

Accepted Solution

by:
GreatSamps earned 0 total points
ID: 26102828
arr1[2] = "test123"; is from within MT4 which is perfectly acceptable.

i dont think this can be answered here, i am speaking with metaquotes to see if they have any idea. the application ive written was based around a working example, all i  am doing is increasing the field length.

look like i will need to opt for the work around.
0
 
LVL 40

Expert Comment

by:evilrix
ID: 26102921
>> for example, i get one value normally a long one and put into arr1[20].string using strcpy.>> i then repeat with new data into arr1[30].string and arr1[40].string.>> what i find is the value i put into arr1[40].string is now in all three, 40,30,and 20.Was this just a typo in the question? Assuming you are trying to index the string did you mean to do this instead?arr1.string[N] where N is your index position?e.g  strcpy(arr1.string[10], &quot;example");
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
Suggested Courses

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question