Pointer and Memory Questions.

Hello, I've wrote a simple program that uses pointers and dynamic memory to create
an array with an element size the user enters. I have a couple questions about how it works.

Firstly, here is the program.

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
41
42
43
44
45
46
47
48
49
50
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>

using namespace std;

//Forward declarations
void RandomArrayFill(int* array, int size);
int* ArrayCreation(int* array, int size);

int main()
{
	//Seed random number generator
	srand(time(0));

	int* myArr = 0;
	int arraySize = 0;

	cout << "Please enter the size of the array: ";
	cin >> arraySize;

	myArr = ArrayCreation(myArr, arraySize);

	RandomArrayFill(myArr, arraySize);
	
	cout << endl;
	for (int x = 0; x < arraySize; ++x)
		cout << myArr[x] << " ";

	cout << endl;
	system("Pause");
}

int* ArrayCreation(int* array, int size)
{
	//Creates new array variable with size user specified
	int* newArray = new int[size];

	delete[] array;
	array = 0;

	return newArray;
}

void RandomArrayFill(int* array, int size)
{
	for (int x = 0; x < size; ++x)
		array[x] = rand() % 101;
}


On line 40 when I delete the array used as a parameter in the function, my understanding is when I call the function and use myArr, another pointer is created. After that it is not used in the function from what I can tell.

So what was the point in creating it in the first place?
Couldn't I had just created the function and make it use just the size parameter?

Also when the copy is made does it point to a different memory location that has the same value as the one myArr points to or does it point to the same memory location, which would mean I would have 2 pointers going to the same place?

Also on line 17 when = 0 is used I'm assuming that means the pointer isn't pointing to any memory at that moment. Is that correct?

Thanks.
On line 40 when I delete the array used as a parameter in the function, my understanding is when I call the function and use myArr, another pointer is created. After that it is not used in the function from what I can tell.

So what was the point in creating it in the first place?
Yes, another pointer is created. This pointer points to the same memory of the argument in the function call.
It is used only to delete[] the memory pointed by it, and since the original also points to that memory it's like calling delete[] on that.
On line 41 you then set the copy to null (see below), but this is actually unnecessary because the copy isn't used again in the function and the original pointer is still pointing to the old (now invalid) memory.

Couldn't I had just created the function and make it use just the size parameter?
Yes, but in that case you'd have to delete[] the pointer before calling the function. Otherwise the old memory becomes unreachable, therefore undeletable.
In your case it's not necessary because myArr doesn't point to any allocated memory.

Also when the copy is made does it point to a different memory location that has the same value as the one myArr points to or does it point to the same memory location, which would mean I would have 2 pointers going to the same place?
It points to the same memory location. Like I said, this is done to allow the function to free the memory pointed by the original array (doing so using the copy, that points to the same memory). It goes without saying that the pointer argument is also supposed to be assigned to the return value of the function, otherwise it keeps pointing to the deleted memory.

Also on line 17 when = 0 is used I'm assuming that means the pointer isn't pointing to any memory at that moment. Is that correct?
That assignment does exactly what you read: it assigns the pointer to the 0th memory address.
In most, if not all, code you'll ever see it's the only case where a memory address is explicitly used. Usually you assign using other pointers or to the return value of a new statement.
The 0th address, also called null (Null, NULL, nil, nullptr in C++11) is special because it is always invalid. No memory will ever be allocated starting from 0, so assigning a pointer to null means that that pointer is not pointing to anything.
Being invalid, trying to access it will result in an error (segmentation fault, segfault).

Pointers should never point to deallocated memory. So after you delete them, either assign them to new memory or set them to null to signal they are not pointing to anything (unless you are SURE you will not use them again EVER).
Deleting a pointer to invalid memory usually causes an error, unless it points to null, in which case it just does nothing.
Thanks.

Just to make sure I understand,

