4 ms·
How do you prefer to use e.g. malloc?
by rightbyte 8y ago
How do you prefer to use e.g. malloc?
- icedchai 8y agoYou don't need to cast there... malloc returns a void*.
- AnimalMuppet 8y agoWhich you are going to use how? Don't you have to cast it to something to be able to use it?
- deathanatos 8y agoNo, you don't. The following, int *i = malloc(sizeof(int)); is valid C, and will compile just fine. I think it's considered good form, too, as the cast doesn't really add anything not already present, and might even drift out of date with the surrounding code. (And just gets in the way of legibility.) IIRC, it is not valid C++. But malloc()/void * is somewhat rarer to find there, as you have better options.
- shakna 8y agoThat's an implicit cast from void * to int * . void *malloc(size_t size);
- caf 8y agoThere's no such thing as an "implicit cast". A cast is an explicit conversion (a cast expression), and the assignment expression discussed here has an implicit conversion.
- tedunangst 8y agoIn standard parlance, that is a conversion. Only (this) is a cast.
- valarauca1 8y agoyou mean `mmap`? Also yes I'm radical enough to suggest the 50+ year old C standard library API is bad. Which isn't really a radical position >.>
- shakna 8y agoIs your irritation aimed at the implementations of malloc in the standard compilers? Or all malloc implementations? (e.g. Hoard [0]). [0] https://github.com/emeryberger/Hoard https://github.com/emeryberger/Hoard
- valarauca1 8y agomy irritation is C's primitive type system. And programmers willingness to conflate their standard library with system level operations.
- deathanatos 8y agommap and malloc do very different things. You didn't really clarify why you thought malloc() was bad, or why you think someone should call mmap(), so I'm mostly left to guess. mmap, for example, is free to require that the requested range be a multiple of a page; trying to use it as if it were malloc() would, on such systems, cause any allocations less than a page to require a whole page. And since this is a rather common case, you'd be using a ton more memory. malloc(), however, can subdivide a large allocation from the OS into small allocations for the process; it is my understanding that most modern malloc()s will do this, in some form. (It is also my understanding that some malloc() implementations will use mmap() for allocating memory from the OS…)
- valarauca1 8y ago`malloc` is part of the standard library. the underlying implementation is environmental specific, or implementation specific depending on the host application. Generally speaking `mmap` is the system call that * Allocates memory * Moves allocations around `malloc` is the common entry point into a library that attempts to ensure you invoke the `mmap` system call as little as possible. trying to use it as if it were malloc() would, on such systems, cause any allocations less than a page to require a whole page. And since this is a rather common case, you'd be using a ton more memory. This is a misconception [1]. Malloc is governed by hardware limitations. If you ask for 50bytes, it'll give you a full 4KiB page, because hardware can only segment memory in 4KiB chunks. Actually some allocators will attempt to _use_ those non-allocated regions of the remaining ~4046 bytes for other allocations in order to be faster (less systemcalls), this is why sometimes writing past buffer ends can give you garbage data instead of segfaults. [1] https://stackoverflow.com/questions/45458732/how-are-memory-allocations-smaller-than-page-size-handled-in-linux https://stackoverflow.com/questions/45458732/how-are-memory-...
- unwind 8y agoPlease see https://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc https://stackoverflow.com/questions/605845/do-i-cast-the-res... for some thoughts on this. In short (as already stated, of course), you don't need to cast void * in C to/from other object pointers. Adding a cast to the return value of malloc() is a bad idea (I'm biased).