Top Mud Sites Forum

Top Mud Sites Forum (http://www.topmudsites.com/forums/index.php)
-   MUD Coding (http://www.topmudsites.com/forums/forumdisplay.php?f=9)
-   -   Memory management (http://www.topmudsites.com/forums/showthread.php?t=333)

Sitral 01-18-2004 10:12 AM

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?

KaVir 01-18-2004 10:37 AM

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.

Sitral 01-18-2004 11:21 AM


KaVir 01-18-2004 11:46 AM

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.

Sitral 01-18-2004 12:08 PM

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.

Kastagaar 01-19-2004 05:30 AM

[/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.

Sitral 01-19-2004 07:35 AM


KaVir 01-19-2004 08:09 AM

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*.

Rhuarc 01-19-2004 08:18 AM

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++.

KaVir 01-19-2004 08:30 AM

Sorry Rhuarc, but Kastagaar is absolutely correct - note that he is specifically referring to C, and not C++.

Kastagaar 01-19-2004 11:43 AM

Indeed, because in C++ you would be using std::string, hence making the question moot.

Rhuarc 01-19-2004 01:44 PM

Your right of course -- my apologies.

I've been too long in the c++ box.

KaVir 01-19-2004 05:03 PM

Me too - and it's even worse now, since I started learning Java!

Kastagaar 01-20-2004 07:28 AM

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.

01-24-2004 02:34 AM

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.  :-)

Rhuarc 01-25-2004 05:04 PM

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!


All times are GMT -4. The time now is 01:41 PM.

Powered by vBulletin® Version 3.6.7
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright Top Mud Sites.com 2022