views:

146

answers:

5

I have created a macro to make reserve memory for my strings in C. It looks like this:

#define newString(size) (char*)malloc(sizeof(char) + size)

So is there any reason I shouldn't use this macro in my own personal projects? I know I shouldn't do this in production code because it would require everyone to have that header file and everyone to know that newString was a macro.

A: 

You should check if the allocation was successful.

char* ptr = (char*)malloc(sizeof(char) * size); //Not '+' size!!
if (ptr == 0) //or NULL if defined
{
    //cannot allocate
}

But there is no problem in using a macro

Victor Hurdugaci
+5  A: 

(char*)malloc(sizeof(char) * (size+1)) would be more appropriate (the +1 is to account for the NULL at the end of string, if applicable).

If one is copying a string, strlen() doesn't account for the NULL terminating the string hence an additional memory char is required.

jldupont
+1 for explaining why you need the `size+1`
ChrisF
Instead of `size+1`, use `size + sizeof('\0')` which may be more readable.
Thomas Matthews
A: 

The main reason is that sizeof(char) is defined to always be 1. So you should write instead:

malloc(size)

and it will be shorter to write this than newString(size). Also, malloc returns a void * which you don't need to cast in C.

The other reason is so you don't write a + when you mean *.

Adam Goode
There is nothing wrong with using sizeof(char). We all know it is 1, and that it is unlikely to change, but there isn't any drawback to having extra information. It isn't like he will need to type this over and over again. He is defining the macro for that reason.
A. Levy
To add to what **@A. Levy** said, `char` is also more readable then having a magic number lying around.
Lucas McCoy
But `sizeof(char) == 1` will *never* change, and there is a drawback: it makes the code more verbose and cluttered. I would immediately become suspicious of any code I saw using `sizeof(char)` as to me that is an indication that the author does not know C that well.
jk
One reason to specify sizeof is that your code may use 16-bit Unicode characters in addition to 8-bit ASCII ones.
Steven Sudit
Of course it will change. When you move to Unicode and Search+Replace "char" to "wchar_t".
Hans Passant
@nobugz: Such a significant change requires a bit more than some simple Search+Replaces. Better use p=malloc(sizeof(*p)*size); in the first place.
Secure
+1  A: 

I'm not so sure it's a good idea to use the preprocessor to save yourself some typing. (That road is dark and scary.) It won't be long before you'll want to add something to your macro, and you'll start running into problems that will be way hard to debug.

If you're rolling your own memory management, then use a real function and provide a complimentary "delete" function.

If you're worried about performance, your compiler is smart enough to inline little functions for you.

Seth
A: 

Your macro is equivalent to

malloc(size + 1)

as in C you don't need to cast the result of malloc and sizeof(char) equals 1 by definition. So it doesn't really save you much typing. I would recommend against it, even in your personal projects, as I don't see any benefit to it.

jk