dynamic pointers

Hello everyone, I'm asked to write a program copying an array into a dynamic pointer and I'm having trouble figuring out why in my for loop the pointer isnt working with the array. Any input would be appreciated.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
  int main()
{
	const int ARRAY = 10;//constant variable declared
	int miles[ARRAY] = { 22, 19, 27, 18, 16, 21, 99, 35, 77, 23 };//array declared
	int *travel = new int[ARRAY];//pointer declared

	cout << "Here are the following array elements copied into a pointer: \n\n";

	for (int i = 0; i < ARRAY; ++i)//pointer accessess array
	{
		cout << *travel = miles[i] << ", ";
	}

	delete[] travel;//delete memory space/no longer needed

	system("pause");
	return 0;
}
What do you mean by "isn't working"?

Do you mean what one compilers calls:
 In function 'int main()':
11:33: error: invalid operands of types 'int' and 'const char [3]' to binary 'operator<<'

You do have three operators and four operands on that line.
Lets call the operands A, B, C, and D.

You have:
A << B = C << D;

What is the precedence of these operators? In which order are they evaluated?

It seems clear, by the error, that the C << D evaluates before B = C.
The element of array (C) is int. The string literal ", " seems to be treated as const char [3].

It should be clear why those two operands cannot use operator<< that outputs something. The other operator<< is left-shift and does not match either.

You can change precedence with parentheses.


However, I do suggest that you write two loops.
* In the first do copy values from local array to dynamically allocated array.
* In the second print values from dynamically allocated array.

However, I do suggest that you write two loops.
* In the first do copy values from local array to dynamically allocated array.
* In the second print values from dynamically allocated array.


Do you mean likes this?

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
int main()
{
	const int ARRAY = 10;//constant variable declared
	int miles[ARRAY] = { 22, 19, 27, 18, 16, 21, 99, 35, 77, 23 };//array declared
	int *travel = new int[ARRAY];//pointer declared

	cout << "Here are the following array elements copied into a pointer: \n\n";

	/*for (int i = 0; i < ARRAY; ++i)//pointer accessess array
	{
		cout << (*travel = miles[i]) << ", ";
	}
	*/

	for (int i = 0; i < ARRAY; ++i)//pointer accessess array
	{
		*travel = miles[i];
	}

	for (int i = 0; i < ARRAY; ++i)//pointer accessess array
	{
		cout << (*travel = miles[i]);
	}

	delete[] travel;//delete memory space/no longer needed

	system("pause");
	return 0;
}


Also, if I just change the precedence with parentheses, does the code then logically make sense?

Thanks for your input!
Why do you have the = miles[i] in the second loop (line 22)?
If you did a proper copy on line 17, then there is no point to do it again.
Furthermore, now your print-loop does not separate printed values with comma and space.


Logically? Let me note that:
1
2
3
4
5
6
7
T* p = ...

*p
// is same as
*(p+0)
// is same as
p[0]
Hello,

You look like your on track with learning C++ but why not improve your skills with this course: https://www.udemy.com/learn-cpp-from-scratch/?couponCode=CPPFORUM

This course will help you improve your C++ course and the skills you will learn from it will help you with your future assignments I hope you find this course helpful
Ok, I think I got it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
int main()
{
	const int ARRAY = 10;//constant variable declared
	int miles[ARRAY] = { 22, 19, 27, 18, 16, 21, 99, 35, 77, 23 };//array declared
	int *travel = new int[ARRAY];//pointer declared

	cout << "Here are the following array elements copied into a pointer: \n\n";

	for (int i = 0; i < ARRAY; ++i)//pointer accessess array
	{
		(*travel = miles[i]);
	}

	for (int i = 0; i < ARRAY; ++i)//pointer accessess array
	{
		cout << (*travel) << ", ";
	}

	delete[] travel;//delete memory space/no longer needed

	system("pause");
	return 0;
}


When say logically, I mean the data stored in miles is being copied into the dynamically allocated memory pointer correctly.
The output our your current code is:
Here are the following array elements copied into a pointer: 

23, 23, 23, 23, 23, 23, 23, 23, 23, 23,  

Do you count that as "correct"?


On line 11 you have travel[0] = miles[i];.
In other words you do write only to the first element of the array that you did allocate on line 5.
You never touch the other 9 elements.

Each assignment overwrites the previous value of travel[0] and therefore travel[0] holds the last written value (23) after the loop.

Your print loop shows value from travel[0] ARRAY times.


If you do replace *travel with travel[i], then each iteration of the loops will access different element of the array. That might be what you want?
Ok, that was carelessness on my part.

I tried doing this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
int main()
{
	const int ARRAY = 10;//constant variable declared
	int miles[ARRAY] = { 22, 19, 27, 18, 16, 21, 99, 35, 77, 23 };//array declared
	int *travel = new int[ARRAY];//pointer declared

	cout << "Here are the following array elements copied into a pointer: \n\n";

	for (int i = 0; i < ARRAY; ++i)//pointer accessess array
	{
		*travel = miles[i];
		travel++;
	}

	for (int i = 0; i < ARRAY; ++i)//pointer accessess array
	{
		cout << *travel << ", ";
	}

	delete[] travel;//delete memory space/no longer needed

	system("pause");
	return 0;
}


And got this message: "ConsoleApplication26.exe has triggered a breakpoint" and something about no symbols being loaded.

When I replaced *travel with travel[i] I got junk values

A new situation.

The new int[ARRAY] allocates a block of memory dynamically that is large enough to hold ARRAY integers. The block is effectively an array of ARRAY integers. It has no name, but lets call it 'fubar'.

The new returns the address of the memory block. The address of integer fubar[0]. You store that address into pointer 'travel'.

After line 5 travel == &(fubar[0]).

On first time on line 11 you do read integer value from miles[0] and write it to *travel. To integer at travel. To travel[0]. To fubar[0].

Then, on line 12 you do change the travel. You update it to point to fubar[1].
Therefore, after the first iteration of the loop travel == &(fubar[1]).

Q: Where does the travel point to after the loop, at line 14?
A: travel == &(fubar[ARRAY]) == fubar + ARRAY

Alas, the fubar has only ARRAY elements, and the last of them is fubar[ARRAY-1].

The fubar + ARRAY is not within the fubar block. It is after the block. We have no idea what is in there.


The second loop prints (the undefined junk) value of fubar[ARRAY] ARRAY times. You read from address that has not been allocated to you. That is an out of range error.


On line 20 you want to deallocate an "array of integers" memory block that starts from address fubar + ARRAY. You have no allocated block at that address. Trying to give away something that isn't yours can and should crash.

The block that you have is at address travel - ARRAY.


1
2
3
4
5
6
7
8
9
for ( int i = 0; i < ARRAY; ++i )
{
 travel[i] = miles[i];
}

for ( int i = 0; i < ARRAY; ++i )
{
  cout << travel[i] << ", ";
}


You can use pointers too, but you may not lose the pointer that points to the start of the block in the process:
1
2
3
4
for ( int* tram = travel; tram != (travel+ARRAY); ++tram )
{
  cout << *tram << ", ";
}

Thanks for the informative reply. Im gonna do some more tweaking with this after work and post an update.
now that you have done it the brute force way, take a look at

std::copy() //prefer this one
and the older C-ish one is memcpy (avoid it, but know that its similar when you see it in old code).

Topic archived. No new replies allowed.