Skip to content

Instantly share code, notes, and snippets.

@shiyanhui
Last active January 4, 2021 01:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shiyanhui/f22eb5295b48edb46b43dad1ebe54b03 to your computer and use it in GitHub Desktop.
Save shiyanhui/f22eb5295b48edb46b43dad1ebe54b03 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
// 看起来你是在x86_64上来跑的, int为4-bytes, 此时内存布局为:
//
// a[0]
// ↓
// 96000000 fa000000 5e010000 c2010000
int a[4] = {150, 250, 350, 450};
int * c = a;
printf("1: a[0] = %d, a[1] = %d, a[2] = %d, a[3] = %d\n", a[0], a[1], a[2], a[3]);
// c
// ↓
// 96000000 fa000000 5e010000 c2010000
c = c + 1;
printf("current c points to %d\n", *c);
//
// c
// ↓
// 96000000 fa000000 5e010000 c2010000
c = (int *) ((char *) c + 1);
printf("%p\n", c);
// c
// ↓
// 96000000 faf40100 00010000 c2010000
//
// 最终:
//
// a[0] = 0x96
// a[1] = 0x1f4fa -> 128250
// a[2] = 0x100 -> 256
// a[4] = 0x1c2
*c = 500;
printf("2: a[0] = %d, a[1] = %d, a[2] = %d, a[3] = %d\n", a[0], a[1], a[2], a[3]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment