![]() |
#1 |
New Member
Join Date: Apr 2002
Posts: 11
![]() |
It seemed like a simple idea, free and allocate a string in the same function and make it user friendly. Is it possible to free/malloc through a pointer? This's roughly what I'd like to do:
[code] void osprintf( char *target, char *string ) { if( target != NULL ) { free( target ); target = NULL; } target = (char *) malloc( strlen(string) ); sprintf( target, string ); return; }[/quote] But this only impacts the local incarnation of target (with the exception of free?). I've exhausted my knowledge and creativity and the best I've been able to do is make the function a char-pointer and return target, but the syntax just isn't as appealing. Any advice, or am I asking for the impossible? |
![]() |
![]() |
![]() |
#2 |
Legend
Join Date: Apr 2002
Name: Richard
Home MUD: God Wars II
Posts: 2,052
![]() ![]() |
You need to use a pointer to a pointer for 'target' (unless you're using C++, in which case a reference to a pointer might be cleaner). You should also check for a NULL 'string' being passed in.
|
![]() |
![]() |
![]() |
#3 |
New Member
Join Date: Apr 2002
Posts: 11
![]() |
|
![]() |
![]() |
![]() |
#4 |
Legend
Join Date: Apr 2002
Name: Richard
Home MUD: God Wars II
Posts: 2,052
![]() ![]() |
Yes, that is exactly right. The reference solution (for C++ only) would look exactly the same as your first example, except that the function argument "char *target" would be written "char *&target".
Note that in addition to dealing with a NULL 'string' argument, there are two other problems which you can't cater for - an uninitialised 'target', or a 'target' which points to memory stored somewhere other than on the heap. These are points to be kept in mind, as they could easily be done by mistake. |
![]() |
![]() |
![]() |
#5 |
New Member
Join Date: Apr 2002
Posts: 11
![]() |
Mwaha, referencing hits all the nails, and I owe you much gratitude. As for the concerns...well, if it isn't obvious already, I'm still learning as I go. And since I'm not answering to a crowd, I think I'll wait till I cross those bridges to fix them.
Thanks again. |
![]() |
![]() |
![]() |
#6 |
Member
|
[/quote]
A couple of notes. In C, you shouldn't cast the result of malloc (or calloc or realloc). The compiler will do this for you, and it knows better. On a practical note, the cast can hide a warning which indicates that you have forgotten stdlib.h (if, indeed, you have forgotten it). Forgetting this and using malloc causes undefined behaviour.* My second note is that sprintf in that context can be erroneous. if the text contained in the variable string contains %s or %d or whatever, then this too will be undefined behavious - a possible crash. Use strcpy instead. *This is because undeclared functions are assumed to return int. One specific case where this would fail is in a machine which has a 2-byte int and a 4-byte pointer. The pointer returned from malloc would be truncated to two bytes before being converted back into a 4-byte pointer with half its information missing. |
![]() |
![]() |
![]() |
#7 |
New Member
Join Date: Apr 2002
Posts: 11
![]() |
|
![]() |
![]() |
![]() |
#8 |
Legend
Join Date: Apr 2002
Name: Richard
Home MUD: God Wars II
Posts: 2,052
![]() ![]() |
Yes, in C++ you need to cast - although in C++ you should really be using 'new' instead of 'malloc'. You might also want to try looking at std::string as an alternative to char*.
|
![]() |
![]() |
![]() |
#9 |
Member
Join Date: Jul 2003
Posts: 50
![]() |
This is incorrect. malloc() returns a void*, which, if your compiler is typecasting for you, your compiler is broken.
You must typecast the result of malloc() to the type that you require. [code] char* p = (char*)malloc( 10 * sizeof(char) ); [/quote] will compile, [code] char *p = malloc( 10 * sizeof(char) ); [/quote] will not, not even in vc++. |
![]() |
![]() |
![]() |
#10 |
Legend
Join Date: Apr 2002
Name: Richard
Home MUD: God Wars II
Posts: 2,052
![]() ![]() |
Sorry Rhuarc, but Kastagaar is absolutely correct - note that he is specifically referring to C, and not C++.
|
![]() |
![]() |
![]() |
#11 |
Member
|
Indeed, because in C++ you would be using std::string, hence making the question moot.
|
![]() |
![]() |
![]() |
#12 |
Member
Join Date: Jul 2003
Posts: 50
![]() |
Your right of course -- my apologies.
I've been too long in the c++ box. |
![]() |
![]() |
![]() |
#13 |
Legend
Join Date: Apr 2002
Name: Richard
Home MUD: God Wars II
Posts: 2,052
![]() ![]() |
Me too - and it's even worse now, since I started learning Java!
|
![]() |
![]() |
![]() |
#14 |
Member
|
Straying off topic for a moment...
I learned C at university, followed by C++. Then, spending a year out in industry, I practiced C while teaching myself Java. I revisited C++ when it came to leave university and enter the professional world, and I've been working with it ever since. What I've discovered over this time is the respect I have for C++ and the multitude of features it has over Java, and for Java and the multitude of features it has over C++. I almost feel enlightened in some way. |
![]() |
![]() |
![]() |
#15 |
Posts: n/a
|
Don't sweat it. Kerningham and Ritchie cast malloc to char* or whatever they needed all the time. Didn't they invent C? ;-)
I also cast malloc consistently from void* to whatever when programming in C. Of course I always include stdlib.h Kastagar is correct that it ain't necessary at all. So why do I do it? I think it makes the code a bit clearer. And well it's a habit. :-) |
![]() |
![]() |
#16 |
Member
Join Date: Jul 2003
Posts: 50
![]() |
Well, when I write code, I try to have an eye towards readability. One of the nice things that casting does for you, is it not only makes it clear to the compiler what you were expecting, but it makes it crystal clear to anyone else who later has to come and maintain your code.
Trusting the compiler to know what you wanted is just a big an evil as trusting the client to rtfm... Works in theory! |
![]() |
![]() |
![]() |
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
Angst management | Brody | Roleplaying and Storytelling | 6 | 06-15-2006 02:23 AM |
DNS Management | Threshold | MUD Administration | 1 | 07-25-2005 06:39 PM |
Memory Vs CPU usage | karlan | MUD Coding | 13 | 04-13-2003 10:14 PM |
|
|