Gå til innhold

Får segmentation fault til slutt i prog. [LØST]


Anbefalte innlegg

Har laget et lite program som tar infix input og konverterer den til postfix.

EDIT: Link til nedlasting av kildekode er fjernet.

 

Problemet er at jeg får en segmentation fault til slutt i programmet. Programmet fungerer fint ellers.

Segm.fault. kommer ikke hvis jeg ikke skriver inn tekststrengen fra tastatur, altså hvis jeg gir verdi til s i kildekoden og kommenterer bort lesing fra tastatur i readString.

 

Noen som har noen tips om hva som kan være problemet?

 

Her er koden: Hvis noen trenger header fila ligger den i .tar fila i linken. Der er kun inkluderinger og prototyper på funksjoner.

 

#include "project1.h"
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------

int main(int argc, char **argv) {
char *string;

readString(&string);
infixInput(string);

return 0;
}

//Read a string from keyboard.
void readString(char **s) {
char ch;
int i = 0;
//(*s) = " b + c ";

printf("\nType an infix expression, ex. a - b > ");

while((ch = getchar()) != EOF && ch != '\n') 
 (*s)[i++] = (char)ch;

(*s)[i] = '\0';
}

void infixInput(char *input) {
stack *postfix = NULL;
char tmp;

printf("\nINFIX_INPUT\n\n");
printf("input: %s\n", input);
printf("output: ");

while(*input != '\0') {	

 if(*input == ' '){;} //ignore space

 else if(*input == '(') {
 	pushStack(&postfix, *input);
 }

 else if(*input == ')') {
 	while((tmp = popStack(&postfix)) != '(')
   printf("%c ", tmp);
 }

 else if(*input == '*' || *input == '/') {
 	while((tmp = popStack(&postfix)) != -1) {
   if(tmp == '(' || tmp == '+' || tmp == '-') {
   	pushStack(&postfix, tmp);
   	break;
   }
   printf("%c ", tmp);
 	}
 	pushStack(&postfix, *input);
 }

 else if(*input == '+' || *input == '-') {
 	while((tmp = popStack(&postfix)) != -1) {
   if(tmp  == '(') {
   	pushStack(&postfix, tmp);
   	break;
   }
   printf("%c ", tmp);
 	}
 	pushStack(&postfix, *input);
 }

 else 
 	printf("%c ", *input);

 ++input;
}

while(postfix != NULL) {
 printf("%c ", postfix -> character);
 postfix = postfix -> prev;
}

printf("\n\n");
}

//insert an item on stack.
void pushStack(stack **s, char c) {
stack *tmp = (stack *)malloc(sizeof(stack));
tmp -> character = c;
tmp -> prev = *s;
*s = tmp;
}

//remove an item from stack.
char popStack(stack **s) {
char character;
stack *tmp;

if((*s) == NULL) 
 return -1;

else {
 character = (*s) -> character;
 tmp = (*s);
 *s = (*s) -> prev;
 free(tmp);
}

return character;
}

Endret av Orjanp
Lenke til kommentar
Videoannonse
Annonse

Ser ikke at du allokerer minne til s i readString (eller noen andre plasser heller, egentlig). Når du setter s til å være strengen "b + c", så allokerer kompilatoren minne til denne siden det er en "konstant". Du må derfor ta en malloc eller noe slikt for å få et minneområde til å kunne legge inn bokstavene fra keyboardet. Slik det er nå, så er ikke s (*string i main) satt til å peke på noe som helst..

 

Håper det hjalp (begynner å bli litt rusten i C...)!

Lenke til kommentar

Takk for hjelpen.

 

Valgrid kom opp med en hel masse greier som jeg ikke forstod så veldig mye av foreløpig. Skal prøve å sette meg inn i Valgrid etterhvert. Så ut som et nyttig verktøy.

 

Løsningen ble slik:

int main(int argc, char **argv) {
char string[32]; //Denne sørger nå for å allokere plass til strengen jeg ønsker å lese inn.

readString(string);
infixInput(string);

return 0;
}

