allocation

Memory Allocation/Deallocation? [closed]

纵饮孤独 提交于 2019-11-27 04:11:07
问题 I have been looking at memory allocation lately and I am a bit confused about the basics. I haven't been able to wrap my head around the simple stuff. What does it mean to allocate memory? What happens? I would appreciated answers to any of these questions: Where is the "memory" that is being allocated? What is this "memory"? Space in an array? Or something else? What happens exactly when this "memory" gets allocated? What happens exactly when the memory gets deallocated? It would also really

dynamic allocating array of arrays in C

一笑奈何 提交于 2019-11-27 03:59:43
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. :-) The m[line][column] = 12 syntax is ok (provided line and column are in range). However, you didn't write

Memory allocation of class objects [closed]

百般思念 提交于 2019-11-27 03:33:39
问题 I have been asked this question in interview. Please help me find its answer. Suppose you have a class Employee. There are 2 variables in it - 1. String Name 2. Int Age Now, Employee emp = new Employee(); Now the Questions asked are : Where the object emp stored in memory i.e in stack or heap and how ? Where the name and age variables stored in memory and how? What does each word in this statement do i.e what does employee do..then emp..then =.. then new.. then employee.. then ()..then ; What

Multithreaded Memory Allocators for C/C++

痴心易碎 提交于 2019-11-27 02:58:27
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? I've used tcmalloc and read about Hoard. Both have similar implementations and both achieve roughly linear performance scaling with respect to the

How to profile memory usage & performance with Instruments?

浪子不回头ぞ 提交于 2019-11-27 02:49:16
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 Allocations and see that my iPhone app starts at 1 MB total allocated memory and grows to 5 MB after normal usage.

Should I check if malloc() was successful?

孤街浪徒 提交于 2019-11-27 02:02:19
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> Gopi No need to cast malloc() . Yes it is required to check whether the malloc() was successful or not. Let's say malloc() failed and you are trying to access the pointer thinking memory is allocated will

Malloc error “can't allocate region” failed with error code 12. Any idea how to resolve this?

南笙酒味 提交于 2019-11-27 01:20:32
问题 i am getting this error and dont know what to do with that: AppName(3786,0xa0810540) malloc: *** mmap(size=16777216) failed (error code=12) *** error: can't allocate region *** set a breakpoint in malloc_error_break to debug If i set a breakpoint to that line that occurs the error, i dont know what i have to search specially for. In instruments i have checked the allocations and the value is increasing until 14,5 GB of all allocations. Can someone give me help? brush51 EDIT 1: More

C++ Multi-dimensional Arrays on the Heap

空扰寡人 提交于 2019-11-27 01:06:22
问题 How would I go about dynamically allocating a multi-dimensional array? 回答1: If you know the size of nested dimensions already, you can also literally allocate a multi dimensional array using new: typedef int dimensions[3][4]; dimensions * dim = new dimensions[10]; dim[/* from 0 to 9 */][/* from 0 to 2 */][/* from 0 to 3 */] = 42; delete [] dim; instead of 10 , a runtime determined value can be passed. Since it's not part of the type operator new returns, that's allowed. This is nice if you

Freaky way of allocating two-dimensional array?

我们两清 提交于 2019-11-26 23:55:18
问题 In a project, somebody pushed this line: double (*e)[n+1] = malloc((n+1) * sizeof(*e)); Which supposedly creates a two-dimensional array of (n+1)*(n+1) doubles. Supposedly , I say, because so far, nobody I asked could tell me what this does, exactly, nor where it originated from or why it should work (which allegedly, it does, but I'm not yet buying it). Perhaps I'm missing something obvious, but I'd appreciate it if somebody could explain above line to me. Because personally, I'd feel much

Dynamic Allocation of two-dimensional array C++

て烟熏妆下的殇ゞ 提交于 2019-11-26 23:47:18
问题 Hi I'm pretty new to C++ and I need to dynamicacally allocate two-dimensional array. No error but in runtime when I set a order and first row then I get a runtime error: "Segmentation Fault"...Here's code: #include <iostream> using namespace std; double ** allocateDynamicArray(int &order){ double ** arr = new double *[order]; for(int i = 0;i < order; i++){ *arr = new double[order+1]; } return arr; } void deallocateDynamicArray(double **arr, int order){ for(int i=0; i<order; i++){ delete []