Quantcast
Channel: Active questions tagged gcc - Stack Overflow
Viewing all articles
Browse latest Browse all 22016

Printing Heap Memory Addresses using GDB

$
0
0

I have this code from the web that I want to test to play with the heap to understand how it stores the addresses.

 #include <stdlib.h>
 #include <unistd.h>
 #include <string.h>
 #include <stdio.h>
 #include <sys/types.h>

 struct internet {
  int priority;
  char *name;
};

void winner()
{
  printf("and we have a winner @ %d\n", time(NULL));
}

int main(int argc, char **argv)
{
  struct internet *i1, *i2, *i3;

  i1 = malloc(sizeof(struct internet));
  i1->priority = 1;
  i1->name = malloc(8);

  i2 = malloc(sizeof(struct internet));
  i2->priority = 2;
  i2->name = malloc(8);

  strcpy(i1->name, argv[1]);
  strcpy(i2->name, argv[2]);

  printf("and that's a wrap folks!\n");
}

I compile it using:

gcc -g -o heap1 heap.c

Then I run the gdb:

gdb heap1

Now, since the code needs input from the arguments, it should print segmentation faulty along with address on the heap as I am using malloc. But the gcc on my machine does not print that. I am not sure it's the version or it needs special configuration to print that heap address memory for variables that have errors.

The gcc version is 9.2.1 while gdb is 8.3.

This is the output of the gdb run command:

Program received signal SIGSEGV, Segmentation fault.
__strcpy_avx2 () at ../sysdeps/x86_64/multiarch/strcpy-avx2.S:301
301     ../sysdeps/x86_64/multiarch/strcpy-avx2.S: No such file or directory.

Viewing all articles
Browse latest Browse all 22016

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>