//Read a string from keyboard.
void readString(char *s) {
char ch;
int i = 0;
//(*s) = " b + c ";

printf("\nType an infix expression, ex. a - b > ");

while((ch = getchar()) != EOF && ch != '\n') 
 s[i++] = (char)ch;

s[i] = '\0';
}

Det var eneste endringen jeg gjorde, og da forsvant segmentation fault feilen. Så takk for hjelpen Paull. :)

Endret av Orjanp
Lenke til kommentar

Jeg lastet ned Valgrind 2.1.1. Kjørte med kommandoen

# valgrind -v tools=memcheck ./project1.

Fikk da følgende output:

==13732== Memcheck, a memory error detector for x86-linux.
==13732== Copyright (C) 2002-2004, and GNU GPL'd, by Julian Seward.
==13732== Using valgrind-2.1.1, a program supervision framework for x86-linux.
==13732== Copyright (C) 2000-2004, and GNU GPL'd, by Julian Seward.
==13732== Valgrind library directory: /usr/local/lib/valgrind
==13732== Command line
==13732==    ./project1
==13732== Startup, with flags:
==13732==    -v
==13732==    --tool=memcheck
==13732== Reading syms from /home/orjanp/informatikk/inf2400/project1/project1 (0x8048000)
==13732== Reading syms from /lib/ld-2.3.2.so (0x3C000000)
==13732== Reading syms from /lib/ld-2.3.2.so (0xB0000000)
==13732== Reading syms from /lib/libdl-2.3.2.so (0xB002A000)
==13732==    object doesn't have any debug info
==13732== Reading syms from /lib/libc-2.3.2.so (0xB002E000)
==13732==    object doesn't have any debug info
==13732== Reading syms from /usr/local/lib/valgrind/vgskin_memcheck.so (0xB0264000)
==13732== Reading syms from /usr/local/lib/valgrind/stage2 (0xB8000000)
==13732== Reading suppressions file: /usr/local/lib/valgrind/default.supp
==13732== REDIRECT soname:libc.so.6(__GI___errno_location) to soname:libpthread.so.0(__errno_location)
==13732== REDIRECT soname:libc.so.6(__errno_location) to soname:libpthread.so.0(__errno_location)
==13732== REDIRECT soname:libc.so.6(__GI___h_errno_location) to soname:libpthread.so.0(__h_errno_location)
==13732== REDIRECT soname:libc.so.6(__h_errno_location) to soname:libpthread.so.0(__h_errno_location)
==13732== REDIRECT soname:libc.so.6(__GI___res_state) to soname:libpthread.so.0(__res_state)
==13732== REDIRECT soname:libc.so.6(__res_state) to soname:libpthread.so.0(__res_state)
==13732== REDIRECT soname:libc.so.6(stpcpy) to *vgpreload_memcheck.so*(stpcpy)
==13732== REDIRECT soname:libc.so.6(strnlen) to *vgpreload_memcheck.so*(strnlen)
==13732== REDIRECT soname:ld-linux.so.2(stpcpy) to *vgpreload_memcheck.so*(stpcpy)
==13732== REDIRECT soname:ld-linux.so.2(strchr) to *vgpreload_memcheck.so*(strchr)
==13732==
==13732== Reading syms from /usr/local/lib/valgrind/vg_inject.so (0x3C017000)
==13732== Reading syms from /usr/local/lib/valgrind/vgpreload_memcheck.so (0x3C01C000)
==13732== TRANSLATE: 0x3C010390 redirected to 0x3C01DA30
==13732== Reading syms from /lib/libc-2.3.2.so (0x3C037000)
==13732==    object doesn't have any debug info

Type an infix expression, ex. a - b > a - b
==13732== Use of uninitialised value of size 4
==13732==    at 0x8048452: readString (in /home/orjanp/informatikk/inf2400/project1/project1)
==13732==    by 0x80483F7: main (in /home/orjanp/informatikk/inf2400/project1/project1)

INFIX_INPUT

