# Pointer question

## Recommended Posts

This is kind of a vague question, but here goes: Let's say I have this code: #include <stdlib.h> #include <iostream.h> void main() { int *ptr; ptr = (int *) malloc (10 * sizeof(int)); for (int i = 0; i < 10; i++) { *ptr = i; cout << *ptr << endl; ptr = ptr + 1; } } I understand that when I increment pointer, I'm incrementing the address that ptr points to, so that's why the pointer moves over to the next element in the array. Specifically the address that ptr points to is incremented by 4 each time. I'm assuming this somehow corresponds to 4 bytes (the size of an integer)? Also I want to know where the conversion is done to convert the 1 (that is summed with ptr) to 4 bytes? I guess in my mind if I wanted to move the ptr over an element and I knew the size of the element was 4 bytes and I also knew that each address digit corresponds to 1 byte, I would do: ptr = ptr + 0x00000004 Maybe I'm thinking about this too hard... I guess I don't understand why they even have the option to do ptr++

##### Share on other sites
The compiler simply takes care of that for you so that you don't have to figure out the size of the element every time.

##### Share on other sites
The conversion is done internally, since ptr is declared as an int *.

It's mainly there as legacy these days. Pointers are [nearly] never used in that manner due to C++ improvements over the more C style arrays.

##### Share on other sites
If it helps think of it as bumping the pointer by the number of elements rather than bytes. ptr+1 moves "move to the next element", which if you have an array of 4-byte things means add 4 to the pointer.

If you look at the assembly for your example you'll see that it does eventually end up as ptr+4. This makes sense because individual bytes are really the only level of abstraction you've got.

##### Share on other sites
As mentioned, the necessary magic is done by the compiler, which "knows" the type of thing that's being pointed at and automatically multiplies the increment by the sizeof(that type). This is one of the reasons why you can't store objects polymorphically by value in an array (e.g. if you try Base b[10]; b[0] = Derived();, you will find that it doesn't work at all like you would hope). It's also a bad idea to do this kind of pointer arithmetic manually these days; it amounts to the same thing as array indexing (and the compiler is more than smart enough to give tight code either way), and it's also easy to "lose" the pointer to the beginning of the array - hello memory leaks.

In any case, in C++ you should use C++-style allocation/deallocation, and modern versions of the libraries:

#include <iostream>using namespace std;int main() { // definitely not void in C++  const int ArraySize = 10;  // Isn't this so much more readable now?  int *ptr = new int[ArraySize];  for (int i = 0; i < ArraySize; i++) {    ptr[i] = i; // instead of doing the pointer arithmetic    cout << ptr[i] << endl;  }  delete[] ptr; // delete[] what you new[], delete what you new  // Of course, in this example, we don't need a heap allocation at all, but  // if I just showed it with a local array, it wouldn't illustrate as much :)}

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628381
• Total Posts
2982360

• 10
• 9
• 15
• 24
• 11