Advertisement

about pointers and functions

Started by January 29, 2000 09:05 AM
17 comments, last by bosjoh 24 years, 10 months ago
I want to have a function that needs an integer and converts it to a string. Like this:

char *val2str(int value);
I want to have a pointer to the return value. I''ve tried things like ptr=valstr and the like. Is there a solution for this or should I use the inconvenient "void valstr(int, char*)" method?
You should be able to get something like that to work. Remember, you''ll have to dynamically allocate your memory for the buffer though. If you just use an array in the function, it''ll be destroyed as soon as the function returns.
Advertisement
I would recommend against using dynamic memory for your string to return. Instead use a static char buffer in your function, and return a pointer to that. That greatly reduces the chances for a memory leak. If lifetime of the string is an issue, copy the string upon return.
You can either allocate the string within the function and then delete the string with code outside the function, or you can use something like this:
char* val2str(char* str, int value)
Allocate a string outside the function and pass it in as str, and then return str within the function.

--Shannon Schlomer, BLAZE Technologies, Inc.
--Shannon Schlomer, BLAZE Technologies, Inc.
SiCrane: I had used your method (global char stringbuf[256]) before, but what happens is this:
char *str1,*str2;str1=(char *)malloc(256);str2=(char *)malloc(256);str1=val2str(20);str2=val2str(30);

What happens is that you both lose the memory originally allocated, and when you use the function the 2nd time str1 equals to str2. I guess I have to use the method of blaze.
Maybe I am going to stick my foot in my mouth here but, isn't it just as easy to do something like a strcpy or a sprintf in this case.

char buf[25];

sprintf(buf, "%s", val2str(10));

or

strcpy(buf, val2str(10));
buf[strlen(val2str(10))] ='\0';

??? Just thought I would chime in.
Kressilac

ps Problem with this could be bounds checking but it is most likely not going to occur with 25 digits.


Edited by - kressilac on 1/31/00 8:24:46 AM
Derek Licciardi (Kressilac)Elysian Productions Inc.
Advertisement
Hi,
This might not be possible, but if you ARE using C++, is there any good reason you can't use string? (I realize there could be any number of reasons, but...) Using string simplifies this sort of code tremendously, insures that you don't go around leaking memory or reusing buffers, etc...

-Brian

Edited by - osmanb on 2/1/00 12:11:19 PM
Why even bother with all that?

#define val2str(string, number) sprintf(string, "%d", number);



Mason McCuskey
Spin Studios - home of Quaternion, 2000 GDC Indie Games Fest Finalist!
www.spin-studios.com
Founder, Cuttlefish Industries
The Cuttlefish Engine lets anyone develop great games for iPad, iPhone, Android, WP7, the web, and more!
quote:
SiCrane: I had used your method (global char stringbuf[256]) before, but what happens is this:

char *str1,*str2;
str1=(char *)malloc(256);
str2=(char *)malloc(256);
str1=val2str(20);
str2=val2str(30);

What happens is that you both lose the memory originally allocated, and when you use the function the 2nd time str1 equals to str2. I guess I have to use the method of blaze.


When using static buffers as return values the syntax looks more like this:

char * str1;
char * str2;
str1 = val2str(20);
// Use str1
str2 = val2str(30);
// Use str2

If you need both str1 and str2, then copy the string into a buffer, before calling the function again.

String returns aren''t such a good example of use of static buffers, things like network code can take much better advantage of it.

Mason: Instances where you''d want to go to all this trouble: escape-coded strings for output; non-standard byte encodings (sprintf is still restricted to ASCII character set).
Thanks for all your replies.
Now I will display all the code, so it shows that using a static buffer doesn''t work (Or I am doing something wrong):
char stringbuf[256];char *stringadd(char *txt1,char *txt2){   unsigned char len1,len2;   len1=strlen(txt1);   len2=strlen(txt2);   if (len1+len2>255) len2=255-len1;   strcpy(stringbuf,txt1);   strcpy(stringbuf+len1,txt2);   return(stringbuf);};char *val_to_str(unsigned long value){   unsigned char pos=0,r;   unsigned long tval=value;   do {      pos++;      tval=tval/10;   } while (tval>0);   for (r=0;r      stringbuf[pos-r-1]=48+value%10;      value=value/10;   };   stringbuf[pos]=0;   return(stringbuf);};//Converts a value to a string.


When I try to call a function like this:
printf(addstring("test",val_to_str(20)));
It shows "testtest" in stead of "test20". When I swap the arguments it displays random garbage and the word test.
Try it.

BTW, this is programmed in good ol'' MS-DOS BC++ 5.0

This topic is closed to new replies.

Advertisement