Pointers & new

Hey guys , please see the code
1
2
3
4
5
6
  int array[10] {1,2,3,4,5,6,7,8,9,10};

int *ptr= new int[5];
ptr=array;



if we're talking about dynamic array, there's 5 spaces in the memory for integers , but the array has 10 elements ?

does that mean it stores 5 integers from the array and when I use delete , it get rid of the 5 elements ?
The memory address of ptr is same as array after ptr=array. In other words ptr points to the memory address of array after ptr=array. So ptr will be same as array. So if you change something in array, the same change would be seen in ptr.

Try running this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>

using namespace std;

int main()
{
	int array[10] = {1,2,3,4,5,6,7,8,9,10};

	int *ptr=new int[5];
	ptr=array;

	for(int i = 0; i<10; i++)
	{
		array[i] = 0; //We change the value in array
		cout << ptr[i] << endl; //And see the same change in ptr
	}

	return 0;
}
Last edited on
There is a memory leak here:
1
2
3
4
int array[10] {1,2,3,4,5,6,7,8,9,10};

int *ptr= new int[5];
ptr=array;

At line 3, the program requests memory to store 5 integers, and the start address of that block of memory is placed in ptr. Then at line 4, ptr is reassigned to point to something else. The block of 5 integers previously requested is now lost, it cannot be accessed or deleted, as the address has been replaced with a different value.
int array[10] = { ... };
This allocates 10 consecutive integers and makes a pointer array which will point to the first element.

int *ptr = new int[5];
This allocates 5 consecutive integers and makes a pointer ptr which will point to the first element.

ptr = array; prt now points to the same integer as array. Nothing points to the old integer anymore and now we can't get it back. This is a memory leak.
so if I wanted a dynamic array I use what I used in line 3 only ? and modify the values as if I wanted to modify an array ?

Thanks guys ^^
Also can I delete only one of the array ?

for example this :


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

int main(){


	int *ptr = new int[3];

	*ptr = 5;

	ptr[1] = 6;

	ptr[2] = 3;


	delete [1]ptr;
	

	getchar();
	getchar();
	return 0;
}


can I just delete element 1 ?
Last edited on
No.

You delete what you new'd. If you new[] 3 items, then you must delete[] 3 items.
Let's say we have
1
2
3
4
5
int* ptr = new int[3];
// Do stuff with ptr, e.g.
ptr[0] = 1;
ptr[1] = 2;
ptr[2] = 3;

Technically speaking, this is perfectly valid syntax:
1
2
// "delete" element 1???
delete (ptr + 1); // !! 

But I have no clue what sort of undefined behavior that may or may not cause (so don't try it).
The only thing you can safely delete is the whole thing, via
delete[] ptr;.

What exactly are you trying to accomplish by deleting element 1?
Do you want
1
2
3
4
/* "delete" element 1 here */
std::cout << ptr[0] << '\n'; // 1
std::cout << ptr[1] << '\n'; // ??? (undefined)
std::cout << ptr[2] << '\n'; // 3 
or do you want
1
2
3
4
5
/* "delete" element 1 here */
std::cout << ptr[0] << '\n'; // 1
std::cout << ptr[1] << '\n'; // 3 (original element 2 gets shifted down a spot)
std::cout << ptr[2] << '\n'; // ??? (undefined; the array is 1 element smaller,
                             //      so index 2 is out-of-bounds now) 
or even this?
1
2
3
4
/* "delete" element 1 here */
std::cout << ptr[0] << '\n'; // 1
std::cout << ptr[1] << '\n'; // 0 (element replaced with default value)
std::cout << ptr[2] << '\n'; // 3 

I don't see any reason you would want to do the first one.
For the second one, you can use a loop (or standard library function) to shift the other elements over a spot and then simply "forget about" the value of the last element in your program.
For the third one, you can just write ptr[1] = int();.
thank you guys.
Topic archived. No new replies allowed.