Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 656 Vote(s) - 3.56 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Do I cast the result of malloc?

#1
In [this question][1], someone suggested in a [comment][2] that I should ***not*** cast the result of `malloc`. i.e., I should do this:

int *sieve = malloc(sizeof(*sieve) * length);

rather than:

int *sieve = (int *) malloc(sizeof(*sieve) * length);

Why would this be the case?

[1]:

[To see links please register here]

[2]:

[To see links please register here]

Reply

#2
Casting the value returned by `malloc()` is not necessary now, but I'd like to add one point that seems no one has pointed out:

In the ancient days, that is, before **ANSI C** provides the `void *` as the generic type of pointers, `char *` is the type for such usage. In that case, the cast can shut down the compiler warnings.

Reference: [C FAQ][1]

[1]:

[To see links please register here]

Reply

#3
The returned type is void*, which can be cast to the desired type of data pointer in order to be dereferenceable.
Reply

#4
Just adding my experience, studying computer engineering I see that the two or three professors that I have seen writing in C always cast malloc, however the one I asked (with an immense CV and understanding of C) told me that it is absolutely unnecessary but only used to be absolutely specific, and to get the students into the mentality of being absolutely specific. Essentially casting will not change anything in how it works, it does exactly what it says, allocates memory, and casting does not effect it, you get the same memory, and even if you cast it to something else by mistake (and somehow evade compiler errors) C will access it the same way.

**Edit:** Casting has a certain point. When you use array notation, the code generated has to know how many memory places it has to advance to reach the beginning of the next element, this is achieved through casting. This way you know that for a double you go 8 bytes ahead while for an int you go 4, and so on. Thus it has no effect if you use pointer notation, in array notation it becomes necessary.
Reply

#5
It is not mandatory to cast the results of `malloc`, since it returns `void*` , and a `void*` can be pointed to any datatype.
Reply

#6
The concept behind void pointer is that it can be casted to any data type that is why malloc returns void. Also you must be aware of automatic typecasting. So it is not mandatory to cast the pointer though you must do it. It helps in keeping the code clean and helps debugging
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through