==13732==
==13732== Conditional jump or move depends on uninitialised value(s)
==13732==    at 0x3C0831E9: _IO_vfprintf_internal (in /lib/libc-2.3.2.so)
==13732==    by 0x3C08B6F1: _IO_printf (in /lib/libc-2.3.2.so)
==13732==    by 0x8048489: infixInput (in /home/orjanp/informatikk/inf2400/project1/project1)
==13732==    by 0x8048405: main (in /home/orjanp/informatikk/inf2400/project1/project1)
==13732==
==13732== Use of uninitialised value of size 4
==13732==    at 0x3C01DC68: strlen (mac_replace_strmem.c:189)
==13732==    by 0x3C083434: _IO_vfprintf_internal (in /lib/libc-2.3.2.so)
==13732==    by 0x3C08B6F1: _IO_printf (in /lib/libc-2.3.2.so)
==13732==    by 0x8048489: infixInput (in /home/orjanp/informatikk/inf2400/project1/project1)
==13732==
==13732== Use of uninitialised value of size 4
==13732==    at 0x3C01DC71: strlen (mac_replace_strmem.c:189)
==13732==    by 0x3C083434: _IO_vfprintf_internal (in /lib/libc-2.3.2.so)
==13732==    by 0x3C08B6F1: _IO_printf (in /lib/libc-2.3.2.so)
==13732==    by 0x8048489: infixInput (in /home/orjanp/informatikk/inf2400/project1/project1)
==13732==
==13732== Conditional jump or move depends on uninitialised value(s)
==13732==    at 0x3C0A84C6: _IO_file_xsputn@@GLIBC_2.1 (in /lib/libc-2.3.2.so)
==13732==    by 0x3C0833B2: _IO_vfprintf_internal (in /lib/libc-2.3.2.so)
==13732==    by 0x3C08B6F1: _IO_printf (in /lib/libc-2.3.2.so)
==13732==    by 0x8048489: infixInput (in /home/orjanp/informatikk/inf2400/project1/project1)
==13732==
==13732== Use of uninitialised value of size 4
==13732==    at 0x3C0A84D1: _IO_file_xsputn@@GLIBC_2.1 (in /lib/libc-2.3.2.so)
==13732==    by 0x3C0833B2: _IO_vfprintf_internal (in /lib/libc-2.3.2.so)
==13732==    by 0x3C08B6F1: _IO_printf (in /lib/libc-2.3.2.so)
==13732==    by 0x8048489: infixInput (in /home/orjanp/informatikk/inf2400/project1/project1)
==13732==
==13732== Conditional jump or move depends on uninitialised value(s)
==13732==    at 0x3C0A84D9: _IO_file_xsputn@@GLIBC_2.1 (in /lib/libc-2.3.2.so)
==13732==    by 0x3C0833B2: _IO_vfprintf_internal (in /lib/libc-2.3.2.so)
==13732==    by 0x3C08B6F1: _IO_printf (in /lib/libc-2.3.2.so)
==13732==    by 0x8048489: infixInput (in /home/orjanp/informatikk/inf2400/project1/project1)
==13732==
==13732== Use of uninitialised value of size 4
==13732==    at 0x3C0A83C0: _IO_file_xsputn@@GLIBC_2.1 (in /lib/libc-2.3.2.so)
==13732==    by 0x3C0833B2: _IO_vfprintf_internal (in /lib/libc-2.3.2.so)
==13732==    by 0x3C08B6F1: _IO_printf (in /lib/libc-2.3.2.so)
==13732==    by 0x8048489: infixInput (in /home/orjanp/informatikk/inf2400/project1/project1)
input: a - b
==13732==
==13732== Use of uninitialised value of size 4
==13732==    at 0x80484A0: infixInput (in /home/orjanp/informatikk/inf2400/project1/project1)
==13732==    by 0x8048405: main (in /home/orjanp/informatikk/inf2400/project1/project1)
==13732==
==13732== Use of uninitialised value of size 4
==13732==    at 0x80484AD: infixInput (in /home/orjanp/informatikk/inf2400/project1/project1)
==13732==    by 0x8048405: main (in /home/orjanp/informatikk/inf2400/project1/project1)
==13732==
==13732== Use of uninitialised value of size 4
==13732==    at 0x80484BA: infixInput (in /home/orjanp/informatikk/inf2400/project1/project1)
==13732==    by 0x8048405: main (in /home/orjanp/informatikk/inf2400/project1/project1)
==13732==
==13732== Use of uninitialised value of size 4
==13732==    at 0x80484DD: infixInput (in /home/orjanp/informatikk/inf2400/project1/project1)
==13732==    by 0x8048405: main (in /home/orjanp/informatikk/inf2400/project1/project1)
==13732==
==13732== Use of uninitialised value of size 4
==13732==    at 0x8048519: infixInput (in /home/orjanp/informatikk/inf2400/project1/project1)
==13732==    by 0x8048405: main (in /home/orjanp/informatikk/inf2400/project1/project1)
==13732==
==13732== Use of uninitialised value of size 4
==13732==    at 0x8048521: infixInput (in /home/orjanp/informatikk/inf2400/project1/project1)
==13732==    by 0x8048405: main (in /home/orjanp/informatikk/inf2400/project1/project1)
==13732==
==13732== Use of uninitialised value of size 4
==13732==    at 0x80485A1: infixInput (in /home/orjanp/informatikk/inf2400/project1/project1)
==13732==    by 0x8048405: main (in /home/orjanp/informatikk/inf2400/project1/project1)
==13732==
==13732== Use of uninitialised value of size 4
==13732==    at 0x80485A9: infixInput (in /home/orjanp/informatikk/inf2400/project1/project1)
==13732==    by 0x8048405: main (in /home/orjanp/informatikk/inf2400/project1/project1)
==13732==
==13732== Use of uninitialised value of size 4
==13732==    at 0x804861B: infixInput (in /home/orjanp/informatikk/inf2400/project1/project1)
==13732==    by 0x8048405: main (in /home/orjanp/informatikk/inf2400/project1/project1)
==13732==
==13732== Use of uninitialised value of size 4
==13732==    at 0x8048603: infixInput (in /home/orjanp/informatikk/inf2400/project1/project1)
==13732==    by 0x8048405: main (in /home/orjanp/informatikk/inf2400/project1/project1)
output: a b -

