0

So I'm starting with using GDB. I'm trying out simple things to get used to it.

So I have the following code with a small bug in it. (Segmentation fault):

char *concat(char *s, char *t){
    return strcpy(s+strlen(s), t);
}

int main() {
    
    char *a = "Pliz, ";
    char *b = "Help me SoF";

    printf("%s + %s = %s\n", a, b, concat(a, b));
    return 0;
}

So it gives me a Segmentation fault. When running gdb main core, and backtrace bt to display the program stack I get three frames. When looking at frame 1 of function concat(), I should see the two parameters s and t and their values. But I dont get to see it, How come? What I want is the following as in the picture (See desired result/view; The concat1.c is the same as main.c in my case).

Picture of my terminal

Desired result

Krzysztof Madej
  • 32,704
  • 10
  • 78
  • 107
EnesK
  • 93
  • 6
  • Debug symbols are not present. You need to add `-g` to add debug symbols. – Krishna Kanth Yenumula Nov 29 '20 at 16:31
  • "But I dont get to see it, How come?" -- when asking a question, _show_ what you see, don't just describe it. This will generally produce better answers. – Employed Russian Nov 29 '20 at 16:52
  • @EmployedRussian, Thank you for feedback. I have edited my question. I added the picture of what I get, and I added the picture of what I want to see. – EnesK Nov 29 '20 at 17:19
  • Picture is better than a description, but picture of text is worse than the text itself (which you could have easily copy/pasted). In the future, please paste the text itself. (The answer by Krishna Kanth Yenumula is correct though: it's clear from your picture that you've compiled your code without `-g` flag, – Employed Russian Nov 29 '20 at 23:04

1 Answers1

1

Here a and b are string literals. You cannot modify them. You are adding b at the end of a .This is the reason for segmentation fault. Visit this answer Modifying string literals.

Debug symbols are not present. You need to add -g to your compile and link commands to add debug symbols.

Employed Russian
  • 199,314
  • 34
  • 295
  • 362
Krishna Kanth Yenumula
  • 2,533
  • 2
  • 14
  • 26