-1

I am kind of confused with the pointers.

Say if I have this:

int size;
int bytes;
int numbers;
int *ptr;

ptr = new int[500];

My question is the pointer at first points to any specific variable or just to overall int variables. What if I want a pointer to point only at numbers variable.

I have a tail question if you don't mind. I am trying to allocate dynamic memory to my array, is this valid? :

int numbers[20];
int *ptr;
ptr = new int[size];

Would this give a heap memory to my array?

I am working with an array of very large size and there is no other way to work with it without using heap memory.

P.S: I can't use vector at this point.

trincot
  • 317,000
  • 35
  • 244
  • 286
  • To understand pointers, consider that pointers are references to actual RAM that a program exists in. Classic strongly typed languages like C require that all variables in memory at runtime are typed. So, for example, one can have a 4-byte block of memory set aside for a 32-bit integer type. In order for appropriate read, write, and usage apart, the contents size and type must be directly identifiable in a static way during compilation. It looks like your assignment would not use heap memory, and instead be treated like a normal array. Read the docs on malloc() for dynamic allocation. – Adrian M. Mar 18 '17 at 19:08

3 Answers3

1

int *ptr is an pointer to an int variable. It doesn't matter what variable, as long as it is an int so you could have the following.

int size;
int bytes;
int numbers;
int *ptr;

ptr = &size;
ptr = &bytes;
ptr = &numbers;

I use the & symbol because this gives the actual address of the variable not its value. Pointers can only store the address of a variable.

If you then wanted to use the actual values that the pointer points to, you need to deference it, like so

int number = 5;
int *ptr;
ptr = &number;
cout << *ptr;   
//5

As for your second question. Yes that would give you a valid pointer to an array on the heap.

For more information I suggest looking up what an array actually is, since you might be confused why you don't need to use the & symbol when assigning a pointer to an array.

Gary Holiday
  • 3,297
  • 3
  • 31
  • 72
0

int *ptr is uninitialized, so it actually points to "garbage values" or in this case, random memory addresses. Check out Uninitialized pointers in code

If you want int *ptr to point to the value of numbers, you should first initialize int numbers, because this can cause Undefined behavior if you try to dereference it.

You can assign the ptr equal to numbers by using the Address-of operator &. Then use the dereference operator * to grab the value of ptr.

numbers = 4;
ptr = &numbers;
cout << *ptr << endl;

This will print 4.

Community
  • 1
  • 1
0
int arNumbers[20];

gives you memory from stack and is not dynamic and compiler would take care of releasing the memory.

int *pNumbers = new int[20];

gives you memory from heap and is dynamic and you need to delete it when you are done:

delete[] pNumbers;

if you need 20 numbers, one of them is enough.

Ali Asadpoor
  • 327
  • 1
  • 13