Hi there!.
Who is kind enough to give me an explanation about the differences between al_ustr_new_from_buffer() and al_ustr_new(). Obviously a technical explanation, since have just save me a lot of trouble, I was getting a lot of garbage from one of my char pointers when reading from file an using the al_ustr_new_from_buffer() function the problem have just disappeared... It's like eliminates the garbage automatically. But that garbage seems to be created by the al_fread function.
Thanks.
PS: I know I could search in the source, but I doubt I could understand something...
DAMN!! THE TITTLE! fuck!
al_ustr_new(s) is the same thing as al_ustr_new_from_buffer(s, strlen(s)).
Well... On my system they don't return the same result.
al_ustr_new() gives me a lot of garbage and al_ustr_new_from_buffer() works fine, this is the smaller example I could come up with. Probable I'm doing something wrong.
Check out the console, not the allegro window.
Well, as Matthew said, al_ustr_new uses strlen which computes the length of the string by looking for the first 0 character in that memory block. I'd guess that you don't write the trailing 0 when you save the .dat file, so strlen reports something much larger than what it should be.
Change it to:
char *buf = new char [len + 1]; buf[len] = 0;
You must null terminate each of your strings. (Or you can use al_ustr_new_from_buffer() for every case.)
That won't help though since he does this (confirming my guess):
And al_ustr_size doesn't include the trailing 0.
Just use the al_ustr_new_from_buffer and be done with it.
EDIT: Nvm, can't read.
That won't help though since he does this (confirming my guess):
I'm talking about when he loads the strings.
But I'd use al_ustr_new_from_buffer() since it exists for this purpose.
Hmm, I get it now, I didn't know that al_ustr_new() was looking for the null character, but otherwise how would know when to stop?.
Yhea the best choice is to keep using al_ustr_new_from_buffer() since I'm already saving a flag into the file, otherwise I could save the string with the null character included, and read the file until I find that null charter and then move on with the rest, and I wouldn't need the len flag.