|
Questions regarding al_ustr_free |
Bob Keane
Member #7,342
June 2006
|
I've solved the problem but was wondering what was going on. I passed a struct by address to a function in C. The function took some previously input data to calculate between two and four points. Then a menu was drawn to select one of the points. A case statement was used to make a selection. In each case, the selected option was assigned to the corresponding field in the struct and other calculations are made. At the end of the function, outside my input loops and just before my return statement, I used al_ustr_free to clear the four strings. When checking the updated struct field in main() after, I found the field was blank or null. To solve this, I moved the appropriate al_ustr_free statements to the case statements after I assigned the data to the struct. I still have a ustr unfreed after the function ends. I was wondering why the struct field was null even though I assigned a value to it before I freed the corresponding ustr? By reading this sig, I, the reader, agree to render my soul to Bob Keane. I, the reader, understand this is a legally binding contract and freely render my soul. |
torhu
Member #2,727
September 2002
|
It's not clear to me exactly what you are doing, but you are not supposed to access string after freeing it. |
Bob Keane
Member #7,342
June 2006
|
In a nutshell, I assigned the ustr to a variable in a struct then freed it. I then put a simple print statement in main to print the variable from the struct, but when it printed, it was null. I thought assigning the ustr to the struct before freeing it would save the variable in the struct when the function passed but it did not. By reading this sig, I, the reader, agree to render my soul to Bob Keane. I, the reader, understand this is a legally binding contract and freely render my soul. |
torhu
Member #2,727
September 2002
|
No, because what you have is a reference to something. If you free it, you have a reference to nothing instead. |
Bob Keane
Member #7,342
June 2006
|
That explains it. Thanks. By reading this sig, I, the reader, agree to render my soul to Bob Keane. I, the reader, understand this is a legally binding contract and freely render my soul. |
Edgar Reynaldo
Major Reynaldo
May 2007
|
Assigning to a struct is fine, and if you have the address of the struct, you can alter it directly. The problem is the dangling pointer you create by deleting it. The value now points to a dead object. EDIT My Website! | EAGLE GUI Library Demos | My Deviant Art Gallery | Spiraloid Preview | A4 FontMaker | Skyline! (Missile Defense) Eagle and Allegro 5 binaries | Older Allegro 4 and 5 binaries | Allegro 5 compile guide |
|