==13732==
==13732== Invalid read of size 4
==13732==    at 0x3C00B15A: _dl_fini (dl-fini.c:64)
==13732==    by 0x3C063210: exit (in /lib/libc-2.3.2.so)
==13732==    by 0x3C04CD0D: __libc_start_main (in /lib/libc-2.3.2.so)
==13732==    by 0x8048340: (within /home/orjanp/informatikk/inf2400/project1/project1)
==13732==  Address 0x202D21D1 is not stack'd, malloc'd or free'd
==13732==
==13732== Process terminating with default action of signal 11 (SIGSEGV): dumping core
==13732==  Access not within mapped region at address 0x202D21D1
==13732==    at 0x3C00B15A: _dl_fini (dl-fini.c:64)
==13732==    by 0x3C063210: exit (in /lib/libc-2.3.2.so)
==13732==    by 0x3C04CD0D: __libc_start_main (in /lib/libc-2.3.2.so)
==13732==    by 0x8048340: (within /home/orjanp/informatikk/inf2400/project1/project1)
==13732==
==13732== ERROR SUMMARY: 61 errors from 19 contexts (suppressed: 11 from 1)
==13732==
==13732== 1 errors in context 1 of 19:
==13732== Invalid read of size 4
==13732==    at 0x3C00B15A: _dl_fini (dl-fini.c:64)
==13732==    by 0x3C063210: exit (in /lib/libc-2.3.2.so)
==13732==    by 0x3C04CD0D: __libc_start_main (in /lib/libc-2.3.2.so)
==13732==    by 0x8048340: (within /home/orjanp/informatikk/inf2400/project1/project1)
==13732==  Address 0x202D21D1 is not stack'd, malloc'd or free'd
==13732==
==13732== 1 errors in context 2 of 19:
==13732== Use of uninitialised value of size 4
==13732==    at 0x8048603: infixInput (in /home/orjanp/informatikk/inf2400/project1/project1)
==13732==    by 0x8048405: main (in /home/orjanp/informatikk/inf2400/project1/project1)
==13732==
==13732== 1 errors in context 3 of 19:
==13732== Conditional jump or move depends on uninitialised value(s)
==13732==    at 0x3C0A84C6: _IO_file_xsputn@@GLIBC_2.1 (in /lib/libc-2.3.2.so)
==13732==    by 0x3C0833B2: _IO_vfprintf_internal (in /lib/libc-2.3.2.so)
==13732==    by 0x3C08B6F1: _IO_printf (in /lib/libc-2.3.2.so)
==13732==    by 0x8048489: infixInput (in /home/orjanp/informatikk/inf2400/project1/project1)
==13732==
==13732== 1 errors in context 4 of 19:
==13732== Use of uninitialised value of size 4
==13732==    at 0x3C01DC68: strlen (mac_replace_strmem.c:189)
==13732==    by 0x3C083434: _IO_vfprintf_internal (in /lib/libc-2.3.2.so)
==13732==    by 0x3C08B6F1: _IO_printf (in /lib/libc-2.3.2.so)
==13732==    by 0x8048489: infixInput (in /home/orjanp/informatikk/inf2400/project1/project1)
==13732==
==13732== 1 errors in context 5 of 19:
==13732== Conditional jump or move depends on uninitialised value(s)
==13732==    at 0x3C0831E9: _IO_vfprintf_internal (in /lib/libc-2.3.2.so)
==13732==    by 0x3C08B6F1: _IO_printf (in /lib/libc-2.3.2.so)
==13732==    by 0x8048489: infixInput (in /home/orjanp/informatikk/inf2400/project1/project1)
==13732==    by 0x8048405: main (in /home/orjanp/informatikk/inf2400/project1/project1)
==13732==
==13732== 2 errors in context 6 of 19:
==13732== Use of uninitialised value of size 4
==13732==    at 0x804861B: infixInput (in /home/orjanp/informatikk/inf2400/project1/project1)
==13732==    by 0x8048405: main (in /home/orjanp/informatikk/inf2400/project1/project1)
==13732==
==13732== 3 errors in context 7 of 19:
==13732== Use of uninitialised value of size 4
==13732==    at 0x80485A9: infixInput (in /home/orjanp/informatikk/inf2400/project1/project1)
==13732==    by 0x8048405: main (in /home/orjanp/informatikk/inf2400/project1/project1)
==13732==
==13732== 3 errors in context 8 of 19:
==13732== Use of uninitialised value of size 4
==13732==    at 0x80485A1: infixInput (in /home/orjanp/informatikk/inf2400/project1/project1)
==13732==    by 0x8048405: main (in /home/orjanp/informatikk/inf2400/project1/project1)
==13732==
==13732== 3 errors in context 9 of 19:
==13732== Use of uninitialised value of size 4
==13732==    at 0x8048521: infixInput (in /home/orjanp/informatikk/inf2400/project1/project1)
==13732==    by 0x8048405: main (in /home/orjanp/informatikk/inf2400/project1/project1)
==13732==
==13732== 3 errors in context 10 of 19:
==13732== Use of uninitialised value of size 4
==13732==    at 0x8048519: infixInput (in /home/orjanp/informatikk/inf2400/project1/project1)
==13732==    by 0x8048405: main (in /home/orjanp/informatikk/inf2400/project1/project1)
==13732==
==13732== 3 errors in context 11 of 19:
==13732== Use of uninitialised value of size 4
==13732==    at 0x80484DD: infixInput (in /home/orjanp/informatikk/inf2400/project1/project1)
==13732==    by 0x8048405: main (in /home/orjanp/informatikk/inf2400/project1/project1)
==13732==
==13732== 3 errors in context 12 of 19:
==13732== Use of uninitialised value of size 4
==13732==    at 0x80484BA: infixInput (in /home/orjanp/informatikk/inf2400/project1/project1)
==13732==    by 0x8048405: main (in /home/orjanp/informatikk/inf2400/project1/project1)
==13732==
==13732== 5 errors in context 13 of 19:
==13732== Use of uninitialised value of size 4
==13732==    at 0x80484AD: infixInput (in /home/orjanp/informatikk/inf2400/project1/project1)
==13732==    by 0x8048405: main (in /home/orjanp/informatikk/inf2400/project1/project1)
==13732==
==13732== 5 errors in context 14 of 19:
==13732== Use of uninitialised value of size 4
==13732==    at 0x3C0A83C0: _IO_file_xsputn@@GLIBC_2.1 (in /lib/libc-2.3.2.so)
==13732==    by 0x3C0833B2: _IO_vfprintf_internal (in /lib/libc-2.3.2.so)
==13732==    by 0x3C08B6F1: _IO_printf (in /lib/libc-2.3.2.so)
==13732==    by 0x8048489: infixInput (in /home/orjanp/informatikk/inf2400/project1/project1)
==13732==
==13732== 5 errors in context 15 of 19:
==13732== Conditional jump or move depends on uninitialised value(s)
==13732==    at 0x3C0A84D9: _IO_file_xsputn@@GLIBC_2.1 (in /lib/libc-2.3.2.so)
==13732==    by 0x3C0833B2: _IO_vfprintf_internal (in /lib/libc-2.3.2.so)
==13732==    by 0x3C08B6F1: _IO_printf (in /lib/libc-2.3.2.so)
==13732==    by 0x8048489: infixInput (in /home/orjanp/informatikk/inf2400/project1/project1)
==13732==
==13732== 5 errors in context 16 of 19:
==13732== Use of uninitialised value of size 4
==13732==    at 0x3C0A84D1: _IO_file_xsputn@@GLIBC_2.1 (in /lib/libc-2.3.2.so)
==13732==    by 0x3C0833B2: _IO_vfprintf_internal (in /lib/libc-2.3.2.so)
==13732==    by 0x3C08B6F1: _IO_printf (in /lib/libc-2.3.2.so)
==13732==    by 0x8048489: infixInput (in /home/orjanp/informatikk/inf2400/project1/project1)
==13732==
==13732== 5 errors in context 17 of 19:
==13732== Use of uninitialised value of size 4
==13732==    at 0x3C01DC71: strlen (mac_replace_strmem.c:189)
==13732==    by 0x3C083434: _IO_vfprintf_internal (in /lib/libc-2.3.2.so)
==13732==    by 0x3C08B6F1: _IO_printf (in /lib/libc-2.3.2.so)
==13732==    by 0x8048489: infixInput (in /home/orjanp/informatikk/inf2400/project1/project1)
==13732==
==13732== 5 errors in context 18 of 19:
==13732== Use of uninitialised value of size 4
==13732==    at 0x8048452: readString (in /home/orjanp/informatikk/inf2400/project1/project1)
==13732==    by 0x80483F7: main (in /home/orjanp/informatikk/inf2400/project1/project1)
==13732==
==13732== 6 errors in context 19 of 19:
==13732== Use of uninitialised value of size 4
==13732==    at 0x80484A0: infixInput (in /home/orjanp/informatikk/inf2400/project1/project1)
==13732==    by 0x8048405: main (in /home/orjanp/informatikk/inf2400/project1/project1)
--13732--
--13732-- supp:   11 Ugly strchr error in /lib/ld-2.3.2.so
==13732==
==13732== IN SUMMARY: 61 errors from 19 contexts (suppressed: 11 from 1)
==13732==
==13732== malloc/free: in use at exit: 8 bytes in 1 blocks.
==13732== malloc/free: 1 allocs, 0 frees, 8 bytes allocated.
==13732==
--13732--     TT/TC: 0 tc sectors discarded.
--13732--            1125 chainings, 2 unchainings.
--13732-- translate: new     1785 (30064 -> 400835; ratio 133:10)
--13732--            discard 1 (23 -> 320; ratio 139:10).
--13732--  dispatch: 63 jumps (bb entries), of which 4852 (7701%) were unchained.
--13732--            103/2130 major/minor sched events.  1912 tt_fast misses.
--13732-- reg-alloc: 362 t-req-spill, 74685+2875 orig+spill uis, 9514 total-reg-r.
--13732--    sanity: 20 cheap, 1 expensive checks.
--13732--    ccalls: 6683 C calls, 55% saves+restores avoided (22026 bytes)
--13732--            8784 args, avg 0.87 setup instrs each (2234 bytes)
--13732--            0% clear the stack (20049 bytes)
--13732--            3150 retvals, 30% of reg-reg movs avoided (1828 bytes)
Segmentation fault

 

