dynamic-memory-allocation

When to use Malloc instead of New [duplicate]

余生颓废 提交于 2019-12-13 12:16:35
问题 This question already has answers here : Closed 10 years ago . Duplicate of: In what cases do I use malloc vs new? Just re-reading this question: What is the difference between "new" and "malloc" and "calloc" in C++? I checked the answers but nobody answered the question: When would I use malloc instead of new? There are a couple of reasons (I can think of two). Let the best float to the top. 回答1: A couple that spring to mind: When you need code to be portable between C++ and C. When you are

The array is static, but the array size isn't know until runtime. How is this possible?

三世轮回 提交于 2019-12-13 12:03:05
问题 This has been troubling me for a while. It goes to the heart of my (lack of) understanding of the difference between static and dynamic memory allocation. The following array is an ordinary static array, which should mean the memory is allocated during compile time, correct? Yet, I've set it up so that the user enters the array size at runtime. #include <iostream> using namespace std; int main() { cout << "how many elements should the array hold? "; int arraySize; cin >> arraySize; int arr

How to declare a variable size 2D array in C?

南笙酒味 提交于 2019-12-13 12:03:02
问题 I have a problem with a project. I have to make a variable size 2D array for storing some prediction error..so this is about images. The trouble is that I have to load images of different sizes so for each image I would have to get into a file the 2D array with the coresponding number of pixels..I've searched among your questions but it's not what I'm looking for.Can anyone help me? Thank you 回答1: If you have a modern C compiler (at least C99) in function scope it is as simple as: unsigned

dynamic allocation of 2d array function

江枫思渺然 提交于 2019-12-13 10:05:12
问题 So I have a program in C structured in 3 files: main.c , alloc.h and alloc.c . In the main.c function, I have the declaration of a pointer to another pointer to which I intend to alloc an n * m array: #include <stdio.h> #include <stdlib.h> #include "alloc.h" int main() { int **mat, n, m; alloc_matrix(&mat, int &n, int &m); return 0; } In alloc.c I have the following declarations: #ifndef ALLOC_H_INCLUDED #define ALLOC_H_INCLUDED #include <stdio.h> #include <stdlib.h> void alloc_matrix(int***,

Stack, heap and dynamic memory allocation

梦想的初衷 提交于 2019-12-13 08:15:24
问题 I have some confusion about these three concepts: Stack, Heap, and Dynamic memory allocation. I'll provide examples in C++. Am I correct to say that given a program, for all its variables, arrays, and maybe objects on stack, when the program just starting, all the memory space needed is already there, so everything is predetermined? But when the program is running, for me it still sounds like "dynamic" since a stack is still changing in a sense that values are still push into, pop off the

Bubble sort using only pointers and dynamic memory allocation

最后都变了- 提交于 2019-12-13 07:48:34
问题 I am trying to run Bubble Sort Technique using pointers and dynamic memory allocation, but the code doesn't seem to run (eclipse crashes). Please help. I am posting the code below: #include<iostream> using namespace std; void sort(int *); //=========================================== int main() { int *a = new int[5]; int *c = a; cout << "Enter the numbers\n"; for(int i = 0; i < 5; i++) { cin >> *a; a++; } a = a - 4; sort(a); cout << c; cout<<"\nSorting complete"; cout<<"\n Array after sorting

C error - free(): invalid next size (fast)

对着背影说爱祢 提交于 2019-12-13 03:57:18
问题 I am not sure why I am getting the below error. Strangely enough, this error doesn't occur when I use Mac OS X, but it does when I use my Linux (Debian) partition. ----------- Empty Queue: 0 0 0 0 0 0 0 0 0 0 --------------- Populated Queue: 5 3 1 7 6 3 2 1 4 4 ------------- After Dequeue: 3 1 7 6 3 2 1 4 4 0 Datum: 5 *** glibc detected *** ./queue_demo: free(): invalid next size (fast): 0x0000000000c73010 *** ======= Backtrace: ========= /lib/x86_64-linux-gnu/libc.so.6(+0x75b76)

Is it practically OK to delete object not constructed using the new expression?

荒凉一梦 提交于 2019-12-12 10:39:09
问题 Pedantically, this may not be OK. As per cppref: If expression is anything else, including if it is a pointer obtained by the array form of new-expression, the behavior is undefined. Putting that aside, is the following code OK in practice ( T is non-array, and assuming that new is not replaced)? auto p = (T*)operator new(sizeof(T)); new(p) T{}; delete p; It is said that in cppref that When calling the allocation function, the new-expression passes the number of bytes requested as the first

How to return dynamic object from operator function?

随声附和 提交于 2019-12-12 10:17:25
问题 I am quite confused about this. How to return a dynamically allocated object from operator function? Consider following example: #include "stdafx.h" #include <iostream> #include "vld.h" using std::cout; class Point { public: Point(int x,int y) : a(x),b(y) { } Point() { } Point operator + (Point p) { Point* temp=new Point(); temp->a=a+p.a; temp->b=b+p.b; Point p1(*temp); // construct p1 from temp delete temp; // deallocate temp return p1; } void show() { cout<<a<<' '<<b<<'\n'; } private: int a

C++ New vs Malloc for dynamic memory array of Objects

我怕爱的太早我们不能终老 提交于 2019-12-12 08:58:08
问题 I have a class Bullet that takes several arguments for its construction. However, I am using a dynamic memory array to store them. I am using C++ so i want to conform to it's standard by using the new operator to allocate the memory. The problem is that the new operator is asking for the constructor arguments when I'm allocating the array, which I don't have at that time. I can accomplish this using malloc to get the right size then fill in form there, but that's not what i want to use :) any