1

Can someone explain how an array of pointers implementation of c++ dynamically?

Is the below code correct?

If so,

 int *ptr[5]; 

 for (int i = 0; i < 5; i++)
  {

  int size = 2;

  ptr[i] = new int [size] ;

 //*(ptr + i) = new int[size]; this is the same as above line

  cout << &ptr[i] << endl;   ----------> line 1
  cout << ptr[i] << endl; -----------> line 2
  }

What is actually printing in line 1 and 2 ?

this is the addresses i get for line 1

0x7fff88f805d0
0x7fff88f805d8
0x7fff88f805e0
0x7fff88f805e8
0x7fff88f805f0

this is the addresses I get for line 2

0x55f946348ef0
0x55f946349330
0x55f946349360
0x55f946349390
0x55f9463493c0

Can somebody explain this whole mess of pointer arrays.

4
  • 1
    Theelements in each array seem to be equally spaced in both cases. How far do you think 78 is from 80 in hexadecimal? Commented Apr 30, 2020 at 21:21
  • 1
    int *ptr[i] = new int[10]; no... You declare int *ptr [5]; (five pointers), In your loop you want ptr[i] = new int[10];, the [..] acts as a dereference on its own, e.g. *(ptr + i). Commented Apr 30, 2020 at 21:23
  • Well, the addresses seem to implied that the items are allocated in some kind of order. However, more likely they are random and come from random memory. In your case, the program is "fresh" and so they appear to have an order, but in fact, each "new" could return something that isn't in any way related to any of the other "new"s. The size isn't an exact match because the "new" has its own overhead for allocating and deallocating the item. Additionally, some compilers have alignment issues to worry about which again obfuscates why a particular address is chosen and its apparent size. Commented Apr 30, 2020 at 21:30
  • @cigien, I got the point thanks for pointing the hexadecimal mixup, I edited the question. Can you explain to me what addresses, I'm getting at line 2 in the edited question. what does it mean? Commented Apr 30, 2020 at 21:45

2 Answers 2

1

enter image description here

The picture provides a graphical explanation to the problem if anyone gets confused with the array of pointers concept with dynamically allocating the array of pointers to new int or any other type array


int *ptr[2]; // statically declared pointer array stack

    int p [2];

for (int i = 0; i < 2; i++)
      {
      int size = 2;

      ptr[i] = new int[size];
      cout << i << " array of int " << endl;
      //*(ptr + i) = new int[size];

      for (int j = 0; j < size; j++)
        {
        cout << "value : " ;
        cout << *(ptr[i] + j) ;  // <------- this should give 0's as value
        //cout << (ptr[i])[j] ; <------ same thing
        cout << "  address :";
        cout << ptr[i] + j << endl; //<----- these are in order as well since it's an array of type int

        }

      }
0 array of int 
value : 0  address :0x564c9ede32c0
value : 0  address :0x564c9ede32c4
value : 0  address :0x564c9ede32c8
1 array of int 
value : 0  address :0x564c9ede32e0
value : 0  address :0x564c9ede32e4
value : 0  address :0x564c9ede32e8
Sign up to request clarification or add additional context in comments.

Comments

0

I am assuming you want to perform operation on dynamic array like adding element and printing; Remember:In int *ptr=new int[5]; sizeof(ptr) is 8 bytes on stack memory and array will be stored in heap memory.

We will fetch the element via pointer ptr and every element will be fetched as per type of array (say int ) then ptr will go to 0th index element and read the data of it as int type (only 4 bytes as int is of 4 byte generally) and move to next index till end. Look into code below:

#include <iostream>
using namespace std;

int main() {
int *ptr=new int[5]; //let size = 5
for(int i=0; i<5;i++){
cin>>ptr[i];
}
for(int i=0; i<5;i++){
cout<<&ptr[i]<<":";  //this will print every element's address per iteration
cout<<ptr[i]<<endl;  //this will print every element as per input you gave
}
delete []ptr; //remember it's not delete ptr ask if required
return 0;
}

Now See the the output and dry run yourself you can understand

Output

0x556999c63e70:1
0x556999c63e74:2
0x556999c63e78:3
0x556999c63e7c:4
0x556999c63e80:5

Benefit of dynamic array is you can create dynamic sized array by taking size input as per user choice pass that variable is size of dynamic array i.e you can change above size=5 to 'N' a variable one .

I think this might help you else you can ask for any further clarification.

4 Comments

int *ptr[5]; what I want to figure out is this, and how this expands
This is not the correct way to make 1-dimensional dynamic array ....Since this holds 5 address which does not make sense...
And if you want to store pointers(many) into another pointer (single) then It will make sense when you need 2-D array ....you may visit few reference website of cpp and you get it thoroughly!
I wanted an array which can contain another set of arrays, which is indeed the int* type array, I think i got it after a little drawing. The first array (int * type) is stored in stack and the next arrays (int type )are stored in heap.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.