4

EDIT: To clarify this is a question about an error I am getting in gdb, and if it's normal behavior, not about weather local variables can be accessed outside of their scope.

Lately, i have been working through some C exercises. I would compile my programs like this -

gcc -g -o ../bin/prog prog.c

And they would be debugged like this -

gdb ../bin/prog (gdb) run < ../bin/input

However every time I do this, I run into close to if not the same error -

Starting program: /home/user/workspace/c exercises/prog/bin/prog < ../bin/input

Program received signal SIGSEGV, Segmentation fault.
__strlen_ia32 () at ../sysdeps/i386/i686/multiarch/../../i586/strlen.S:94
 94      ../sysdeps/i386/i686/multiarch/../../i586/strlen.S: No such file or directory

Below is the corresponding source for this specific occurrence, I am running Debian 9 i386. I don't think it's related to my program being incorrect as I get the same error with programs that work. It's always that it cannot find some .so or .S file. Thanks in advance.

/** Exercise 1-19
 *
 *  Write a function reverse(s) that reverses the character string s.
 *  Use it to write a program that reverses its input a line at a time.
 */

#include <stdio.h>

#define MAX 1000

int getLine(char*,int);
char* reverse(char*);

int main()
{
    char line[MAX];
    while(getLine(line, MAX) > 0)
        puts(reverse(line));
}

int getLine(char s[], int lim)
{
    int c,i;

    for(i=0;(c=getchar())!=EOF && c!='\n';i++)
        s[i] = c;

    if(c == '\n')
        s[i++] == '\n';
    s[i] = '\0';
    return i;
}

char* reverse(char s[])
{
    char rev[MAX];
    int i;

    for(i=MAX-1; s[i]!='\0' && i >= 0; i--);
    for(int j=1; j<=i; j++)
        rev[j] = s[i-j];
    rev[i] = '\0';
    return rev;
}
Ed Crap
  • 51
  • 1
  • 1
  • 7
  • 3
    While the crash happens in `strlen`, the problem isn't there. Go up the call-stack to *your* code and look for the problem there. – Some programmer dude May 22 '18 at 07:00
  • 1
    As for a hint about your problem: What happens to local variables inside a function when you return from the function? Would a pointer to such a variable be valid once the function returns? – Some programmer dude May 22 '18 at 07:02
  • @Someprogrammerdude Ahh, I see why it's crashing now, but is it normal for gdb to throw an error like that? I expected it to just print out the line that the segfault happened at. Why is it saying this stuff about a .S file that could not be found? – Ed Crap May 22 '18 at 07:23
  • While the problem is in your code, the crash itself happens in the `strlen` function`, which is defined in a library which you usually have no sources to and don't need any sources to (unless you want to debug the internals of the library). Therefore you get an error saying that the source is not found. It's not an error in itself, and can be disregarded. – Some programmer dude May 22 '18 at 10:13
  • If you don't have the (assembly) source for `strlen` available, GDB can't do source-level debugging. It's just telling you that you can't see inside the call to `strlen()`. But that's fine, because your problem lies outside it (due to passing a pointer to a temporary that's no longer valid). – Toby Speight May 22 '18 at 10:16
  • P.S. consider using standard `fgets()` instead of writing your own `getLine()`. – Toby Speight May 22 '18 at 10:19
  • Maybe the answer in [GDB complaining about missing raise.c](https://stackoverflow.com/questions/48278881/gdb-complaining-about-missing-raise-c) will help. – Mark Plotnick May 22 '18 at 17:09

0 Answers0