-1

Just wondering, I have allocated memory using malloc without a cast. e.g

 char *ptr = malloc(26);

It's compiling successfully with GCC without an error. Is it safe to allocate memory using this expression?

Also, Is there any difference between following expression?

char *ptr = malloc(26); 

and

char *ptr = (char*) malloc(26 * sizeof(char));
msc
  • 33,420
  • 29
  • 119
  • 214
  • [Do I cast the result of malloc?](https://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc) `sizeof(*char)` or `sizeof(char)`? – pzaenger Sep 16 '16 at 10:04
  • [sizeof(char)](http://stackoverflow.com/questions/2215445/are-there-machines-where-sizeofchar-1-or-at-least-char-bit-8) – LPs Sep 16 '16 at 10:05
  • @MichaelWalz I guess (hope..) it is a typo... – LPs Sep 16 '16 at 10:06
  • Multiplying by `sizeof(char)` doesn't harm, and its not necessary to multiply by `sizeof(char)`, since `sizeof(char)` is by definition exactly `1`. – Sunil Bojanapally Sep 16 '16 at 10:08
  • 1
    he asks if it is okay to not use a cast in the first question. – Kami Kaze Sep 16 '16 at 10:12

1 Answers1

1

This is safe. It is often done, because you just have to change the type of *ptr and not the cast so it is more maintainable.

malloc gives you a void* which can be implicitly cast to any other pointer.

There should be no difference between the statements (if you use sizeof char and not *char) in the second question, as the char should always be the smallest memory unit.

sizeof char* btw is not the sizeof char. sizeof char* gives you the size of a pointer, which probably is 4 or 8 depending on your architecture.

The best way would be char *ptr = malloc(26 * sizeof *ptr); again this is more maintainable, because you just have to change the datatype of *ptr.

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
Kami Kaze
  • 2,069
  • 15
  • 27
  • 1
    1) Not having to specify the type is the least reason not to cast. 2) There are no implicit casts in C! 3) An implicit **conversion** is not "optional" (there is no "can be", but it either is done or not. Whether it is done is defined by the language standard. – too honest for this site Sep 16 '16 at 10:23
  • 1
    About the "best way", there is a discussion, wheter to use `sizeof` always function-like, i.e. with the operand in parentheses or not. For expressions, this is not necessary (it is for types, though), but always using them makes usage uniform, althought eh parentheses are strictly part of the operand, not the `sizeof`. – too honest for this site Sep 16 '16 at 10:30
  • whether parentheses are used is up to personal taste with the "best way" I meant he should get the size of the variable not the data type so it is easier to maintain. – Kami Kaze Sep 20 '16 at 07:17
  • @Olaf would you be so kind to elaborate the point about conversion and casts? I am not sure I understand your point. – Kami Kaze Sep 20 '16 at 07:21
  • Not sure what you mean. Type conversion (e.g. the standard integer conversion) and pointer conversions are explained in the standard). As are casts. Basically a cast is not done automatically. – too honest for this site Sep 20 '16 at 12:06