let's say myArr points to memory block 42 and newArray points to block 56. When the function is called the array pointer also points to block 42. When delete[] is used on array everything in block 42 is deleted. the new integer array created is put in block 56. Then the return statement makes myArr point to 56. And if I don't use myArr as an argument, it would still end up pointing to block 56 but 42 would still be full of data.

Also why is delete[] used instead of just delete because I don't specifically say that array or even myArr are arrays. Wouldn't the compiler/computer just think they are regular variables just named array?
let's say myArr points to memory block 42 and newArray points to block 56. When the function is called the array pointer also points to block 42. When delete[] is used on array everything in block 42 is deleted. the new integer array created is put in block 56. Then the return statement makes myArr point to 56. And if I don't use myArr as an argument, it would still end up pointing to block 56 but 42 would still be full of data.
Correct.
To semplify, you can see the pointer parameter as "the function deletes this".

Also why is delete[] used instead of just delete
When you new[] an array you need to pass its size, but when deleting you don't need to. This is possible because with new[] the size information is saved in a magic place known by the compiler and retrieved by delete[].
With normal newdelete this magic info is not saved nor retrieved (it's always 1). So you always need to match the two versions to ensure ALL the memory is actually deallocated.

because I don't specifically say that array or even myArr are arrays
Actually you do. You say they are arrays the moment you use new[] instead of new

Wouldn't the compiler/computer just think they are regular variables just named array?
It does. That's why it won't give you a compilation error if you use mismatched operations (but the program will be bugged nonetheless).
It's up to the programmer to remember if his variables are arrays or single objects and use the appropriate delete version.


I'm not sure I managed to explain the delete[] thing clearly enough...
Last edited on

Actually you do. You say they are arrays the moment you use new[] instead of new

How would that make myArr and array arrays? I use new[] on newArray.


I'm not sure I managed to explain the delete[] thing clearly enough...

Clear enough for me, which is saying something, lol

Also, this has nothing to do with programming but, how do you get keywords like new and delete blue when posting here?
How would that make myArr and array arrays? I use new[] on newArray.
Yeah, I probably worded that awfully. Let's try like this:
An array is a sequence of object, identified by a name referencing the first object.
A pointer is a reference to an object, which may or may not be the first of a sequence.
1
2
int arr[5] = {0}; //actual array: sequence of 5 objects. The first is referenced by the variable 'arr'
int* ptr = arr; //'ptr' becomes a reference to the first element of the sequence 
So you can see that both the pointer and 'arr' are just "names that refer to the first object in a sequence".

Now, 'newArray' is an array (referencing first object of sequence). When you return it, 'myArr' becomes a reference to the first object in a sequence.
Assuming you called the function again, 'array' is a copy of 'myArr' so it's a reference to the first object in a sequence.

Actually, in your case, when the function is called the parameter is a null pointer, not an array.
I didn't consider it because the only time the function is called its argument is 'myArr', which is used only to hold an array (pointing to null kind of doesn't count), so I mentally translated that to 'array' being an array.
Though 'myArr' becomes an array only AFTER the function call.


Was that better?

Also, this has nothing to do with programming but, how do you get keywords like new and delete blue when posting here?
I put them in code tags. One liners are not put on their own line.

Edit:
A more practical answer to
Also why is delete[] used instead of just delete
would be "because the function expects the parameter to be an array to delete". If the function purpose was to delete a single object you'd find delete.
Last edited on
If 'array' isn't a array and 'myArr' isn't until after the function call, then why is delete[] used? Or should I have used delete instead in my little program?
See edit above.
In your little program delete is completely unnecessary in either form. You call ArrayCreation() only once with an argument that doesn't need to be deleted.
Last edited on
I get it now. It doesn't need to be deleted because it is null and doesn't point to anything. If I called the function more than once it would need to be deleted because then it would otherwise leave the array created after the first call in memory.
Exactly.

Sorry about the roundabout explanation, it was like 7 am and I couldn't get my ideas straight.
Last edited on
Topic archived. No new replies allowed.