Re: need of a sprintf like function...



Thad Smith wrote:
ypjofficial@xxxxxxxxxxxxxx wrote:

if there any library function which can avoid allocating memory to str.
i.e i can write
char * str = NULL;
and directly pass str to that printf like function and the function
will internally handle the allocation of memory and releasing it.


Yes. The C99 function snprintf has a length parameter, which you may set to 0.

Setting the length parameter of snprintf to zero does not actually accomplish the task of allocating memory. It just allows you determine what length the string should have. You can then allocate a block of memory of the correct length, and call snprintf again to actually perform the conversion to string. See my other post in this thread.


--
Simon.
.



Relevant Pages

  • Re: HLA Lib
    ... All memory allocation is freed up when the process quits. ... reduce need to resize blocks for 98% string operations. ... HLA strings already consume. ...
    (alt.lang.asm)
  • Re: C++ vs Java "new" (no flame war please!)
    ... of such objects that would fit in available memory. ... memory and much more time than would the JVM for the equivalent Java ... The "allocation" ... If you have a small object with a string, ...
    (comp.lang.java)
  • Re: What is the difference between NULL string and empty string
    ... The space requirements are only the size of str. ... allocated to hold the emply string. ... might be interpreted as an error condition (failure in allocation the ...
    (comp.programming)
  • Re: alloca / _alloca / dynamic stack memory
    ... // Return Pointer to allocated memory ... but does not free memory until the ... // memory allocation method ... What does String have for data? ...
    (microsoft.public.vc.language)
  • Re: String Processing Basic Stuff
    ... Now I have to remove multiple spaces without using much memory ... undef $value; ... undef $str; ... or if you have slurped an 800 MB string in $_ ...
    (comp.lang.perl.misc)