Segmentation fault after running in linux

Posted on 2003-03-11
Medium Priority
Last Modified: 2010-04-15
Hi, i'm making this program in c.. when i run it under DOS.. works perfecly, and when i run it in linux i get segmentation fault.. i can't understand why.. PLEASE help.

void setParam(indice *ind,par p)
       /*here i get segmentation fault after this function is called from
        tag_beg_end_mode function */

/* This function receive setParam function as a parameter */

int tag_beg_end_mode(FILE *aFile,int tag,indice *elem,
                    void (*f)(indice *elemento,par p))
{     etiqueta startMark,endMark;
     if (validate_open_close(aFile,&startMark,&endMark,tag))
     {   par dato;
         ( (*f)(elem,dato) );
          return 1;}
     return 0;

int complete_function(FILE *aFile,int tag,indice *aux)
return ((tag_beg_end_mode(aFile,tag++,aux,setParam));

No matter what i do in setParam function, whenever i use their params i get segmentation fault.
Thanx.. Gus
Question by:gus_m
  • 2
  • 2
  • 2

Expert Comment

ID: 8113008
In this case you are obviously having a null pointer derefenced there.
Try debuging it with GDB or simply print the pointer of ind parameter.

Also, watch for path names in Linux (I see a FILE structure there, is the file succesfully opened?).


Expert Comment

ID: 8113089
My guess is that somehow the 'ind' pointer in setParam() is invalid.  The first thing I would do is ensure that the value of 'ind' is the same inside setParam() as 'aux' is inside complete_function().  If it is, then I'd check to see the call to complete_function() to verify that 'aux' is being set correctly in the call.  If the pointer variables have different values, then you'll need to narrow down (likely inside tag_beg_end_mode()) where the pointer variable is getting overwritten.

Author Comment

ID: 8114040
the complete_function function is called from another

int process_file(char *aFileName,vecIndice *vIndices)
 FILE *file;
 if ((file=fopen(aFileName,"rt"))!=NULL)
  { indice aux;
    int tag=0,i=0;
    while (!reach_end(file)&&complete_function(file,tag,&aux))
    if (reach_end(file))
    { fclose(file);
      return 1;
 }else printf("File Error");
return 0;
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.


Accepted Solution

Nosfedra earned 225 total points
ID: 8114259
This is just awful to read (is this an XML parser?)... Try getting out the function calls from the function parameters and check each parameter before calling the functions. You should do this for debugging purposes and eventually get back to the original coding after sorting out the problem...

As before, the vIndices might be NULL or something. But this is like looking for a needle in the haystack, without the whole code and maybe doing some step by step walking.

Seems like the aux first passed is non initialized (is it a pointer?)

Try the gdb...

Expert Comment

ID: 8114466
It has nothing to do with the problem you describe, but your 'tag' variables don't seem to do anything.  In process_file(), tag is always 0.  In complete_function, you increment tag, but since you return right away, the ++ is useless.

Author Comment

ID: 8115569
I give you the points cause you were the only one who realized that it was a parser, it is very similar.
I've already done what you said, and it works perfecly in DOS but somehow it doesn't work in linux..
Thanx anyway.


PD: if you still want to help me, here is my e-mail. gustavo_mazzei@speedy.com.ar

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

571 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question