• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Segmentation Fault

 
Ranch Hand
Posts: 42
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all,

I am currently working on a school project that is creating a parser and it is using a doubly linked-list to add tokens into the symbol table (I know very brief summary if you need more information feel free to ask). I recently have run into the issue of a segmentation fault. I have discovered where the fault lies but have not found a way to fix it. I have tried putting in if-statements to check for NULL and greater than 0 but they did nothing to stop the problem. I have tried using alternative ways and I get rid of the segmentation fault but my code fails in other ways. Please let me know if there is anything else that is thought of. I have provided a small portion of the code below.

 
Ranch Hand
Posts: 123
1
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rhyeca

You say you've found the problem; is that what the comment 'SYNTAX ERROR LIES HERE' is referring to? Can you show us the compiler output, or do you mean that you think it's this line that's causing the segmentation fault (which is different to a syntax error)?

Can you show us the code that defines/sets newChar?

By the way, assuming newChar contains a valid string (and the segmentation fault might indicate that it doesn't), strcpy() includes the '\0' so you should remove line 12.

Cheers
John
 
John Matthews
Ranch Hand
Posts: 123
1
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
...and can you show the definition of symbolTable? I reckon it might be the name field that's the problem.
 
Rhyeca Riley
Ranch Hand
Posts: 42
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Apologies for the late reply, did not have my laptop on me at work unfortunately.

Here is the symbolTable


and here is where newChar is

 
Rhyeca Riley
Ranch Hand
Posts: 42
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also adding some other variables you may see... Thank you again

 
Rhyeca Riley
Ranch Hand
Posts: 42
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How I can tell it is the lines that are causing the seg. fault is because if I remove them my program no longer has any seg. faults but it does not produce the correct outputs for the rest of the outputs
 
John Matthews
Ranch Hand
Posts: 123
1
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok - thanks for the additional info. But unless I'm missing something (not unlikely) it's still not obvious what's going wrong. If it's not possible to post the whole program, I would suggest adding a few printfs eg. something like
before the strlen(newChar) just to check that the newChar string is what you think it is.

You should at least get output from the first printf, but if you get a segfault after that then it indicates there is a problem with newChar eg. it's not a 'proper' string and is missing the '\0' at the end.

(Seeing the whole program would really help though.)
 
John Matthews
Ranch Hand
Posts: 123
1
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By the way, I can see you are using 'new' to allocate memory for name. So why are you using malloc() to allocate memory for table? This must be a C++ program if 'new' is available, so you should be using 'new' rather than malloc() everywhere unless there's a good reason not to. And it would probably be easier to use std::string instead of char* and strcpy() etc.

Back to the problem - you didn't show where newChar (ie. the addToken() argument) is set. That's crucial - calling strlen() on something which isn't a valid string is likely to result in a segfault. To clarify - show the code before the call to addToken() that sets the argument passed to addToken() that becomes newChar in what you've posted already.
 
Bartender
Posts: 20940
127
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

John Matthews wrote:By the way, I can see you are using 'new' to allocate memory for name. So why are you using malloc() to allocate memory for table? This must be a C++ program if 'new' is available, so you should be using 'new' rather than malloc() everywhere unless there's a good reason not to. And it would probably be easier to use std::string instead of char* and strcpy() etc.



Yes. "new" uses malloc and delete uses free. And regardless of what language you're using, it's a very good practice to not randomly access functions at different layers, so either use all new/delete or all malloc/free.

Most of the sample code could, in fact, be made part of a constructor method for symbolTable. Or, according to the C++ conventions, "class SymbolTable".
 
Rhyeca Riley
Ranch Hand
Posts: 42
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all for the help and suggestions. I discovered my fault lied in my header file rather than the file I was working in. I had two variables being declared with the same name.
 
Marshal
Posts: 65114
247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well done finding the real cause, and telling us. It is annoying that the compiler didn't alert you to that error.
 
Tim Holloway
Bartender
Posts: 20940
127
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Well done finding the real cause, and telling us. It is annoying that the compiler didn't alert you to that error.



I am pretty sure it would have, but the 2 definitions were probably in different namespaces. In which case, C++ expects that you know what you're doing. C++ expects a lot, sometimes.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!