Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Understanding pointer indirection

Posted on 2008-06-22
9
787 Views
Last Modified: 2013-12-14
Hi,

Can somebody explain what indirection really means?

1) I don't understand why they call it indirection... what is the source of this name?
2) as far as i can understand, indirecting a pointer is changing the value of the variable it points to. Is this true, or is there more to indirection?
3) supose I create a pointer using "int *ptr;"... is it possible to indirect the pointer, even if it does not point to a variable?

Thank!
0
Comment
Question by:codeQuantum
  • 5
  • 4
9 Comments
 
LVL 53

Accepted Solution

by:
Infinity08 earned 250 total points
ID: 21842348
>> 1) I don't understand why they call it indirection... what is the source of this name?

Basically, because it provides a way to indirectly access data.


>> 2) as far as i can understand, indirecting a pointer is changing the value of the variable it points to. Is this true, or is there more to indirection?

Indirection of a pointer is getting access to the data it points to. For retrieving and/or modifying that data.


>> 3) supose I create a pointer using "int *ptr;"... is it possible to indirect the pointer, even if it does not point to a variable?

As long as the pointer points to a valid memory location, you can get the data at that memory location by indirection of the pointer.

A pointer can point to valid memory by explicitly making it point to valid memory, for example :

        int *p = new int(5);
        int value = 4;
        int *p2 = &value;

But it could also be "by accident". Uninitialized pointers can have any value, so they could point to a valid memory location, in which case you can get the data at that location by indirection of the pointer.
0
 
LVL 5

Author Comment

by:codeQuantum
ID: 21842433
Thanks, point 1) and 2) are clear to me now.

I was asking that last question because I am doing an exercise right now that is related.

It says : >> create 2 pointers... A and B. Then make it so each of those pointers is assigned a memory zone that can hold an int. <<

So if I understand your answer correctly, I have to create two int variable and associate the pointers to them, or use new int(123) in order to solve the problem? or is there another way to do this?

(in other words, is there an elegant solution that does not involve creating unnecessary int variables, or using an arbitrary number inside the new int(x)?)
0
 
LVL 53

Assisted Solution

by:Infinity08
Infinity08 earned 250 total points
ID: 21842473
>> or use new int(123) in order to solve the problem?

I think the question meant to allocate dynamic memory, so you'll have to use new, yes.


>> (in other words, is there an elegant solution that does not involve creating unnecessary int variables, or using an arbitrary number inside the new int(x)?)

The new int(5) was just an example. You can use new in a variety of ways (to allocate memory that can contain one or more objects of a given type). For example :

        int *p = new int;             // <--- allocate memory for one int
        int *p2 = new int[10];     // <--- allocate memory for 10 ints
        int *p3 = new int(5);       // <--- allocate memory for one int, and initialize that int value to 5

Note that you need to explicitly release the allocated memory once you don't need it any more. Use either delete or delete [] for that (the latter when you used new []) :

        delete p;
        delete [] p2;
        delete p3;


Here's a nice tutorial about dynamic memory :

        http://www.cplusplus.com/doc/tutorial/dynamic.html
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 5

Author Comment

by:codeQuantum
ID: 21842530
So I guess I need to use

int *p = new int;

Thanks infinity08, you have been very helpful. :)
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 21842539
>> So I guess I need to use
>> 
>> int *p = new int;

Yep. Twice - once for A and once for B ;)
0
 
LVL 5

Author Comment

by:codeQuantum
ID: 21842543
One last detail, if I wanted to break that operation on two lines, would that work :

int *p;
*p = new int;

?

I am sometimes confused by the indirection operator * that seem to be used in 2 different ways between declaration and indirection.
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 21842569
>> int *p;
>> *p = new int;

Make that :

        int *p;
        p = new int;

and it will work. The type of p is int* (pointer-to-int). *p is the indirection of p, so it actually refers to the int value.


>> that seem to be used in 2 different ways between declaration and indirection.

This is a declaration :

        int *p;

The * here is NOT the indirection operator. It is part of the type of p (int* or pointer-to-int)
0
 
LVL 5

Author Comment

by:codeQuantum
ID: 21842603
One last thing (sorry, I promess this is the last one!)

I read that arrays are basically pointers (to their first element). If it is so, why can't I do this :

      int untab[] = {5, 19, 14, 8, -10, 99};
      int *unptr = new int;
      untab = unptr; // returns an error

Both are int pointers, so what's the problem?

I understand that I would lose information doing that. But since C++ is not as protective as Java, I don't see why it does not compile...
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 21842621
>> I read that arrays are basically pointers

That's not true. Arrays will degenerate to a pointer in certain contexts, so you can do this for example :

        int untab[] = {5, 19, 14, 8, -10, 99};
        int *unptr = untab;                                   // <--- now unptr will point to the first element in the untab array (5)

However, that doesn't work the other way around. You cannot re-assign an array identifier, and let it point to different memory.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

789 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question