Thread: ctype segmentation fault

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    271

    ctype segmentation fault

    Hi all,

    The program I wrote compiles, but it always causes a segmentation fault in runtime and I have no idea why.

    Code:
    #include <iostream>
    #include <locale>
    
    using namespace std;
    
    const unsigned int MAXBUF = 2048;
    
    int main(int argc, char* argv[])
    {
      const ctype<char>& ct_de = use_facet<ctype<char> >(locale("de_DE.utf8"));
    
      char buf[MAXBUF];
      char* c;
    
      cin.imbue(locale::classic());
      cout.imbue(locale::classic());
      while(cin.getline(buf, MAXBUF))
      {
        c = buf;
        while(*c)
          {
    	cout << ct_de.tolower(*c++);
          }
        cout << endl;
      }
      return 0;
    }
    I'm running this on an Ubuntu whose default locale is "en_EN.UTF-8".

    A libation and hecatomb for anyone who can help.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    g++ -g prog.cpp
    gdb a.out

    When you're in gdb, type 'run' to run the program.
    When the program crashes, type 'bt' to find out where you are.

    That will show you the line of code which failed.
    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.

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    271
    Yes, I tried that. I'm still trying to get the hang of gdb, but anyway, I'm still not proficient enough to understand what I'm looking at.

    I was trying to ween myself off MSVC, but I had to go back, just to see what was going on, and even there, the error message is inscrutable.

    Code:
    First-chance exception at 0x004358b1 in de_tolower.exe: 0xC0000005: Access violation reading location 0xfeeefef6.
    Unhandled exception at 0x004358b1 in de_tolower.exe: 0xC0000005: Access violation reading location 0xfeeefef6.
    The only thing I did to the code was change the ctype definition so that it would conform to Microsoft locale settings:

    Code:
    const ctype<char>& ct_de = use_facet<ctype<char> >(locale("German_Germany"));
    I tried to step into the code as much as possible in debug mode but it would not go beyond "xlocale":

    Code:
    	_Elem tolower(_Elem _Ch) const
    		{	// convert element to lower case
    		return (do_tolower(_Ch));
    		}
    I'm stumped.

    To be honest, it would be much easier for me just to set up my own conversion table for the upper ASCII, but since c++ had localized libraries (which seem to be platform dependent to a certain degree), I thought it might be a good idea to learn how to use them. If no solutions turn up, I'll just have to manually encode the case conversion.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > locale("German_Germany")
    Do this first, assign the result to a variable of the appropriate type, then check the result before doing anything else.
    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.

  5. #5
    Registered User
    Join Date
    Apr 2007
    Posts
    1
    Code:
    const ctype<char>& ct_de = use_facet<ctype<char> >(locale("de_DE.utf8"));
    this line is giving it most probably

  6. #6
    Registered User
    Join Date
    Oct 2005
    Posts
    271
    THANK YOU!!!

    But would you mind telling me why? I mean, what would be the difference between creating a locale object, then passing it to use_facet, and doing "locale("de_DE.utf8")" inside use_facet?

    Also, if you don't mind answering another question as well, I've discovered that to REALLY use C/C++, you need to understand machine level code (or assembly or whatever). I've run into so many of these snafus ever since I learned C/C++ that I am now sorely tempted to dive into assembly. Do you guys think this is a good idea? Or could you suggest a better alternative that would enhance my skills at debugging errors, most especially segmentation faults?

  7. #7
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    I think the issue here is that use_facet returns a const reference, which means you have to initialize ct_de (rather than assign it) and that means you can't very nicely put use_facet... inside a try/catch construct. i.e.:
    Code:
    try
    {
      const ctype<char>& ct_de = use_facet<ctype<char> >(locale("de_DE.utf8"));
    }
    catch (const std::runtime_error&)
    {
      //error handling
    }
    
    //other code
    //whoops, ct_de is out of scope
    You can however do:
    Code:
      locale loc;
      try
      {
       loc = locale("de_DE");
      }
      catch (const std::runtime_error& err)
      {
         //error handling
      }
    
      const ctype<char>& ct_de = use_facet<ctype<char> >(loc);;
    Although I admit I'm not sure how you'd handle such an error (and I don't rightly understand why using "de_DE" throws "bad locale name," but I don't know much about locales)
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > Do you guys think this is a good idea?
    No it isn't.

    Peering at the ASM to help you debug your "understanding" of C++ will at best only tell you how your compiler behaves, not what is expected of standard C++.

    Learning C++ is considerably harder than simply learning the syntax. As you're finding out, making something compile is trivial to making something which actually works reliably.

    Might I ask what book(s) you learnt from?

    > most especially segmentation faults?
    Take small known working programs, add a 'fault' which you suspect will cause a problem, then see if you can 'find' the problem using the debug tools.
    Like everything else, it's just down to practice with the tools available.
    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.

  9. #9
    Registered User
    Join Date
    Oct 2005
    Posts
    271
    I first learned c++ programming through two books "on to c++" by Patrick Winston and "c++ in plain English" by Brian Overland. I actually managed to read them almost cover to cover.
    After that, I just used a bunch of reference books which I look at whenever the need arises. "The C++ Standard Library" by Josuttis is my main resource, as it probably is for many other people.

    Thanks, JaWib, I kind of understand what you're saying, and I'll look into my references for a better understanding.

    And Salem, thanks for setting me aright on the debugging issue.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why am I getting segmentation fault on this?
    By arya6000 in forum C++ Programming
    Replies: 6
    Last Post: 10-12-2008, 06:32 AM
  2. Segmentation fault
    By bennyandthejets in forum C++ Programming
    Replies: 7
    Last Post: 09-07-2005, 05:04 PM
  3. Segmentation fault
    By NoUse in forum C Programming
    Replies: 4
    Last Post: 03-26-2005, 03:29 PM
  4. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 01:33 PM
  5. Segmentation fault...
    By alvifarooq in forum C++ Programming
    Replies: 14
    Last Post: 09-26-2004, 12:53 PM