C++ character overflow help!!

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jan 2009
Posts: 4
Reputation: pacx is an unknown quantity at this point 
Solved Threads: 0
pacx pacx is offline Offline
Newbie Poster

C++ character overflow help!!

 
0
  #1
Jan 21st, 2009
Hi
I am using Visual 6 C++ and try to retrieve a member's all closed trades. i declare a string as below:

char str[8000]={0};

But this declaration is ok and the program work fine if the member's record size does not exceed 8000bytes.


In order to be able to retrieve member's record where total size more than 8000, i have declare as below:

char *str = new char();

This declaration works fine but it hang my server. Whenever i execute this API with this declaration, my MT4 server will hang, and its service cannot restarted. We have to restart the server.

Is there any way or any example where i can return big volume data?


Thanks.


My main code to retrieve data as below:
  1. trades=ExtServer->OrdersGetClosed(start, end,userlogins,1, &total);
  2.  
  3. //OrdersGetClosed(const time_t from,const time_t to,const int *logins,const int count,int* total);
  4.  
  5. // check orders
  6. for(j=0; j<total; j++)
  7. {
  8. if(strcmp(GetCmd(trades[j].cmd), "credit")!=0 && strcmp(GetCmd(trades[j].cmd), "balance")!=0)
  9. {
  10. sprintf(tmp,"%d",trades[j].order);
  11. strcat(str,tmp);
  12. strcat(str,",");
  13. sprintf(tmp,"%d",trades[j].login);
  14. strcat(str,tmp);
  15. strcat(str,",");
  16. FormatDateTime(trades[j].open_time,tmp,sizeof(tmp)-1,TRUE,TRUE);
  17. strcat(str,tmp);
  18. strcat(str,",");
  19. strcat(str, GetCmd(trades[j].cmd));
  20. strcat(str,",");
  21. COPY_STR(tmp, trades[j].symbol);
  22. _strlwr(tmp);
  23. strcat(str, tmp);
  24. strcat(str,",");
  25. sprintf(tmp,"%.2lf",trades[j].volume/100.0);
  26. strcat(str,tmp);
  27. strcat(str,",");
  28. ToSymExt(tmp, trades[j].open_price, trades[j].digits);
  29. strcat(str, tmp);
  30. strcat(str,",");
  31. FormatDateTime(trades[j].close_time,tmp,sizeof(tmp)-1,TRUE,TRUE);
  32. strcat(str,tmp);
  33. strcat(str,",");
  34. ToSym(tmp, trades[j].close_price, trades[j].digits);
  35. strcat(str, tmp);
  36. strcat(str,",");
  37.  
  38. strcat(str, ToMoney(trades[j].commission, 2, tmp, sizeof(tmp)-1));
  39. strcat(str,",");
  40. strcat(str, ToMoney(trades[j].taxes, 2, tmp, sizeof(tmp)-1));
  41. strcat(str,",");
  42. strcat(str, ToMoney(trades[j].commission_agent, 2, tmp, sizeof(tmp)-1));
  43. strcat(str,",");
  44. strcat(str, ToMoney(trades[j].storage, 2, tmp, sizeof(tmp)-1));
  45. strcat(str,",");
  46. strcat(str, ToMoney(trades[j].profit, 2, tmp, sizeof(tmp)-1));
  47. strcat(str,",");
  48. mul=Decimals(trades[j].digits);
  49. if(trades[j].cmd==OP_BUY)
  50. pips=NormalizeDouble(trades[j].close_price*mul,0)-NormalizeDouble(trades[j].open_price *mul,0);
  51. else
  52. pips=NormalizeDouble(trades[j].open_price *mul,0)-NormalizeDouble(trades[j].close_price*mul,0);
  53.  
  54. sprintf(tmp,"%d", int(pips));
  55. strcat(str, tmp);
  56. strcat(str,",");
  57. strcat(str, trades[j].comment);
  58.  
  59. strcat(str,"\n");
  60. }
  61.  
  62. }
  63. strcat(str,"end\r\n");
  64. //---- clear
  65. HEAP_FREE(trades); trades=NULL;
  66. if(strlen(str)==0)
  67. return _snprintf(buffer,size-1,"ERROR\r\nNo Orders for %d\r\nend\r\n", userlogin);
  68. else
  69. return _snprintf(buffer,size-1,"%s", str);


i am using native dll and cannot use MFC

any advise would be appreciated thanks
Last edited by Ancient Dragon; Jan 22nd, 2009 at 12:10 am. Reason: add code tags
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,678
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1504
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: C++ character overflow help!!

 
0
  #2
Jan 22nd, 2009
>>char *str = new char();

Do you really mean something like [icode]char *str = new char(Size);[/code] so that more than 1 character is allocated???

Can't really tell much from the code you posted because there are several undefined variables -- such as temp.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 4
Reputation: pacx is an unknown quantity at this point 
Solved Threads: 0
pacx pacx is offline Offline
Newbie Poster

Re: C++ character overflow help!!

 
0
  #3
Jan 22nd, 2009
hi

i am currently using
char str[8000]={0};

this program actually retrieve some data via api and i am suppose to return and 'massage' the values in the api via the variable str

the problem with this code is that when i try to return str that exceed 8000 characters, the program will hung...
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 640
Reputation: Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough 
Solved Threads: 104
Murtan Murtan is offline Offline
Practically a Master Poster

Re: C++ character overflow help!!

 
0
  #4
Jan 22nd, 2009
Do you need to return the whole result in a character buffer?

Could you return a vector of strings with one string per trade?
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 4
Reputation: pacx is an unknown quantity at this point 
Solved Threads: 0
pacx pacx is offline Offline
Newbie Poster

Re: C++ character overflow help!!

 
0
  #5
Jan 22nd, 2009
hi


my application allow only native dlls.. i cant seem to use string to solve my problem..
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 640
Reputation: Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough 
Solved Threads: 104
Murtan Murtan is offline Offline
Practically a Master Poster

Re: C++ character overflow help!!

 
0
  #6
Jan 22nd, 2009
Can you use vector?

If not, you have a little more work cut out for you, but it can still be done. (I am presuming you have a heap and can allocate memory.)

I can fairly easily imagine a class where you can 'add' a C style string to it and it would keep an array of the strings. When you call the 'add' it could allocate enough space to hold the string and copy it in. It would then store the pointer to the new string in an array of character pointers. If you ran out of entries in the array of pointers, you could allocate a new array of pointers (some number of pointers bigger -- think 50 or 100) and copy the old pointers to it and release the old array.

This would provide functionality similar to vector<string> without using "non-native" code.

Once you have assembled all of the trade data, you would return this new object and the calling code could iterate the strings in the object to access all of the compiled data.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 40
Reputation: seanhunt is an unknown quantity at this point 
Solved Threads: 6
seanhunt seanhunt is offline Offline
Light Poster

Re: C++ character overflow help!!

 
0
  #7
Jan 22nd, 2009
Originally Posted by pacx View Post
hi


my application allow only native dlls.. i cant seem to use string to solve my problem..
if for some reason you cant use stl, you coulc have buffers that hold up to a set amount;in other words keep a large buffer aside that holds, say 4000 characters, as soon as it is full use the next 4000, and switch back and forth. Aside from having more thant 8000 characters at your disposal, anything more than that has to go somewhere if you are at a hard limit.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 4
Reputation: pacx is an unknown quantity at this point 
Solved Threads: 0
pacx pacx is offline Offline
Newbie Poster

Re: C++ character overflow help!!

 
0
  #8
Jan 22nd, 2009
any sample codes i can take a look ?
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 578 | Replies: 7
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC