allocation

malloc(0) actually works? [duplicate]

浪子不回头ぞ 提交于 2019-11-26 23:44:02
问题 This question already has answers here : Closed 6 years ago . Possible Duplicate: what’s the point in malloc(0)? Why does malloc(0) actually return a valid pointer for writing ? char *str = NULL; str = (char*)malloc(0); // allocate 0 bytes ? printf("Pointer of str: %p\n", str); strcpy(str, "A very long string ..................."); printf("Value of str: %s", str); free(str); // Causes crash if str is too long Output: Pointer of str: 0xa9d010 Aborted Value of str: A very long string ..........

What happens when you deallocate a pointer twice or more in C++?

∥☆過路亽.° 提交于 2019-11-26 22:24:10
int main(){ Employee *e = new Employee(); delete e; delete e; ... delete e; return 0; } e 's not a reference, it's a pointer. You get undefined behaviour if you try to delete an object through a pointer more that once. This means that pretty much anything can happen from 'appearing to work' to 'crashing' or something completely random. It's undefined behavior, so anything can happen. What's likely to happen is bad. Typically, the free store is a carefully managed system of free and allocated blocks, and new and delete do bookkeeping to keep everything in a consistent state. If you delete again

Linux Allocator Does Not Release Small Chunks of Memory

限于喜欢 提交于 2019-11-26 20:38:11
The Linux glibc allocator seems to be behaving weirdly. Hopefully, someone can shed some light on this. Here is the source file that I have: first.cpp: #include <unistd.h> #include <stdlib.h> #include <list> #include <vector> int main() { std::list<char*> ptrs; for(size_t i = 0; i < 50000; ++i) { ptrs.push_back( new char[1024] ); } for(size_t i = 0; i < 50000; ++i) { delete[] ptrs.back(); ptrs.pop_back(); } ptrs.clear(); sleep(100); return 0; } second.cpp: #include <unistd.h> #include <stdlib.h> #include <list> int main() { char** ptrs = new char*[50000]; for(size_t i = 0; i < 50000; ++i) {

Are Structs always stack allocated or sometimes heap allocated?

爷,独闯天下 提交于 2019-11-26 19:47:17
I was of the impression that in C#, struct elements are allocated on the stack and thus disappear when returning from a method in which they were created. But what happens if I place the struct-values in a list and return that? The elements survives. Are struct instances sometimes allocated on the heap? internal struct Stru { public int i; } internal class StruTry { public List<Stru> Get(int max) { var l = new List<Stru>(); for (int i = 0; i < max; i++) l.Add(new Stru {i=i}); return l; } } code printing 0, 1, 2 [Test] public void T() { var ll = new StruTry().Get(3); foreach (var stru in ll)

malloc(sizeof(int)) vs malloc(sizeof(int *)) vs (int *)malloc(sizeof(int))

感情迁移 提交于 2019-11-26 19:21:38
问题 This question was migrated from Software Engineering Stack Exchange because it can be answered on Stack Overflow. Migrated 6 years ago . I acknowledge that all three of these have a different meaning. But, I don't understand on what particular instances would each of these apply. Can anyone share an example for each of these? Thank you. malloc(sizeof(int)) malloc(sizeof(int *)) (int *)malloc(sizeof(int)) 回答1: malloc(sizeof(int)) means you are allocating space off the heap to store an int .

How to avoid long chain of free's (or deletes) after every error check in C?

僤鯓⒐⒋嵵緔 提交于 2019-11-26 16:51:06
问题 Suppose I write my code very defensively and always check the return types from all the functions that I call. So I go like: char* function() { char* mem = get_memory(100); // first allocation if (!mem) return NULL; struct binder* b = get_binder('regular binder'); // second allocation if (!b) { free(mem); return NULL; } struct file* f = mk_file(); // third allocation if (!f) { free(mem); free_binder(b); return NULL; } // ... } Notice how quickly free() things get out of control. If some of

CUDA allocating array of arrays

南笙酒味 提交于 2019-11-26 16:47:44
问题 I have some trouble with allocate array of arrays in CUDA. void ** data; cudaMalloc(&data, sizeof(void**)*N); // allocates without problems for(int i = 0; i < N; i++) { cudaMalloc(data + i, getSize(i) * sizeof(void*)); // seg fault is thrown } What did I wrong? 回答1: You have to allocate the pointers to a host memory, then allocate device memory for each array and store it's pointer in the host memory. Then allocate the memory for storing the pointers into the device and then copy the host

Escape analysis in Java

走远了吗. 提交于 2019-11-26 15:27:37
问题 As far as I know the JVM uses escape analysis for some performance optimisations like lock coarsening and lock elision. I'm interested if there is a possibility for the JVM to decide that any particular object can be allocated on stack using escape analysis. Some resources make me think that I am right. Is there JVMs that actually do it? 回答1: I don't think it does escape analysis for stack allocation. example: public class EscapeAnalysis { private static class Foo { private int x; private

Is it possible to allocate array inside function and return it using reference?

断了今生、忘了曾经 提交于 2019-11-26 14:47:58
问题 I've tried using a tripple pointer, but it keeps failing. Code: #include <stdlib.h> #include <stdio.h> int set(int *** list) { int count, i; printf("Enter number:\n"); scanf("%d", &count); (*list) = (int **) malloc ( sizeof (int) * count); for ( i = 0; i<count;i++ ) { (**list)[count] = 123; } return count; } int main ( int argc, char ** argv ) { int ** list; int count; count = set(&list); return 0; } Thanks for any advice 回答1: What you call list is actually an array. You might do it the

How to allocate a 2D array of pointers in C++

帅比萌擦擦* 提交于 2019-11-26 13:08:47
问题 I\'m trying to make a pointer point to a 2D array of pointers. What is the syntax and how would I access elements? 回答1: By the letter of the law, here's how to do it: // Create 2D array of pointers: int*** array2d = new (int**)[rows]; for (int i = 0; i < rows; ++i) { array2d[i] = new (int*)[cols]; } // Null out the pointers contained in the array: for (int i = 0; i < rows; ++i) { for (int j = 0; j < cols; ++j) { array2d[i][j] = NULL; } } Be careful to delete the contained pointers, the row