dynamic-allocation

memory allocation in Stack and Heap

假如想象 提交于 2019-11-30 03:14:35
This may seem like a very basic question, but its been in my head so: When we allocate a local variable, it goes into stack. Similarly dynamic allocation cause the variable to go on heap. Now, my question is, is this variable actually lie on stack or heap or we will just a reference in the stack and Heap. For example, Suppose I declare a variable int i . Now this i is allocated on the stack. So, when I print the address of i , this will be one of the location on stack? Same question for heap as well. Chris Eberle I'm not entirely sure what you're asking, but I'll try my best to answer. The

How do I allocate a 2 D array with contigious memory ? How Do I use it to access rows and columns? Give me an example

五迷三道 提交于 2019-11-29 15:12:51
I have created a 2 d array which reads as follows int i,j,lx,ly;// lx,ly are the row and column respectively double** a; a=(double**) malloc((lx+2)*sizeof(double)); a[0]= (double*) malloc((lx+2)*(ly+2)* sizeof(double)); assert(a[0]); for(i=1;i<lx+2;i++) { a[i]=a[i-1]+i*(ly+2); } // I allocate a value of 0 to all the elements in this array as below for(i=0;i<(lx+2)*(ly+2);i++) { a[i]=0; } // I print out all my elements below for(i=0;i<(lx+2)*(ly+2);i++) { printf("position %d values %d\n",i,a[i]); } // When I see the output , it shows me a junk value at one particular position 13. I am unable to

What is Dynamic Memory Allocation in C++?

若如初见. 提交于 2019-11-29 07:55:55
I'm learning about Dynamic Memory Allocation in C++ and the keywords new and new[] are mentioned. It is said to enable users to specify the size of the memory allocation at runtime, unlike simply declaring a variable or array with a fixed size in the source code. I don't understand this concept. How does it work? I just need a clarification on the idea and an example would be helpful! So, if you want an array of 10 integers, you'd be writing: int arr[10]; But what if you wanted to do something like this; cout << "How many?"; cin >> num; int arr[num]; Well, the C++ language doesn't allow that.

What is wrong with using arrays dynamically allocated in C++? [duplicate]

ⅰ亾dé卋堺 提交于 2019-11-28 22:38:06
This question already has an answer here: Why should C++ programmers minimize use of 'new'? 18 answers Understanding the meaning of the term and the concept - RAII (Resource Acquisition is Initialization) 11 answers Like the following code : int size = myGetSize(); std::string* foo; foo = new std::string[size]; //... // using the table //... delete[] foo; I heard that such use (not this code precisely, but dynamic allocation as a whole) can be unsafe in some cases, and should be used only with RAII. Why? I see three main problems with your code: Use of naked, owning pointers. Use of naked new

Get the size (in bytes) of an object on the heap

戏子无情 提交于 2019-11-28 20:51:01
I'm aware you can use MemoryLayout<T>.size to get the size of a type T . For example: MemoryLayout<Int32>.size // 4 However, for class instances (objects), MemoryLayout<T>.size returns the size of the reference to the object (8 bytes on 64 bit machines), not the size of the actual objects on the heap. class ClassA { // Objects should be at least 8 bytes let x: Int64 = 0 } class ClassB {// Objects should be at least 16 bytes let x: Int64 = 0 let y: Int64 = 0 } MemoryLayout<ClassA>.size // 8 MemoryLayout<ClassB>.size // 8, as well :( How can I get the size of the objects themselves? For those

How do I allocate a 2 D array with contigious memory ? How Do I use it to access rows and columns? Give me an example

六月ゝ 毕业季﹏ 提交于 2019-11-28 08:47:47
问题 I have created a 2 d array which reads as follows int i,j,lx,ly;// lx,ly are the row and column respectively double** a; a=(double**) malloc((lx+2)*sizeof(double)); a[0]= (double*) malloc((lx+2)*(ly+2)* sizeof(double)); assert(a[0]); for(i=1;i<lx+2;i++) { a[i]=a[i-1]+i*(ly+2); } // I allocate a value of 0 to all the elements in this array as below for(i=0;i<(lx+2)*(ly+2);i++) { a[i]=0; } // I print out all my elements below for(i=0;i<(lx+2)*(ly+2);i++) { printf("position %d values %d\n",i,a[i

size of dynamically allocated array

廉价感情. 提交于 2019-11-28 08:24:41
Is it true that a pointer assigned to the starting address of a dynamically allocated array does not have the information of the size of the array? So we have to use another variable to store its size for later processing the array through the pointer. But when we free the dynamically allocated array, we don't specify the size, instead we just "free ptr" or "delete [] ptr". How could free or delete know the size of the array? Can we use the same scheme to avoid storing the size of the array in another variable? Thanks! Yes, this is true. delete knows the size of the memory chunk because new

What is Dynamic Memory Allocation in C++?

自闭症网瘾萝莉.ら 提交于 2019-11-28 01:27:12
问题 I'm learning about Dynamic Memory Allocation in C++ and the keywords new and new[] are mentioned. It is said to enable users to specify the size of the memory allocation at runtime, unlike simply declaring a variable or array with a fixed size in the source code. I don't understand this concept. How does it work? I just need a clarification on the idea and an example would be helpful! 回答1: So, if you want an array of 10 integers, you'd be writing: int arr[10]; But what if you wanted to do

What is wrong with using arrays dynamically allocated in C++? [duplicate]

青春壹個敷衍的年華 提交于 2019-11-27 14:25:05
问题 This question already has an answer here: Why should C++ programmers minimize use of 'new'? 18 answers Understanding the meaning of the term and the concept - RAII (Resource Acquisition is Initialization) 11 answers Like the following code : int size = myGetSize(); std::string* foo; foo = new std::string[size]; //... // using the table //... delete[] foo; I heard that such use (not this code precisely, but dynamic allocation as a whole) can be unsafe in some cases, and should be used only

size of dynamically allocated array

China☆狼群 提交于 2019-11-27 02:11:18
问题 Is it true that a pointer assigned to the starting address of a dynamically allocated array does not have the information of the size of the array? So we have to use another variable to store its size for later processing the array through the pointer. But when we free the dynamically allocated array, we don't specify the size, instead we just "free ptr" or "delete [] ptr". How could free or delete know the size of the array? Can we use the same scheme to avoid storing the size of the array