Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Questions regarding al_ustr_free

Credits go to torhu for helping out!
This thread is locked; no one can reply to it. rss feed Print
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.
"Love thy neighbor as much as you love yourself means be nice to the people next door. Everyone else can go to hell. Missy Cooper.
The advantage to learning something on your own is that there is no one there to tell you something can't be done.

torhu
Member #2,727
September 2002
avatar

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.
"Love thy neighbor as much as you love yourself means be nice to the people next door. Everyone else can go to hell. Missy Cooper.
The advantage to learning something on your own is that there is no one there to tell you something can't be done.

torhu
Member #2,727
September 2002
avatar

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.
"Love thy neighbor as much as you love yourself means be nice to the people next door. Everyone else can go to hell. Missy Cooper.
The advantage to learning something on your own is that there is no one there to tell you something can't be done.

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

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
I've seen your code now, and you need to allocate once and destroy once and in one stack frame only to keep things simple.

Go to: