简体   繁体   中英

c++ array of pointers and memory address allocation

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.

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 ie 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.

在此处输入图像描述

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM