Gamestudio Links
Zorro Links
Newest Posts
Blobsculptor tools and objects download here
by NeoDumont. 03/28/24 03:01
Issue with Multi-Core WFO Training
by aliswee. 03/24/24 20:20
Why Zorro supports up to 72 cores?
by Edgar_Herrera. 03/23/24 21:41
Zorro Trader GPT
by TipmyPip. 03/06/24 09:27
VSCode instead of SED
by 3run. 03/01/24 19:06
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
1 registered members (degenerate_762), 1,114 guests, and 1 spider.
Key: Admin, Global Mod, Mod
Newest Members
sakolin, rajesh7827, juergen_wue, NITRO_FOREVER, jack0roses
19043 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Difference between Asset and char* #486689
09/18/22 12:36
09/18/22 12:36
Joined: Jan 2022
Posts: 57
N
NorbertSz Offline OP
Junior Member
NorbertSz  Offline OP
Junior Member
N

Joined: Jan 2022
Posts: 57
Hello,

I am not sure how can I make a string / char array that can behave like the Asset variable.

I have this structure to store the last X prices of all the timeframes of all the assets in the global namespace:
Code
map<char*, map<int, deque<double>>> prices;

(FYI std::deque is like std::vector, but it is optimized for pushing elements also to the front.)
So I can store all the prices like this:
Code
TimeFrame = 5;
if (frame(0)){
   prices[Asset][TimeFrame].push_front(priceClose(0));
   // ... maintain the length of the deque
}


Filling up with data is done. But when I want to read it I have a problem.

I have 3 assets, and in the asset loop there is this sequence:
Code
for (pair<char*, map<int, deque<double>>> element: prices){
   printf("\n -- %s", element.first);
}

Result:
Code
 -- USDJPY
 -- EURUSD
 -- XAUUSD


The following code is also ok, prints the last price of the current asset:
Code
printf("%s--- %.5f", Asset, prices[Asset][5][0]);


But this two following goes into runtime crash:

Code
//v1 crash
printf("%s--- %.5f", "EURUSD", prices["EURUSD"][5][0]);

//v2 crash
char* asd = "EURUSD";
printf("%s--- %.5f", asd, prices[asd][5][0]);


I got the runtime crash because these deques haven't got element 0.
This code...:
Code
printf("\n--- size Asset: %d, size EURUSD: %d", prices[Asset][5].size(), prices["EURUSD"][5].size());
printf("\n diff: %d", strcmp(Asset, "EURUSD"));
...prints the following:
Code
---size Asset: 800, size EURUSD: 0
 diff: 0

So my question is, what is the difference between Asset / char* var / string literal? The content of Asset is "EURUSD", and comparing to literal "EURUSD" returns 0, but anyways they are different somehow. How can I make a variable or literal "act like" an Asset, to use that prices map I described before?

Last edited by NorbertSz; 09/18/22 12:52.
Re: Difference between Asset and char* [Re: NorbertSz] #486690
09/18/22 16:28
09/18/22 16:28
Joined: Feb 2017
Posts: 1,718
Chicago
AndrewAMD Offline
Serious User
AndrewAMD  Offline
Serious User

Joined: Feb 2017
Posts: 1,718
Chicago
Do not use pointer addresses in a map key because they will sort by address. The chances of an error are approximately 100%. Use std::string instead, which will compare string content.

Re: Difference between Asset and char* [Re: NorbertSz] #486691
09/18/22 16:41
09/18/22 16:41
Joined: Aug 2022
Posts: 65
alun Offline
Junior Member
alun  Offline
Junior Member

Joined: Aug 2022
Posts: 65
Hi NorbertSz,

you'll need to learn more about C++ string literals and pointers.

char* is a pointer so it has an address in memory.

Two different locations in memory might have the value but different addresses.

This is what happening with your code char* Asset has one address and the literal string "EURUSD" has another address, they are treated as different keys when used with the map.

E.g check this
Code
  printf("\n%s %s", Asset, "EURUSD");
  printf("\n%p %p", Asset, "EURUSD");


You probably need to cast them to std::string and use it as a key in your map.

e.g.
Code
std::string key1(Asset);
std::string key2("EURUSD");

map<string, map<int, deque<double>>> prices;
prices[key1] = ...;
... = prices[key2];


C++ also converts char* to string automatically. So this code works the same way:

Code
prices[Asset] = ...;
... = prices["EURUSD"];


example

P.S. concurrency issue with Andrew above laugh

Last edited by alun; 09/18/22 16:56.
Re: Difference between Asset and char* [Re: NorbertSz] #486696
09/19/22 10:48
09/19/22 10:48
Joined: Jan 2022
Posts: 57
N
NorbertSz Offline OP
Junior Member
NorbertSz  Offline OP
Junior Member
N

Joined: Jan 2022
Posts: 57
I slipped through the char* - I thought it will compare by characters, but you are absolutely right.
The string has custom comparator, so the string map key is the solution:
Code
map<std::string, map<int, deque<double>>> prices;

Thank you AndrewAMD and alun!

Last edited by NorbertSz; 09/19/22 10:50.

Moderated by  Petra 

Powered by UBB.threads™ PHP Forum Software 7.7.1