Putting a 1D array into a 2D array?

Mar 11, 2015 at 6:34pm
I have an array, say:

RandArray [30000]

and i want to put the values stored in this array into my new array

NewArray [5][6000]

would i do this by nesting... 3 for loops? one with int < 5, another with int < 6000 and finally int < 30000?
Mar 11, 2015 at 8:11pm
Mathematically speaking, there is little to no difference between the two. You can use a mathematical expression to index into the 1D array as if it were 2D. In fact, there is no such thing as a 2D array - your compiler converts 2D arrays into 1D arrays and does the math for you.
Mar 11, 2015 at 8:43pm
do you want to copy the data or not, that is, do you want to use both arrays after the operation?

if not, you could point to the desired areas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
	int array[30000] = { 200, 400, 600 };
	array[6001] = 6;
	array[12001] = 12;
	array[18001] = 18;
	array[24001] = 24;
	array[29999] = 999;

/// Note: just do this if you know how to use pointers,
///          otherwise learn what pointers are before using them

	int** pointer = new int*[5]; 
        for(int i = 0; i < 5; ++i)
            pointer [i] = &array[6000*i];

	std::cout << pointer[0][2] << ' ' << pointer[1][1] << ' ' 
                      << pointer[2][1] << ' ' << pointer[3][1] << ' ' 
                      << pointer[4][1] << ' ' << pointer[4][5999] << std::endl;


If you have to copy the values you can use memcpy (short for memory copy)
memcpy is located in <cstring>
1
2
3
4
5
6
7
8
9
10
11
12
13
	int array[30000] = { 200, 400, 600 };
	array[6001] = 6;
	array[12001] = 12;
	array[18001] = 18;
	array[24001] = 24;
	array[29999] = 999;

	int NewArray[5][6000];
	memcpy(&NewArray, &array, sizeof(NewArray));

	std::cout << NewArray[0][2] << ' ' << NewArray[1][1] << ' ' 
                      << NewArray[2][1] << ' ' << NewArray[3][1] << ' ' 
                      << NewArray[4][1] << ' ' << NewArray[4][5999] << std::endl;
Last edited on Mar 11, 2015 at 8:55pm
Mar 11, 2015 at 10:17pm
LB wrote:
In fact, there is no such thing as a 2D array - your compiler converts 2D arrays into 1D arrays and does the math for you.


Eh? Maybe some compilers might optimize it into a 1D array with optimizations enabled, but the traditional implementation involves an array of pointers, with each pointer pointing to an array of the desired element.

Whoops. Needless to say, I haven't touched this stuff in a while.

-Albatross
Last edited on Mar 11, 2015 at 11:51pm
Mar 11, 2015 at 11:25pm
Eh? Maybe some compilers might optimize it into a 1D array with optimizations enabled, but the traditional implementation involves an array of pointers, with each pointer pointing to an array of the desired element.


No ma'am. An array of arrays is very different from an array of pointers to arrays.

Arrays guarantee that items are stored consecutively. LB is correct... if you have a 'natural' 2D array (as in... int x[5][5];, then all elements are crammed together consecutively just as they would be in a 1D array.
Mar 11, 2015 at 11:32pm
gcc 4.9.2 with -O0: http://goo.gl/SSRGnQ
clang 3.7 with -O0: http://goo.gl/hgvulQ
Last edited on Mar 11, 2015 at 11:35pm
Mar 11, 2015 at 11:44pm
Ah. Gotcha. Thanks for letting me know about that detail.

-Albatross
Mar 12, 2015 at 7:29am
What happens when there is nowhere enough place to store all elements consecutively?

Does it throw an bad_alloc error or does it allocate the memory in different blocks?
Last edited on Mar 12, 2015 at 7:53am
Mar 12, 2015 at 5:01pm
Unless you use dynamic memory, arrays are allocated on the stack. You will get a compilation error, and the program executable will not be built.

http://ideone.com/qgczoQ
Mar 12, 2015 at 5:53pm
Oh yeah, I forgot that part.
When compiling the programm increases in size with Objects on the stack.

thanks
Mar 12, 2015 at 6:09pm
LB wrote:
You will get a compilation error, and the program executable will not be built.


Not necessarily. It's possible to exhaust stack space without it being detected at compile time.
Mar 12, 2015 at 6:16pm
Indeed: http://ideone.com/YYCpct (some tricks used to prevent optimization)

The reason I did not originally consider this case was because I misunderstood the question - whoops.
Topic archived. No new replies allowed.