Dynamically Allocating Arrays Depending on User Input in C++ -
im watching tutorial on youtube https://www.youtube.com/watch?v=8xaqzcjvohk dynamically allocating arrays depending on user input in c++
this code
1 int main() 2 { 3 int *pointer = nullptr; 4 5 cout << "how many items u gonna enter" << endl; 6 int input; 7 cin >> input; 8 9 pointer = new int[input]; 10 11 int temp; 12 13 (int counter = 0; counter < input; counter++) { 14 cout << "enter item " << counter + 1 << endl; 15 cin >> temp; 16 *(pointer + counter) = temp; 17 } 18 19 cout << "the items have entered are" << endl; 20 (int counter = 0; counter < input; counter++) { 21 cout << counter + 1 << " item " << *(pointer + counter) << endl; 22 } 23 24 delete[]pointer; 25 26 return 0; 27}
im stuck in line 16, dont understand why that, inside (), pointer variable , counter added each other
pointer arithmetic point start.
i'm going try explain briefly how works, suggest integrate concepts book or internet references because important proper handling pointers.
a pointer (as can imagine name) points memory cell:
int* ptr = /*an address memory cell*/
your memory composed sequentially cells, graphically:
address| value -------|------------ 0x00 | [#some value] 8 bit 0x01 | [#some value] 8 bit ... | ... 0xn | [#some value] 8 bit
just make example not long, can assume each cell contains 8 bits , integer value represented 32 bit (usually not true, , depends on machine architecture , compiler).
then int
value stored in 4
cells. (we explicitly don't consider memory alignment).
so pointer contains memory location, address in memory contains value you've allocated (with usage of dynamic memory).
for example:
int* ptr = 0x01
that means variable ptr, stored somewhere in memory, contains address 0x01
. in memory cell 0x01
there integer value allocated dynamically.
but, since value integer type, data take 4 cell in order store complete information. data "split" among cell 0x01, 0x02, 0x03, 0x04
.
the pointer points first memory location of data, , number of cell occupied given type of pointer (in case pointer int
compiler knows information starts cell 0x01
, ends 0x04
).
a variable pointer can evaluated in arithmetic expression, such sums , differences.
fo example:
ptr + 10 ptr - 10
simply, meaning of expression access memory address starting address stored in ptr
, jumping 10
int cells forward or backward.
attention note: expression does not mean add value address obtaining new address.
indeed, assuming ptr = 0x01
, expression:
ptr + 10;
does not mean 0x01 + 10 = 0xa
!
instead means jump 10 "block" of size equal type's size pointed pointer itself.
that is, 0x01 + 10 * 4bytes
. since ptr
pointer int
, +10
means "plus 10 block of integers", and, in example, each int
occupies 4 bytes (32 bit).
to conclude, expression:
*(pointer + counter) = temp;
means access address start pointer
, adding #counter
block of int
, deference address operator*
, write in address value temp
.
that notation can simplify operator[]
:
pointer[counter] = temp;
where meaning same, notation more readable, when have array.
Comments
Post a Comment