Noen tips om hva jeg skal se etter? :)

Endret av Orjanp
Lenke til kommentar

Tror det er mer vanlig å bruke GDB for å finne SIGSEGV-feil:

 

#include <vector>

using namespace std;


int main()
{
 vector<int> v;

 for(int i = 0; i < 10000; i++)
   v[i] = 123; // dette bør gi en SIGSEGV feil.

 return(0);
} // main()

 

g++ -g -Wall program.cpp -o program

 

-g genererer debug-info.

 

gdb program

GNU gdb 6.1

Copyright 2004 Free Software Foundation, Inc.

GDB is free software, covered by the GNU General Public License, and you are

welcome to change it and/or distribute copies of it under certain conditions.

Type "show copying" to see the conditions.

There is absolutely no warranty for GDB.  Type "show warranty" for details.

This GDB was configured as "i686-pc-linux-gnu"...Using host libthread_db library "/lib/libthread_db.so.1".

 

(gdb) run

Starting program: /mnt/home/lars/tests/program

 

Program received signal SIGSEGV, Segmentation fault.

0x08048731 in main () at program.cpp:10

10  v[0] = 123;

(gdb)

 

Da får du vite fil og linjenummer der SIGSEGV'en skjedde.

 

Valgrind er kanskje mer for minnelekasjer og sånnt.

