dynamic-allocation

2D array dynamic memory allocation crashes [duplicate]

时光总嘲笑我的痴心妄想 提交于 2019-12-02 08:14:23
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: How do I correctly set up, access, and free a multidimensional array in C? I am trying to dynamically allocate memory for a 2D array using calloc. The columns are fixed as 2 so its only the rows that are dynamic. Here is what I have been trying : unsigned int **pts, rows; int main() { //some code pts = (unsigned int **)calloc(2*rows, sizeof (unsigned int **)); } //The code to access the array : for(k=1;k<=i;k++)

2D array dynamic memory allocation crashes [duplicate]

╄→尐↘猪︶ㄣ 提交于 2019-12-02 03:10:42
Possible Duplicate: How do I correctly set up, access, and free a multidimensional array in C? I am trying to dynamically allocate memory for a 2D array using calloc. The columns are fixed as 2 so its only the rows that are dynamic. Here is what I have been trying : unsigned int **pts, rows; int main() { //some code pts = (unsigned int **)calloc(2*rows, sizeof (unsigned int **)); } //The code to access the array : for(k=1;k<=i;k++) { printf("\nX%d=",k); scanf("%d",&pts[k][0]); printf("\nY%d=",k); scanf("%d",&pts[k][1]); } But the problem is, while accessing the array, the program crashes. I am

Create a multidimensional array dynamically in C++

只谈情不闲聊 提交于 2019-12-01 21:58:33
What is the good way (understand idiomatic/good practice) to dynamically create a multidimensional array in C++. For example let say I have tree integers w , h and d and I want to create an array MyEnum my_array[w][h][d] . (Of course w, h and d are not known at compile time). Is it best to use nested std::vector or use new or something ? Bonus question : Is it possible to set the dimension dynamically too ? bnaecker In general, nesting std::vector is not a great idea. It's usually a better plan to allocate memory which will hold the entirety of your multidimensonal array as a contiguous block,

Any way to prevent dynamic allocation of a class?

余生颓废 提交于 2019-12-01 16:37:05
I'm using a C++ base class and subclasses (let's call them A and B for the sake of clarity) in my embedded system. It's time- and space-critical, so I really need it to be kind of minimal. The compiler complains about lack of a virtual destructor, which I understand, because that can get you into trouble if you allocate a B* and later delete the pointer as an instance of A* . But I'm never going to allocate any instances of this class. Is there a way I can overload operator new() such that it compiles if there's no dynamic allocation of either class, but causes a compiler error if an end user

Any way to prevent dynamic allocation of a class?

眉间皱痕 提交于 2019-12-01 15:56:06
问题 I'm using a C++ base class and subclasses (let's call them A and B for the sake of clarity) in my embedded system. It's time- and space-critical, so I really need it to be kind of minimal. The compiler complains about lack of a virtual destructor, which I understand, because that can get you into trouble if you allocate a B* and later delete the pointer as an instance of A* . But I'm never going to allocate any instances of this class. Is there a way I can overload operator new() such that it

How to get the size of dynamically allocated 2d array

£可爱£侵袭症+ 提交于 2019-12-01 13:32:21
问题 I have dynamically allocated 2D array. Here is the code int **arrofptr ; arrofptr = (int **)malloc(sizeof(int *) * 2); arrofptr[0] = (int *)malloc(sizeof(int)*6144); arrofptr[1] = (int *)malloc(sizeof(int)*4800); Now i have to know that how many bytes are allocated in arrofptr,arrofptr[0],arrofptr[1]? is there any way to know the size? if we will print sizeof(arrofptr); sizeof(arrofptr[0]); sizeof(arrofptr[1]); then it will print 4. 回答1: You can't find size of arrofptr , because it is only a

Deallocating memory in a 2D array

安稳与你 提交于 2019-12-01 06:39:24
Suppose we have: int** myArray = new int*[100]; for(int i = 0; i < 100; i++){ myArray[i] = new int[3]; } What is the appropriate way to deallocate this array (which method below, if either is a correct way to do so)? 1. delete[] myArray; 2. for(int i = 0; i < 100; i++){ for(int j = 0; j < 3; j++){ delete myArray[i][j]; } } delete[] myArray; Intuitively it seems like we should do something like 2. since we want all of the memory we allocated to be deleted, but I'm not sure. You used one loop to create it, you should use one loop to delete it. The order is reversed to the order of allocation:

Finding size of dynamically allocated array

时光怂恿深爱的人放手 提交于 2019-11-30 16:38:28
问题 Why is it not possible to get the length of a buffer allocated in this fashion. AType * pArr = new AType[nVariable]; When the same array is deallocated delete [] pArr; the runtime must know how much to deallocate. Is there any means to access the length before deleting the array. If no, why no such API is provided that will fetch the length? 回答1: Is there any means to access the length before deleting the array? No. there is no way to determine that. The standard does not require the

Dynamically allocated 2 dimensional array

眉间皱痕 提交于 2019-11-30 15:52:08
问题 I am trying to build two dimensional array by dynamically allocating. My question is that is it possible that its first dimension would take 100 values, then second dimension would take variable amount of values depending on my problem? If it is possible then how I would access it? How would I know the second dimension's boundary? 回答1: (See the comments in the code) As a result you'll get an array such like the following: // Create an array that will contain required variables of the required

Does std::array<> guarantee allocation on the stack only?

别来无恙 提交于 2019-11-30 11:05:22
Is std::array<int,10> (without myself using new ) guaranteed to be allocated in the stack rather then the heap by the C++-Standard? To be clear, I do not mean new std::array<int, 10> . I mainly wonder, if the standard library is allowed to use new inside its implementation. I could not find more explicit answer in the standard, but [array.overview]/2 : An array is an aggregate ( [dcl.init.aggr] ) that can be list-initialized with up to N elements whose types are convertible to T . And [dcl.init.aggr]/1 : An aggregate is an array or a class (Clause [class] ) with no user-provided , explicit, or