Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pointer pointing to itself C

Tags:

c

pointers

I'm writing a function called at the end of a recursive chain. This function needs to figure out where it is in memory at the instance of when it's being called. The code is as follows:

void recover ()
{
     int * x = (int *)&x;
}

The problem is the program just skips over this statement as if it was never written. I've verified this in GDP. Can you think of any reason why this line is being ignored?

Thanks!

like image 626
prestige13 Avatar asked Nov 13 '15 01:11

prestige13


People also ask

Can pointer point to itself?

Yes, a pointer can point to itself.

Can pointer hold its own address?

The main feature of a pointer is its two-part nature. The pointer itself holds an address. The pointer also points to a value of a specific type - the value at the address the point holds.

What can a pointer point to in C?

The Pointer in C, is a variable that stores address of another variable. A pointer can also be used to refer to another pointer function. A pointer can be incremented/decremented, i.e., to point to the next/ previous memory location. The purpose of pointer is to save memory space and achieve faster execution time.

Can a structure contain pointer to itself in C++?

A structure containing a pointer to itself is not a problem. A pointer has a fixed size, so it doesn't matter how big the size of the structure it points to is.


3 Answers

Declare the variable as volatile. It should prevent compiler optimizations.

volatile int * x = (int *)&x;
like image 198
nnn Avatar answered Oct 07 '22 00:10

nnn


If you are compiling with optimization turned on, then this code is probably optimized out due to no effect on the program. Use -O0 option.

like image 42
ysap Avatar answered Oct 07 '22 02:10

ysap


The code you wrote is probably undefined behavior since you're casting a pointer to a pointer to an int to a pointer to an int.

A pointer can't conceptually point to itself since you would never be able to achieve the right level of indirection. Here's why:

Declaration      |  Type     | Type after using the & operator
--------------------------------------------------------------
int   x;         |  int      | &x is an (int*)
int*  x;         |  int *    | &x is an (int**)
int** x;         |  int **   | &x is an (int***)

However, a void* is the only type of pointer I can think of that might be allowed to break this rule, but I'm not sure if it would be legal according to the standard.

I would propose you write the following instead of what you have:

int* recover ()
{
    int local = 0;
    return &local;
}

And compile it with no optimizations like @ysap suggests.

Still from your example it seems like you're trying to figure out where the top of the stack is and that is very easily done in assembly. All you need to do is read the stack pointer. However, this isn't so cleanly exposed in C. Though GCC does have some helper functions for similar tasks I haven't found one for the stack pointer.

I would personally resort to inline assembly since whatever is written will probably not be portable anyway.

Additionally, if I'm understanding your question right it is most likely a duplicate of this question so it would pay to read those answers as well.

Edit: It should be equivalent to return the address of the frame of the current function which can be done by using a GCC builtin function __builtin_frame_address (unsigned int level) linked earlier. The code would look like this:

int* recover()
{
    return (int*)__builtin_frame_address(0);
}
like image 31
nonsensickle Avatar answered Oct 07 '22 00:10

nonsensickle