dynamic-arrays

How to properly work with dynamically-allocated multi-dimensional arrays in C++ [duplicate]

落花浮王杯 提交于 2019-11-27 15:58:49
This question already has an answer here: How do I declare a 2d array in C++ using new? 23 answers How do I define a dynamic multi-dimensional array in C++? For example, two-dimensional array? I tried using a pointer to pointer, but somehow it is failing. The first thing one should realize that there is no multi-dimensional array support in C++, either as a language feature or standard library. So anything we can do within that is some emulation of it. How can we emulate, say, 2-dimensional array of integers? Here are different options, from the least suitable to the most suitable. Improper

c++ dynamic array initialization with declaration

北战南征 提交于 2019-11-27 14:31:38
I have function like this: void findScarf1(bool ** matrix, int m, int n, int radius, int connectivity); and in main function I create 2d dynamic array to pass in this function bool matrix[6][7] = { {0, 0, 1, 1, 1, 0, 0}, {0, 0, 1, 1, 1, 0, 0}, {0, 0, 1, 1, 1, 0, 0}, {0, 0, 1, 1, 1, 0, 0}, {0, 0, 1, 1, 1, 0, 0}, {0, 0, 1, 1, 1, 0, 0} }; The problem is: findScarf1(matrix, 6, 7, 3, 4); causes error C2664: 'findScarf1' : cannot convert parameter 1 from 'bool [6][7]' to 'bool **' How to initialize array compactly(simultaneously with declaration)? p.s. sorry if it's duplicate question but I've spent

C qsort() with dynamic n by 2 multi-dimensional array

谁说我不能喝 提交于 2019-11-27 14:13:48
问题 First, I defined a dynamic array with 2 columns and 10 row. The integer number is set to 10 here just for example. int** array; int number = 10; array = malloc(number * sizeof(int*)); for (i = 0; i < number; i++) array[i] = malloc(2 * sizeof(int)); Then I try to use qsort() on it. qsort( array, number, sizeof array[0], compare ); This is my compare function. It sorts by the integer values in the first column, then sorts by the second column while preserving the order in the first column. E.g.

C++ doesn't tell you the size of a dynamic array. But why?

丶灬走出姿态 提交于 2019-11-27 13:23:11
问题 I know that there is no way in C++ to obtain the size of a dynamically created array, such as: int* a; a = new int[n]; What I would like to know is: Why? Did people just forget this in the specification of C++, or is there a technical reason for this? Isn't the information stored somewhere? After all, the command delete[] a; seems to know how much memory it has to release, so it seems to me that delete[] has some way of knowing the size of a . 回答1: You will often find that memory managers

How do I declare an array when I don't know the length until run time?

谁说胖子不能爱 提交于 2019-11-27 12:20:51
I originally had an array[1..1000] that was defined as a global variable. But now I need that to be n, not 1000 and I don't find out n until later. I know what n is before I fill the array up but I need it to be global therefore need a way to define the size of a global array at run time. Context is filling an array with a linear transformation of the bytes in a file. I don't know how big the file is until someone wants to open it and the files can be of any size. As of Delphi 4, Delphi supports dynamic arrays . You can modify their sizes at run time and they will retain the data you stored in

Dynamically allocating array explain

不问归期 提交于 2019-11-27 11:23:30
问题 This is sample code my teacher showed us about "How to dynamically allocate an array in C?". But I don't fully understand this. Here is the code: int k; int** test; printf("Enter a value for k: "); scanf("%d", &k); test = (int **)malloc(k * sizeof(int*)); for (i = 0; i < k; i++) { test[i] = (int*)malloc(k * sizeof(int)); //Initialize all the values } I thought in C, to define an array you had to put the [] after the name, so what exactly is int** test ; isn't it just a pointer to a pointer?

How do I get an empty array of any size in python?

99封情书 提交于 2019-11-27 09:49:28
问题 I basically want a python equivalent of this in C: int a[x]; but in python I declare an array like: a = [] and the problem is I want to assign random slots with values like: a[4] = 1 but I can't do that with python, since the array is empty. 回答1: If by "array" you actually mean a Python list, you can use a = [0] * 10 or a = [None] * 10 回答2: You can't do exactly what you want in Python (if I read you correctly). You need to put values in for each element of the list (or as you called it, array

Declaring a dynamic array not working as expected

╄→尐↘猪︶ㄣ 提交于 2019-11-27 08:30:34
问题 RsProxyList.Open objDBCommand,,1,1 dim recCount:recCount = RsProxyList.RecordCount Dim output(recCount,2) I get an error because recCount is of wrong type. I have tried to convert it to Int but that does not work either. The following works fine: RsProxyList.Open objDBCommand,,1,1 dim recCount:recCount = RsProxyList.RecordCount Dim output(3,2) How do I convert recCount to get this array declaration to work? 回答1: You need to first declare your Array as dynamic then use ReDim to set the first

What is the status on dynarrays?

耗尽温柔 提交于 2019-11-27 06:13:03
问题 gcc 4.9 now has support for n3696 (Runtime-sized arrays with automatic storage duration). n3662 says: In N3497 Runtime-sized arrays with automatic storage duration, Jens Maurer proposes arrays with runtime bound. These arrays are to std::dynarray as normal fixed-size arrays are to std::array. In this mailing list, Jonathan Wakely says: We should add a C++14 status table to the manual but in the meantime here's a quick summary of the library status. ... These ones are missing: N3672 A proposal

Why does C++ allow variable length arrays that aren't dynamically allocated?

我们两清 提交于 2019-11-27 05:35:54
I'm relatively new to C++, and from the beginning it's been drilled into me that you can't do something like int x; cin >> x; int array[x]; Instead, you must use dynamic memory. However, I recently discovered that the above will compile (though I get a -pedantic warning saying it's forbidden by ISO C++). I know that it's obviously a bad idea to do it if it's not allowed by the standard, but I previously didn't even know this was possible. My question is, why does g++ allow variable length arrays that aren't dynamically allocated if it's not allowed by the standard? Also, if it's possible for