Allocate new array and copy

Hello,

I want to create third array with size of first and second.
But it doesn't work.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
using namespace std;

int main()
{
	int first[] = { 4,15,19,32,44 };
	int second[] = { 2,5,6,9,22,27,43,55,77,54 };

	int FirstLength = sizeof(first) / sizeof(*first);
	int SecondLength = sizeof(second) / sizeof(*second);

	int* Third;
	Third = new int[FirstLength + SecondLength] ;

	for (int i = 0; i < FirstLength; i++)
	{
		Third[i] = first[i]; // It doesn't work
	}

}
Hello Shervan360,

When you say it does not work be a little more specific about what does not work. Also you might explain the problem better.

I believe the problem is with the length variables. "size of (*first) is de-referencing the address of element (0) zero to use whatever value is there.

I think what you are looking for and what I have seen most often is "sizeof(first[0])".

Try that and I will load the program up and give it a test.

Hope that helps,

Andy
Thank you,
The first array has 5 elements. The second has 10 elements.
The third array size should be 15 and I want to copy the first array to the third array.


What do you mean "doesn't work"?

Seems fine here.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
(gdb) b 15
Breakpoint 1 at 0x400838: file foo.cpp, line 15.
(gdb) run
Starting program: ./a.out 

Breakpoint 1, main () at foo.cpp:15
15		for (int i = 0; i < FirstLength; i++)
(gdb) p FirstLength 
$1 = 5
(gdb) p SecondLength 
$2 = 10
(gdb) n
17			Third[i] = first[i]; // It doesn't work
(gdb) 
15		for (int i = 0; i < FirstLength; i++)
(gdb) 
17			Third[i] = first[i]; // It doesn't work
(gdb) 
15		for (int i = 0; i < FirstLength; i++)
(gdb) 
17			Third[i] = first[i]; // It doesn't work
(gdb) 
15		for (int i = 0; i < FirstLength; i++)
(gdb) 
17			Third[i] = first[i]; // It doesn't work
(gdb) 
15		for (int i = 0; i < FirstLength; i++)
(gdb) 
17			Third[i] = first[i]; // It doesn't work
(gdb) 
15		for (int i = 0; i < FirstLength; i++)
(gdb) 
20	}
(gdb) p first
$3 = {4, 15, 19, 32, 44}
(gdb) p second 
$4 = {2, 5, 6, 9, 22, 27, 43, 55, 77, 54}
(gdb) p *Third@15
$7 = {4, 15, 19, 32, 44, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
(gdb) 

Looks exactly like I would expect, and what you would seem to want.
Last edited on
Hello Shervan360,

I was surpirsed that "sizeof(*first)" worked. Learned something new.

I have yet to duplicate any problem that you are trying to refer to. When I added this code everything printed out.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
std::cout << "\n First array ";

for (int lc = 0; lc < FirstLength; lc++)
{	
	std::cout << first[lc] << ", ";
}
	
std::cout << "\n Third array ";

for (int lc = 0; lc < FirstLength; lc++)
{
	std::cout << Third[lc] << ", ";
}

std::cout<<std::endl;

Not the best way to print the arrays, but it works for now. The output I receive is:

 First array 4, 15, 19, 32, 44,
 Third array 4, 15, 19, 32, 44,



What you might be seeing is the "Third" is a pointer to the beginning of the array and only shows the first element not the whole array.

You have used "new" to create a new array, but you did not use "delete" to delete or free the memory when you are done with it. As I understand it this is a memory leak.

Hope that helps,

Andy
Hello Shervan360,

Another test you can do is:
1
2
3
4
5
6
7
8
9
10
Third = new int[FirstLength + SecondLength];  // <--- Existing code.

std::cout << "\n Third array contains: ";

for (size_t index = 0; index < FirstLength+SecondLength; index++)
{
	//Third[index] = 0;

	std::cout << Third[index] << (index < (FirstLength + SecondLength - 1) ? ", " : "\n");
}

This will let you see that "Third" does contain 15 elements. The problem is that it contains all garbage values.

What you can do is uncomment line 7 and put a comment on line 9 and this will initialize the array.

Hope that helps,

Andy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>

int main()
{
   int first[]  { 4,15,19,32,44 };
   int second[] { 2,5,6,9,22,27,43,55,77,54 };

   int FirstLength  { sizeof(first) / sizeof(*first) };
   int SecondLength { sizeof(second) / sizeof(*second) };

   int* Third { new int[FirstLength + SecondLength] };

   for (int i = 0; i < FirstLength; i++)
   {
      Third[i] = first[i];
   }

   for (int i { }; i < SecondLength; i++)
   {
      Third[i + FirstLength] = second[i];
   }

   for (int i { }; i < FirstLength + SecondLength; i++)
   {
      std::cout << Third[i] << ' ';
   }
   std::cout << '\n';

   delete[] Third;
}

4 15 19 32 44 2 5 6 9 22 27 43 55 77 54


C++17 makes it easy to determine the size of C arrays on the stack, std::size. Need to include <iterator>.

9
10
   int FirstLength  { std::size(first) };
   int SecondLength { std::size(second) };


std::size will not work on arrays on the heap, or arrays passed into functions.
Topic archived. No new replies allowed.