Thread: Segmentation Fault Problem: Urgent Help

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    5

    Segmentation Fault Problem: Urgent Help

    hei...ahm..this is not exactly protocol, but i need to solve this out imediatly. And yes, i do know you ppl dont do assigments, but hey, this is only one lil problem

    i'm getting a segmentation fault on my program, and i cant see what's wrong. Ppl with more experience as you guys could just take a glance at it and see the problem. I really need that expertise right now, plz

    ahm, i'll post the entire code (sorry, i dont have a ftp or site to link it to), so, plz, patience with me.

    and, ahm, i realize there is a problem with the `switch', but ahm, i dont think thast the main problem.

    thanks for the help, sorry for the incovenience.

    The objective of this program is to make array operations. From inserting prositions and elements to counting them out. It is actually pretty simple. Ahm, sorry but most of the variables and function names are in portuguese, and, it'd take too long to translate them all. The function "saida" (simple translation would be "exit", but what this function does is print the results of other functions). This is just a simple feedback, and i'm very sorry if it sounds like i want you ppl to make the whole assignment for me. Thats not it. I just dont see what make that "segfault" error.

    If you compile this code on Dev C++ you'll probably get a Memory Error stating it cant be written or read coz its being used for another program or it cant be accessed. On Anjuta it gives me Segmentation Fault. The simpler explanation for it would be i'm trying to access a vector thast not there, or i didnt put a "&" somewhere. hehe, if thats the case, sorry, coz its stupid

    what we did was initialize the whole array with zeros, and when the user wants to insert an element on it, he uses the "inserecomeco" (meaning, basically, "insertstart") function. Maybe thats a wrong approach, but work with me on it for now, and lets get a mistake at a time. Starting witht the segmentatio fault

    i wellcome any sugestion or comment. thanks

    here is the code.



    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define MAX 100
    
    int VOIDINT=666;/*used when theer is no int to print*/
    char VOIDCHAR='z';/*used when ther is no char to print*/
    
    
    int count=0;/* count is the variable that checks if the array is full*/
    int exec=0;/*counts the number of executed lines*/
    char cheio [10] ="vetorcheio";/*states a full array (and prints "arrayfull"*/
    char vaziu [10] ="vetorvazio";/*states an empty array (and prints "emptyarray"*/
    char fora [12] ="foradolimite";/*indicates out of array limits (and prints "outoflimit"*/
    char nvaziu [13] ="vetornaovazio";/*indicates that the array is not empty*/
    
    
    void saida(int respexec, int respint, char respchar)
    {
       if (respint==VOIDINT)
          printf ("x%d  %s",respexec, respchar);
    
       if (respchar==VOIDCHAR)
          printf ("x%d  %d",respexec, respint);
    
       if ((respint==VOIDINT) && (respchar==VOIDCHAR))
          printf ("x%d",respexec);
    }
    
    void inserecomeco (int v[], int num, int n)
    {
       int i;
       if (count==n)/*checks if array is full or not*/
       {
          exec=4;
          saida (exec, VOIDINT, *cheio);
       }
       else
       {
          for (i=n-1; i>0; i--)/*moves the whole array one position to the right*/
          {
             v[i]=v[i-1];
          }
          v[0]=num;
          count++;
          exec=(2*n+6);
          saida (exec, VOIDINT, VOIDCHAR);
       }
    }
    
    void inserefim (int v[], int num, int n)
    {
       if (count==n)/*verifica se o vetor esta cheio*/
       {
          exec=3;
          saida (exec, VOIDINT, *cheio);
       }
       else
       {
          v[count+1]=num;
          count++;
          exec=5;
          saida (exec, VOIDINT, VOIDCHAR);
       }
    }
    
    void removecomeco (int v[], int n)
    {
       int i;
       if (count==0) /*checks if array is empty*/
       {
          exec=4;
          saida (exec, VOIDINT, *vaziu);
       }
       else
       {
          for (i=0; i<n; i++);
          {
             v[i]=v[i+1];
          }
       count--;
       exec=(2*n+5);
       saida (exec, VOIDINT, VOIDCHAR);
       }
    }
    
    void removefim (int v[], int n)
    {
       int i;
       if (count==0) /*same as above*/
       {
          exec=4;
          saida (exec, VOIDINT, *vaziu);
       }
       else
       {
          for (i=n-1; i>0; i--)/*moves the whole vector one position to the right*/
          {
             v[i]=v[i-1];
          }
          count--;
          exec=(2*n+5);
          saida (exec, VOIDINT, VOIDCHAR);
       }
    }
    
    void removeposicao (int v[], int pos, int n)
    {
       int i;
       if ((pos>n) || (pos<0) || (count==0))
       {
          exec=4;
          saida (exec, VOIDINT, *fora);
       }
       else
       {
          for (i=pos; i<n; i++)
          {
             v[pos]=v[pos+1];
          }
       count--;
       exec=(2*n+5);
       saida (exec, VOIDINT, VOIDCHAR);
       }
    }
    
    void insereposicao (int v[], int pos, int num, int n)
    {
       int i;
       if ((pos>n) || (pos<0))
       {
          exec=4;
          saida (exec, VOIDINT, *fora);
       }
       if (count==n)
       {
          exec=5;
          saida (exec, VOIDINT, *cheio);
       }
       for (i=n-1; i>pos; i--)
       {
          v[i]=v[i-1];
       }
       v[pos]=num;
       count++;
       exec=(2*n+7);
       saida (exec, VOIDINT, VOIDCHAR);
    }
    
    void elementoposicao (int v[], int pos, int n) 
    {
       if ((pos>n) || (pos<0))
       {
          exec=3;
          saida (exec, VOIDINT, *fora);
       }
       else
       {
          exec=3;
          saida (exec, v[pos], VOIDCHAR);
       }
    }
    
    void tamanho () // gets the size of the array
    {
       exec=2;
       saida (exec, count, VOIDCHAR);
    }
    
    void ultimo (int v[]) //gets the last element of the array
    {
       if (count==0)
       {
          exec=3;
          saida (exec, VOIDINT, *vaziu);
       }
       else
       {
       exec=3;
       saida (exec, v[count], VOIDCHAR);
       }
    }
    
    void primeiro (int v[]) //gets the first element of the array
    {
       if (count==0)
       {
          exec=3;
          saida (exec, VOIDINT, *vaziu);
       }
       else
       {
       exec=3;
       saida (exec, v[0], VOIDCHAR);
       }
    }
    
    void vazio () // if array is empty, it prints it is, if its isnt, it prints its not :)
    {
       if (count==0)
       {
          exec=3;
          saida (exec, VOIDINT, *vaziu);
       }
       else
       {
       exec=3;
       saida (exec, VOIDINT, *nvaziu);
       }
    }
    
    
    int main ()
    
    {
       int v[MAX]={0};
       int n, pos, num;
       char nomefunc;
       char a [] ="inserecomeco", b [] = "inserefim", c [] = "removecomeco", d 
    [] = "removefim";
       char e [] = "removeposicao", f [] = "insereposicao", g [] = 
    "elementoposicao", h [] = "tamanho";
       char i [] = "ultimo", j [] = "primeiro", k [] = "vazio";
       do
       {
          printf ("Insira o tamanho do vetor: ");/*"Insert the vectos size"*/
          scanf ("%d", &n);
       }while ((n<=0) && (n>100));
    
       printf ("\nInsira a funcao desejada: ");/*Insert the function name*/
       scanf ("%s", &nomefunc);
       switch (nomefunc)
       {
          case 'a':
             printf ("A funcao escolhida foi: InsereComeco.\nDigite o numero a 
    ser inserido: ");/*the chosen function was "InsertStart"*/
             scanf ("%d", &num);
             inserecomeco (v, num, n);
             break;
    
          case 'b':
             printf ("A funcao escolhida foi: InsereFim.\nDigite o numero a ser 
    inserido: ");
             scanf ("%d", &num);
             inserefim (v, num, n);
             break;
    
          case 'c':
             printf ("A funcao escolhida foi: RemoveComeco.");
             removecomeco (v, n);
             break;
    
          case 'd':
             printf ("A funcao escolhida foi: RemoveFim.");
             removefim (v, n);
             break;
    
          case 'e':
             printf ("A funcao escolhida foi: RemovePosicao.\nDigite a posicao a 
    ser removida: ");
             scanf ("%d", &pos);
             removeposicao (v, pos, n);
             break;
    
          case 'f':
             printf ("A funcao escolhida foi: InserePosicao.\nDigite a posicao e 
    o numero a ser inserido(nao se esqueca de colocar um espaco entre eles): ");
             scanf ("%d", &pos);
             scanf ("%d", &num);
             insereposicao (v, pos, num, n);
             break;
    
          case 'g':
             printf ("A funcao escolhida foi: ElementoPosicao.\nDigite a posicao 
    a ser verificada: ");
             scanf ("%d", &pos);
             elementoposicao (v, pos, n);
             break;
    
          case 'h':
             printf ("A funcao escolhida foi: Tamanho.");
             tamanho ();
             break;
    
          case 'i':
             printf ("A funcao escolhida foi: Ultimo.");
             ultimo (v);
             break;
    
          case 'j':
             printf ("A funcao escolhida foi: Primeiro.");
             primeiro (v);
             break;
    
          case 'k':
             printf ("A funcao escolhida foi: Vazio.");
             vazio ();
             break;
    
          default:
             printf ("Essa funcao nao eh valida.");
             break;
    
       }
       system ("pause");
       return 0;
    }

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    Here is a problem that I noticed, I didn't have time to read the whole thing
    Code:
    void saida(int respexec, int respint, char respchar)
    {
       if (respint==VOIDINT)
          printf ("x%d  %s",respexec, respchar);
    respchar is a char that is a single charactor so you would need to use %c to print that char. When you use %s it's treated as a pointer to a char and since it's value likely isn't a vald pointer it will give a segmentation fault.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Perhaps listening to your compiler
    Code:
    $ gcc -W -Wall -ansi -pedantic -O2 foo.c
    foo.c: In function ‘saida’:
    foo.c:21: warning: format ‘%s’ expects type ‘char *’, but argument 3 has type ‘int’
    foo.c: In function ‘main’:
    foo.c:223: warning: unused variable ‘k’
    foo.c:223: warning: unused variable ‘j’
    foo.c:223: warning: unused variable ‘i’
    foo.c:222: warning: unused variable ‘h’
    foo.c:221: warning: unused variable ‘g’
    foo.c:221: warning: unused variable ‘f’
    foo.c:221: warning: unused variable ‘e’
    foo.c:220: warning: unused variable ‘d’
    foo.c:219: warning: unused variable ‘c’
    foo.c:219: warning: unused variable ‘b’
    foo.c:219: warning: unused variable ‘a’
    Also
    > char cheio [10] ="vetorcheio";/*states a full array (and prints "arrayfull"*/
    Remove the number between the brackets, let the compiler do the counting.
    These strings do NOT have a \0 on the end.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Mar 2006
    Posts
    5
    Quote Originally Posted by Salem
    Perhaps listening to your compiler
    Code:
    $ gcc -W -Wall -ansi -pedantic -O2 foo.c
    foo.c: In function ‘saida’:
    foo.c:21: warning: format ‘%s’ expects type ‘char *’, but argument 3 has type ‘int’
    foo.c: In function ‘main’:
    foo.c:223: warning: unused variable ‘k’
    foo.c:223: warning: unused variable ‘j’
    foo.c:223: warning: unused variable ‘i’
    foo.c:222: warning: unused variable ‘h’
    foo.c:221: warning: unused variable ‘g’
    foo.c:221: warning: unused variable ‘f’
    foo.c:221: warning: unused variable ‘e’
    foo.c:220: warning: unused variable ‘d’
    foo.c:219: warning: unused variable ‘c’
    foo.c:219: warning: unused variable ‘b’
    foo.c:219: warning: unused variable ‘a’
    Also
    > char cheio [10] ="vetorcheio";/*states a full array (and prints "arrayfull"*/
    Remove the number between the brackets, let the compiler do the counting.
    These strings do NOT have a \0 on the end.

    thanks both of you, i'll try it out. But, ahm, what can i say, i didnt use those gcc settings. and Dev++ sux...

    thx

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation fault problem
    By odedbobi in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2008, 03:36 AM
  2. Strange segmentation fault
    By Ron in forum C Programming
    Replies: 24
    Last Post: 06-15-2008, 02:10 PM
  3. Segmentation Fault hmm?
    By pobri19 in forum C Programming
    Replies: 4
    Last Post: 05-03-2008, 07:51 AM
  4. Replies: 0
    Last Post: 07-26-2007, 09:55 AM
  5. [C++] Segmentation Fault {Novice C++ Programmer}
    By INFERNO2K in forum C++ Programming
    Replies: 24
    Last Post: 06-08-2005, 07:44 PM