5

[C/C++] Yet another explanation of C/C++ pointers

 2 years ago
source link: https://yurichev.com/news/20220212_C_ptrs/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client
[C/C++] Yet another explanation of C/C++ pointers dot.php

[C/C++] Yet another explanation of C/C++ pointers

For a moment, we can pretend that C/C++ have no arrays at all. Arrays are like syntactic sugar, nothing else.

Many C textbooks can tell you that a[i] is in fact a syntactic sugar for *(a+i).

But even more: array declarations can be seen as syntactic sugar as well.

Instead of:

int a[128];
int b[128];

... you can think of:

int *a=<address of a chunk in global memory or local stack of size 128*sizeof(int)>;
int *b=<address of a chunk in global memory or local stack of size 128*sizeof(int)>;

... or:

unsigned char some_random_chunk_in_memory[100000];

int *a=(int*)&some_random_chunk_in_memory;
int *b=(int*)&(some_random_chunk_in_memory + sizeof(int)*128);

Then you can read/write to both a or b in several ways: a[idx] or *a or *(a+idx), no matter how a was declared/defined: as int a[size] or int *a.

We can write big programs without arrays at all.

This is why Hex-Rays and Ghidra decompilers are so often shows you pointers instead of arrays. There is no reliable way to discern pointer from array in machine code compiled by C/C++ compiler. It's just impossible,

Was it an array access or pointer dereference? Go figure.

This is why both Hex-Rays and Ghidra are interactive tools with GUI. A user should help in setting right data types for variables, giving hints.

Read more: there are couple of other pointers explanations in my Reverse Engineering for Beginners book. Also, see my C-notes.

dot.png?body_20220212_C_ptrs


List of my other blog posts. My company.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK