Hi, I was doing some basic testing regarding string manipulation/run-time resizing and ran into this weird issue.
The program runs fine via the console but I get this weird message when stepping through it with gdb and just can't figure out
what the hell is going on:
Here's the gdb output:
As you can see, I encounter this issue while calling realloc for the second time, so t="aa"
I'm running this on a 64bit Windows machine. Any help for a helpless idiot like me would be greatly appreciated! 🤝
The program runs fine via the console but I get this weird message when stepping through it with gdb and just can't figure out
what the hell is going on:
Code
#include "stdlib.h"
#include "stdio.h"
#include "string.h"
char *foo(){
char *t = malloc(sizeof(char));
int i = 0;
while(i<5){
t[i] = 'a';
t = (char *)realloc(t, sizeof(char));
i++;
}
t[i] = 0;
return t;
}
int main(int argc, char const *argv[])
{
char *c;
c = foo();
printf("%s\n", c);
//free(c);
return 0;
}Here's the gdb output:
As you can see, I encounter this issue while calling realloc for the second time, so t="aa"
Code
Breakpoint 1, foo () at testing.c:5
5 char *t = malloc(sizeof(char));
(gdb) display t
1: t = 0x61ffcc "äÿa"
(gdb) display i
2: i = 6422296
(gdb) display t+i
3: t+i = 0xc3fee4 <Address 0xc3fee4 out of bounds>
(gdb) display t[i]
4: t[i] = <error: Cannot access memory at address 0xc3fee4>
(gdb) next
6 int i = 0;
4: t[i] = <error: Cannot access memory at address 0x1042998>
3: t+i = 0x1042998 <Address 0x1042998 out of bounds>
2: i = 6422296
1: t = 0xa22a80 "\230««««««««þîþîþîþ"
(gdb) next
7 while(i<5){
4: t[i] = -104 '\230'
3: t+i = 0xa22a80 "\230««««««««þîþîþîþ"
2: i = 0
1: t = 0xa22a80 "\230««««««««þîþîþîþ"
(gdb) next
8 t[i] = 'a';
4: t[i] = -104 '\230'
3: t+i = 0xa22a80 "\230««««««««þîþîþîþ"
2: i = 0
1: t = 0xa22a80 "\230««««««««þîþîþîþ"
(gdb) next
9 t = (char *)realloc(t, sizeof(char));
4: t[i] = 97 'a'
3: t+i = 0xa22a80 "a««««««««þîþîþîþ"
2: i = 0
1: t = 0xa22a80 "a««««««««þîþîþîþ"
(gdb) next
10 i++;
4: t[i] = 97 'a'
3: t+i = 0xa22a80 "a««««««««þîþîþîþ"
2: i = 0
1: t = 0xa22a80 "a««««««««þîþîþîþ"
(gdb) next
7 while(i<5){
4: t[i] = -85 '«'
3: t+i = 0xa22a81 "««««««««þîþîþîþ"
2: i = 1
1: t = 0xa22a80 "a««««««««þîþîþîþ"
(gdb) next
8 t[i] = 'a';
4: t[i] = -85 '«'
3: t+i = 0xa22a81 "««««««««þîþîþîþ"
2: i = 1
1: t = 0xa22a80 "a««««««««þîþîþîþ"
(gdb) next
9 t = (char *)realloc(t, sizeof(char));
4: t[i] = 97 'a'
3: t+i = 0xa22a81 "a«««««««þîþîþîþ"
2: i = 1
1: t = 0xa22a80 "aa«««««««þîþîþîþ"
(gdb) next
warning: HEAP[testing.exe]:
warning: Heap block at 00A22A78 modified at 00A22A81 past requested size of 1
Program received signal SIGTRAP, Trace/breakpoint trap.
0x775c1c40 in ?? ()I'm running this on a 64bit Windows machine. Any help for a helpless idiot like me would be greatly appreciated! 🤝
![[Image: 0DvFxnA.gif]](https://i.imgur.com/0DvFxnA.gif)
Donate: 3ABKRghau93ziRaDfVwiiwtjky3i1gLrKn



