Top Mud Sites Forum Return to TopMudSites.com
Go Back   Top Mud Sites Forum > Mud Development and Administration > MUD Coding
Click here to Register

Reply
 
Thread Tools
Old 01-18-2004, 10:12 AM   #1
Sitral
New Member
 
Join Date: Apr 2002
Posts: 11
Sitral is on a distinguished road
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?
Sitral is offline   Reply With Quote
Old 01-18-2004, 10:37 AM   #2
KaVir
Legend
 
KaVir's Avatar
 
Join Date: Apr 2002
Name: Richard
Home MUD: God Wars II
Posts: 2,052
KaVir will become famous soon enoughKaVir will become famous soon enough
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.
KaVir is offline   Reply With Quote
Old 01-18-2004, 11:21 AM   #3
Sitral
New Member
 
Join Date: Apr 2002
Posts: 11
Sitral is on a distinguished road
Ah, thank you.  I do hope you don't mind, but I like to take every opportunity I get to confirm something new.  Is this (roughly) appropriate for the pointer-to-pointer method?
[code] void osprintf( char **target, char *string )
{   // Syntax; osprintf( &target, string );
   if( *target != NULL ) { free( *target ); *target = NULL; }
   *target = (char *) malloc( strlen(string) +1 );
   sprintf( *target, string );
   return;
}[/quote]

I'm afraid referencing pointers is completely foreign to me, though (or at least the term).  I'll be nosing around for some information, but always appreciate a hand.
Sitral is offline   Reply With Quote
Old 01-18-2004, 11:46 AM   #4
KaVir
Legend
 
KaVir's Avatar
 
Join Date: Apr 2002
Name: Richard
Home MUD: God Wars II
Posts: 2,052
KaVir will become famous soon enoughKaVir will become famous soon enough
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.
KaVir is offline   Reply With Quote
Old 01-18-2004, 12:08 PM   #5
Sitral
New Member
 
Join Date: Apr 2002
Posts: 11
Sitral is on a distinguished road
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.
Sitral is offline   Reply With Quote
Old 01-19-2004, 05:30 AM   #6
Kastagaar
Member
 
Join Date: Apr 2002
Location: Hampshire, UK
Posts: 117
Kastagaar is on a distinguished road
Send a message via Yahoo to Kastagaar
Quote:
Originally Posted by
[code] void osprintf( char **target, char *string )
{ // Syntax; osprintf( &target, string );
if( *target != NULL ) { free( *target ); *target = NULL; }
*target = (char *) malloc( strlen(string) +1 );
sprintf( *target, string );
return;
}
[/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.
Kastagaar is offline   Reply With Quote
Old 01-19-2004, 07:35 AM   #7
Sitral
New Member
 
Join Date: Apr 2002
Posts: 11
Sitral is on a distinguished road
Thanks for the warnings.

Was casting malloc changed with C++?  I'm recieving an error "invalid conversion from 'void*' to 'char*'" (using Cygwin just now, if that's likely to make a difference).

Ah, there's a case of "It bit me on the nose".  Just learned not more than three days ago that a variable could be passed to sprintf with the format information (though why that didn't occur to me before stumps me), didn't think it through far enough for the possible dangers.  Will be certain to make plenty of changes in the coming days.  ...sometimes the drawbacks of hands-on learning cast an overwhelming shadow.
Sitral is offline   Reply With Quote
Old 01-19-2004, 08:09 AM   #8
KaVir
Legend
 
KaVir's Avatar
 
Join Date: Apr 2002
Name: Richard
Home MUD: God Wars II
Posts: 2,052
KaVir will become famous soon enoughKaVir will become famous soon enough
Quote:
Originally Posted by
Was casting malloc changed with C++?
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*.
KaVir is offline   Reply With Quote
Old 01-19-2004, 08:18 AM   #9
Rhuarc
Member
 
Join Date: Jul 2003
Posts: 50
Rhuarc is on a distinguished road
Quote:
Originally Posted by (Kastagaar @ Jan. 19 2004,05:30)
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.*
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++.
Rhuarc is offline   Reply With Quote
Old 01-19-2004, 08:30 AM   #10
KaVir
Legend
 
KaVir's Avatar
 
Join Date: Apr 2002
Name: Richard
Home MUD: God Wars II
Posts: 2,052
KaVir will become famous soon enoughKaVir will become famous soon enough
Sorry Rhuarc, but Kastagaar is absolutely correct - note that he is specifically referring to C, and not C++.
KaVir is offline   Reply With Quote
Old 01-19-2004, 11:43 AM   #11
Kastagaar
Member
 
Join Date: Apr 2002
Location: Hampshire, UK
Posts: 117
Kastagaar is on a distinguished road
Send a message via Yahoo to Kastagaar
Indeed, because in C++ you would be using std::string, hence making the question moot.
Kastagaar is offline   Reply With Quote
Old 01-19-2004, 01:44 PM   #12
Rhuarc
Member
 
Join Date: Jul 2003
Posts: 50
Rhuarc is on a distinguished road
Quote:
Originally Posted by (KaVir @ Jan. 19 2004,08:30)
Sorry Rhuarc, but Kastagaar is absolutely correct - note that he is specifically referring to C, and not C++.
Your right of course -- my apologies.

I've been too long in the c++ box.
Rhuarc is offline   Reply With Quote
Old 01-19-2004, 05:03 PM   #13
KaVir
Legend
 
KaVir's Avatar
 
Join Date: Apr 2002
Name: Richard
Home MUD: God Wars II
Posts: 2,052
KaVir will become famous soon enoughKaVir will become famous soon enough
Quote:
Originally Posted by
I've been too long in the c++ box
Me too - and it's even worse now, since I started learning Java!
KaVir is offline   Reply With Quote
Old 01-20-2004, 07:28 AM   #14
Kastagaar
Member
 
Join Date: Apr 2002
Location: Hampshire, UK
Posts: 117
Kastagaar is on a distinguished road
Send a message via Yahoo to Kastagaar
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.
Kastagaar is offline   Reply With Quote
Old 01-24-2004, 02:34 AM   #15
 
Posts: n/a
Quote:
Originally Posted by (Rhuarc @ Jan. 19 2004,13:44)
Your right of course -- my apologies.

I've been too long in the c++ box.
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.  :-)
  Reply With Quote
Old 01-25-2004, 05:04 PM   #16
Rhuarc
Member
 
Join Date: Jul 2003
Posts: 50
Rhuarc is on a distinguished road
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!
Rhuarc is offline   Reply With Quote
Reply


Thread Tools


Memory management - Similar Threads
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

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off

All times are GMT -4. The time now is 08:52 PM.


Powered by vBulletin® Version 3.6.7
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Style based on a design by Essilor
Copyright Top Mud Sites.com 2014