allocation

dynamic allocating array of arrays in C

蓝咒 提交于 2019-11-26 12:41:41
问题 I don\'t truly understand some basic things in C like dynamically allocating array of arrays. I know you can do: int **m; in order to declare a 2 dimensional array (which subsequently would be allocated using some *alloc function). Also it can be \"easily\" accessed by doing *(*(m + line) + column) . But how should I assign a value to an element from that array? Using gcc the following statement m[line][column] = 12; fails with a segmentation fault. Any article/docs will be appreciated. :-)

How to profile memory usage & performance with Instruments?

懵懂的女人 提交于 2019-11-26 12:35:20
问题 Of all the Instruments Trace Templates, I love using: Zombies to detect where an object is getting over-released, great for debugging EXEC_BAD_ACCESS errors. Leaks to detect memory leaks. Core Animation w Color Blended Layers to detect frame rate & translucent subviews, great for smoothing up UITableView scrolling. I always hear people saying to profile my app\'s memory usage & performance. Why should I profile memory usage & performance? My app runs fine. How do I do it? I\'ve used

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

感情迁移 提交于 2019-11-26 12:19:10
问题 int main(){ Employee *e = new Employee(); delete e; delete e; ... delete e; return 0; } 回答1: 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. 回答2: 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

Multithreaded Memory Allocators for C/C++

混江龙づ霸主 提交于 2019-11-26 10:18:48
问题 I currently have heavily multi-threaded server application, and I\'m shopping around for a good multi-threaded memory allocator. So far I\'m torn between: Sun\'s umem Google\'s tcmalloc Intel\'s threading building blocks allocator Emery Berger\'s hoard From what I\'ve found hoard might be the fastest, but I hadn\'t heard of it before today, so I\'m skeptical if its really as good as it seems. Anyone have personal experience trying out these allocators? 回答1: I've used tcmalloc and read about

Why does the compiler allocate more than needed in the stack?

和自甴很熟 提交于 2019-11-26 10:03:17
问题 I have a simple C program. Let\'s say, for example, I have an int and a char array of length 20. I need 24 bytes in total. int main() { char buffer[20]; int x = 0; buffer[0] = \'a\'; buffer[19] = \'a\'; } The stack needs to be aligned to a 16 bytes boundary, so I presume a compiler will reserve 32 bytes. But when I compile such a program with gcc x86-64 and read the output assembly, the compiler reserves 64 bytes. ..\\gcc -S -o main.s main.c Gives me: .file \"main.c\" .def __main; .scl 2;

Should I check if malloc() was successful?

旧街凉风 提交于 2019-11-26 09:55:12
问题 Should one check after each malloc() if it was successful? Is it at all possible that a malloc() fails? What happens then? At school we were told that we should check, ie.: arr = (int) malloc(sizeof(int)*x*y); if(arr==NULL){ printf(\"Error. Allocation was unsuccessful. \\n\"); return 1; } What is the practice regarding this? Can I do it this way: if(!(arr = (int) malloc(sizeof(int)*x*y)) <error> 回答1: No need to cast malloc() . Yes it is required to check whether the malloc() was successful or

Are Structs always stack allocated or sometimes heap allocated?

人走茶凉 提交于 2019-11-26 07:27:27
问题 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; } }

Allocate memory 2d array in function C

随声附和 提交于 2019-11-26 01:37:59
问题 How to allocate dynamic memory for 2d array in function ? I tried this way: int main() { int m=4,n=3; int** arr; allocate_mem(&arr,n,m); } void allocate_mem(int*** arr,int n, int m) { *arr=(int**)malloc(n*sizeof(int*)); for(int i=0;i<n;i++) *arr[i]=(int*)malloc(m*sizeof(int)); } But it doesn\'t work. 回答1: Your code is wrong at *arr[i]=(int*)malloc(m*sizeof(int)); because the precedence of the [] operator is higher than the * deference operator: In the expression *arr[i] , first arr[i] is

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):

Determine size of dynamically allocated memory in C

时光怂恿深爱的人放手 提交于 2019-11-26 00:09:57
问题 Is there a way in C to find out the size of dynamically allocated memory? For example, after char* p = malloc (100); Is there a way to find out the size of memory associated with p ? 回答1: comp.lang.c FAQ list · Question 7.27 - Q. So can I query the malloc package to find out how big an allocated block is? A. Unfortunately, there is no standard or portable way. (Some compilers provide nonstandard extensions.) If you need to know, you'll have to keep track of it yourself. (See also question 7