8

Which if any of the following are correct and would be considered best practice to create a char string capable of holding 100 characters?

char * charStringA = malloc(100);
char * charStringB = malloc(sizeof(char)*100);
char * charStringC = (char*)malloc(100);
char * charStringD = (char*)malloc(sizeof(char)*100);
t1w
  • 1,408
  • 5
  • 25
  • 55
  • 3
    heck here http://theunixshell.blogspot.com/2012/12/to-cast-or-not-to-cast-malloc-return.html – Vijay Dec 31 '12 at 09:54
  • 2
    They are all "correct" syntax. Your problem is not on a syntax level, so please change the header of your question accordingly. I'd go for "best practice" instead of "correct syntax". – Jens Gustedt Dec 31 '12 at 10:01
  • 6
    Well ... if you want a string with 100 "visible" characters you need to allocate 101 bytes. The "extra" byte can then hold the terminating '\0'. – pmg Dec 31 '12 at 10:25

3 Answers3

13
char * charStringA = malloc(100);
char * charStringB = malloc(sizeof(char)*100);

Both are equally correct.
Two important points that should be considered in this evaluation are:

  1. size of char is guaranteed to be one byte by the C standard.
  2. A void pointer can be assigned to any pointer without an explicit cast in C and the casting is unnecessary. Casting the return value of malloc is considered as an bad practice because of the following:

What's wrong with casting malloc's return value?


The above answer applies to the options mentioned in the OP. An better practice is to use sizeof without making any assumptions about the size of any type. This is the reason and purpose that sizeof exists. In this case the best practice will be to use:

char * charStringB = malloc(sizeof(*charStringB)*100);

Note that *charStringB is same as char but this gives you the flexibility that if you want to change the type in future then there is fewer number of places where you need to remember to make modifications.

Alok Save
  • 202,538
  • 53
  • 430
  • 533
  • 3
    But as a best practice I would go for `malloc(sizeof(char)*100)`, since for any other type you need `sizeof`. – Barney Szabolcs Dec 31 '12 at 10:07
  • @BarnabasSzabolcs: That is precisely correct. The answer only evaluates the options mentioned in the OP. As a general good practice, one should **never** assume size of any *type*, this is the reason `sizeof` was provided. – Alok Save Dec 31 '12 at 10:16
  • 2
    the benefit of `char * charStringB = malloc(sizeof(*charStringB)*100);` is that if we decide to support Unicode (16 or 32-bit characters), all that needs to change [on this line] is the `char` changed to `wchar_t`, and everything else will work as expected. – Mats Petersson Dec 31 '12 at 11:43
5

The most general form is:

#include <stdio.h>

typedef struct { int a; char b[55]; } Thing;

Thing *p;
p = malloc (100 * sizeof *p);

This works indepentely of the actual definition of Thing, so if you would "reuse" the line as

Typedef { float water; int fire; } OtherThing;
OtherThing *p;
p = malloc (100 * sizeof *p);

it would still function as intended.

The original case would yield:

char *p;
p = malloc (100 * sizeof *p);

, where the sizeof *p would of course be superfluous (since sizeof(char) == 1 by definition) , but it won't hurt.

BTW: this answer is mostly about style. Syntactically, all variants are acceptible, given you include stdlib.h (or manually introduce a prototype for malloc())

wildplasser
  • 43,142
  • 8
  • 66
  • 109
3

The first one, since char is always one byte, cast of malloc is only necesary in c++

David Ranieri
  • 39,972
  • 7
  • 52
  • 94