1832925 Members
3011 Online
110048 Solutions
New Discussion

form() usage in USL.

 
Rashmi Jha
Occasional Contributor

form() usage in USL.

In our code, in quite a lot of places, USL's form() is used for formatting strings.
Since new aCC compilers ( -AA option ) doesn't support USL,
we had to write our own implementation for form().

Form's declartion is:
char * form(const char *,...);
and is same as used by USL.

With USL's form, there never used to be any memory leaks,
when used like
fp = fopen ( form ( "%s", fileName ) )
i.e the "char *" returned was never freed.
But still there were no memory leaks.

With our implementation, the same gives memory leaks.

What was the implementation of form() in USL, or is there any way in which we can avoid the memory leak( in the our implementation, memory is allocated in the heap, in form(), which needs to released once form() is called)
1 REPLY 1
Dennis Handly
Acclaimed Contributor

Re: form() usage in USL.

You do know that form() was already obsolete on the last version of cfront? I mentioned that in your other thread:
http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=144703

Ah, I had to do the same port recently in part of the IPF linker tools. Except the correct solution is to use sprintf(3) or snprintf(3).

>What was the implementation of form() in USL

It used a fixed size buffer that was used over and over. I believe it used a "clock" algorithm so that the buffer wasn't reused right away. That's why sprintf(3) was good enough.

>or is there any way in which we can avoid the memory leak (memory is allocated in the heap, in form(), which needs to released once form() is called)

Either you remember it like form(), or you require each caller to free the memory. Or force the caller to use ANSI C Standard stdio calls like sprintf(3).