Endret av søppel
Lenke til kommentar

Fikk du disse feilene etter at du fikset segfaulten? Dette ser ut som en feilmelding som følge av at strengen ikke er allokert:

==13732==
==13732== Conditional jump or move depends on uninitialised value(s)
==13732==    at 0x3C0831E9: _IO_vfprintf_internal (in /lib/libc-2.3.2.so)
==13732==    by 0x3C08B6F1: _IO_printf (in /lib/libc-2.3.2.so)
==13732==    by 0x8048489: infixInput (in /home/orjanp/informatikk/inf2400/project1/project1)
==13732==    by 0x8048405: main (in /home/orjanp/informatikk/inf2400/project1/project1)

Men hvis du kompilerer koden din med debug-info (gcc -g) vil Valgrind peke ut linjene hvor det går galt, klart å anbefale :]

Lenke til kommentar
Fikk du disse feilene etter at du fikset segfaulten? Dette ser ut som en feilmelding som følge av at strengen ikke er allokert:

==13732==
==13732== Conditional jump or move depends on uninitialised value(s)
==13732==    at 0x3C0831E9: _IO_vfprintf_internal (in /lib/libc-2.3.2.so)
==13732==    by 0x3C08B6F1: _IO_printf (in /lib/libc-2.3.2.so)
==13732==    by 0x8048489: infixInput (in /home/orjanp/informatikk/inf2400/project1/project1)
==13732==    by 0x8048405: main (in /home/orjanp/informatikk/inf2400/project1/project1)

Men hvis du kompilerer koden din med debug-info (gcc -g) vil Valgrind peke ut linjene hvor det går galt, klart å anbefale :]

Den er fra før jeg fikset problemet. Takk for tipset med gcc -g :)

Endret av Orjanp
Lenke til kommentar

Opprett en konto eller logg inn for å kommentere

Du må være et medlem for å kunne skrive en kommentar

Opprett konto

Det er enkelt å melde seg inn for å starte en ny konto!

Start en konto

Logg inn

Har du allerede en konto? Logg inn her.

Logg inn nå
×
×
  • Opprett ny...