alloca

alloc, malloc, and alloca — What's the difference?

本秂侑毒 提交于 2019-12-02 18:16:21
I was under the impression that alloc in Objective-C (when we invoke [anyObject alloc] is actually implementing C function malloc and the memory getting allocated in heap, but could not find anywhere the answer for this. Also, while searching for alloc , I found alloca which allocates memory in stack. If I am not wrong, alloc allocates memory in heap to create objects. So, what is the difference between alloc and malloc (and alloca )? Can anyone please summarize? alloc() is not a standard C library function. Some older compilers and libraries contain an <alloc.h> library which provides some

Is there an allocator that uses alloca and is otherwise C++ STL compliant?

别来无恙 提交于 2019-11-30 14:58:29
问题 I have two questions: 1) Is it possible to implement an allocator that uses alloca to allocate memory on the stack and is otherwise C++ STL compliant? If there is code out there, you can make me happy by simply pointing me to the URL. :-) If there is no code out there, perhaps you can sketch the functions allocate and deallocate? 2) If the answer to the above question is 'yes', I'd like to understand how it is possible to allocate memory on the stack for class members. As an example, consider

Is there an allocator that uses alloca and is otherwise C++ STL compliant?

我怕爱的太早我们不能终老 提交于 2019-11-30 12:36:52
I have two questions: 1) Is it possible to implement an allocator that uses alloca to allocate memory on the stack and is otherwise C++ STL compliant? If there is code out there, you can make me happy by simply pointing me to the URL. :-) If there is no code out there, perhaps you can sketch the functions allocate and deallocate? 2) If the answer to the above question is 'yes', I'd like to understand how it is possible to allocate memory on the stack for class members. As an example, consider an std::vector<int, AllocaAllocator<int> > and suppose that a call of the member function 'resize' of

What's the difference between alloca(n) and char x[n]?

只谈情不闲聊 提交于 2019-11-29 14:06:19
What is the difference between void *bytes = alloca(size); and char bytes[size]; //Or to be more precise, char x[size]; void *bytes = x; ...where size is a variable whose value is unknown at compile-time. alloca() does not reclaim memory until the current function ends, while the variable length array reclaims the memory when the current block ends. Put another way: void foo() { size_t size = 42; if (size) { void *bytes1 = alloca(size); char bytes2[size]; } // bytes2 is deallocated here }; //bytes1 is deallocated here alloca() can be supported (in a fashion) on any C89 compiler, while the

Is alloca part of the C++ standard?

不羁的心 提交于 2019-11-28 13:18:55
Is alloca part of the C++ standard? No. The answer says it all. Not only is it not part of the C++ standard, it is not part of any standard. It's not part of C nor is it part of POSIX. Furthermore, allow me to quote from the Linux man page for alloca(3) : The alloca() function is machine and compiler dependent. On many systems its implementation is buggy. Its use is discouraged . (emphasis added) 来源: https://stackoverflow.com/questions/2318306/is-alloca-part-of-the-c-standard

how does stack growing work on windows and linux?

巧了我就是萌 提交于 2019-11-28 09:49:19
问题 I just read that windows programs call _alloca on function entry to grow the stack if they need more than 4k on the stack. I guss that every time the guard page is hit windows allocates a new page for the stack, therefore _alloca accesses the stack in 4k steps to allocate the space. I also read that this only applies to windows. How does linux (or other oses) solve this problem if they don't need _alloca ? 回答1: Linux relies on a heavily optimized page fault handling, so what happens is that

In which cases is alloca() useful?

Deadly 提交于 2019-11-28 09:33:43
问题 Why would you ever want to use alloca() when you could always allocate a fixed size buffer on the stack large enough to fit all uses? This is not a rhetorical question... 回答1: It could be useful if the size of the buffer varies at runtime, or if you only sometimes need it: this would use less stack space overall than a fixed-size buffer in each call. Particularly if the function is high up the stack or recursive. 回答2: You might want to use it if there's no way to know the maximum size you

What's the difference between alloca(n) and char x[n]?

被刻印的时光 ゝ 提交于 2019-11-28 07:35:59
问题 What is the difference between void *bytes = alloca(size); and char bytes[size]; //Or to be more precise, char x[size]; void *bytes = x; ...where size is a variable whose value is unknown at compile-time. 回答1: alloca() does not reclaim memory until the current function ends, while the variable length array reclaims the memory when the current block ends. Put another way: void foo() { size_t size = 42; if (size) { void *bytes1 = alloca(size); char bytes2[size]; } // bytes2 is deallocated here

Is alloca part of the C++ standard?

纵然是瞬间 提交于 2019-11-26 22:00:39
问题 Is alloca part of the C++ standard? 回答1: No. The answer says it all. 回答2: Not only is it not part of the C++ standard, it is not part of any standard. It's not part of C nor is it part of POSIX. Furthermore, allow me to quote from the Linux man page for alloca(3) : The alloca() function is machine and compiler dependent. On many systems its implementation is buggy. Its use is discouraged . (emphasis added) 来源: https://stackoverflow.com/questions/2318306/is-alloca-part-of-the-c-standard

Why is the use of alloca() not considered good practice?

感情迁移 提交于 2019-11-26 01:19:55
问题 alloca() allocates memory on the stack rather than on the heap, as in the case of malloc() . So, when I return from the routine the memory is freed. So, actually this solves my problem of freeing up dynamically allocated memory. Freeing of memory allocated through malloc() is a major headache and if somehow missed leads to all sorts of memory problems. Why is the use of alloca() discouraged in spite of the above features? 回答1: The answer is right there in the man page (at least on Linux):