This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

RE: strcpy(stc->val,var) = Segmention Fault


Uílton O. Dutra wrote:

> I create simple program to read configurations files.
> With Bcc32-Win98 my prog works fine, but on Gcc-Linux I got
> "Segmention Fault" in "strcpy(stc->val,var)". 

This doesn't look linux-specific but lines 49-51 is where it first falls
over for me:

    stc->next=(struct symtable  *) malloc (sizeof (symtable));
    stc=stc->next;
    strcpy(stc->var," "); strcpy(stc->var," ");

You're allocating a new symtable and then attempting to write to use one of
its member pointers without assigning it first or allocating it storage;
you'd need a

    stc->var = (char*)malloc(<something>);

before last strcpy, but I can't tell you what because it's not obvious to me
what you're doing there! The second strcpy, for example, overwrites the
first. (It's possible you meant to use one ->var and one ->val.)

It strikes me this structure is going to be a nightmare to free()
afterwards. Remember that malloc() doesn't zero the contents of the memory
it's allocated; at the very least you should cmalloc() your symtables (or
malloc then

    memset(stc,0,sizeof(symtable))

them) so you can see which pointers you have used and should free() and
which you haven't (since they're NULL).

It's posslble I've overlooked some things here - I haven't given this a lot
of thought. Good luck making it work!

Rup.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]