Re: String questions





gamehack wrote On 02/14/06 14:55,:
Hi all,

I've been designing a program which would need to have a function to
replace values in a string.
For example I could have a string:
"<circle x="%" y="%">" and then I would replace the % with actual
values. But I cannot think of a way to convert an integer(say 5643) to
a NULL terminated string. I'm thinking of writing a function like:
char* get_circle(int x, int y)
{
}

Is there some reason you can't use sprintf(), or
snprintf() if you have a C99 implementation?

But that would return a char* and not the actual string. So I would
need to dynamically allocate the string(not on the stack) so it is not
deleted automatically after the function ends. Then the code which is
using the result is responsible to free it, isn't it? Any ideas how to
go about implementing this function? I'm not asking for code, just for
directions. Thanks.

Objects on the stack (more precisely, "objects with
automatic storage duration") disappear when execution
leaves the block that contains them. So you're right:
it would be folly to return a pointer to such an object,
because the object would vanish before the caller ever
got an opportunity to use the pointer to it.

Some other possibilities:

- The caller could provide the buffer and let the
called function store the string in it. Issues:
the caller must know how large a buffer to provide,
or else the called function needs a way to tell
the caller that the buffer was too small.

- The called function could allocate the storage
dynamically with malloc() or equivalent. Issues:
what should the function do if malloc fails, and
who takes responsibility for freeing the memory
when it's no longer needed?

- The string could be placed in static storage, which
can be pointed to safely by a returned value because
static storage outlasts the execution of any single
block. Issues: You can only have one string "in
play" at a time, because it will be overwritten the
next time the function is called. Variations: the
string could be in dynamic storage with the pointer
to it being static, the static area could have enough
"slots" for N strings used in round-robin fashion to
allow up to N circles to be "in play" simultaneously.

PS.
Say you use some function which returns a char* and you have some code
like:
char* c;
c = somefun();
Then surely somefun(); returns a char* which is saved in memory
temporarily and then this is assigned to c. But where does the
temporary value go? Who's responsible for freeing it?

The value returned by a function is just a value; it
may or may not be stored in memory as part of the value-
returning mechanism for functions. For example, it might
reside in a designated CPU register. Whatever mechanism
is used, it is the C implementation's responsibility to
handle the details, not the programmer's.

--
Eric.Sosman@xxxxxxx

.



Relevant Pages

  • Re: Strings in C
    ... defined as part of the language definition? ... Its storage depends on where it is defined. ... difference to the storage of the string. ... String literals always have static storage, and last until the end of the program. ...
    (comp.lang.c)
  • Re: curious about array initialization.
    ... understanding that its through the variable declaration that we ... By "inline string", I assume you mean a string literal. ... more than that you have implicit storage. ... Its a concept from computer programming. ...
    (comp.lang.c)
  • Re: curious about array initialization.
    ... understanding that its through the variable declaration that we ... By "inline string", I assume you mean a string literal. ... more than that you have implicit storage. ...
    (comp.lang.c)
  • Re: WORKING WITH STRINGS ?! THE FINAL CHALLENGE !
    ... the string should stay alive using the 'storage' pointer ... object Button1: TButton ...
    (alt.comp.lang.borland-delphi)
  • RE: XmlSerialization Object Memory Usage
    ... As you would expect, SimpleObject and ComplexObject are ... the same string objects. ... Hashtable storage = new Hashtable; ... private static ComplexObject BuildObject() ...
    (microsoft.public.dotnet.framework.performance)