2

This happens with EVERY class I try to make in C++. Migrating from java, I find problems mainly in making classes. I run valgrind and it's in the constructor, it appears to be.

==30214== Memcheck, a memory error detector
==30214== Copyright (C) 2002-2011, and GNU GPL'd, by Julian Seward et al.
==30214== Using Valgrind-3.7.0 and LibVEX; rerun with -h for copyright info
==30214== Command: ./CoC
==30214== 
==30214== 
==30214== Process terminating with default action of signal 11 (SIGSEGV)
==30214==  Bad permissions for mapped region at address 0x404B4F
==30214==    at 0x4C2B9EC: strcat (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==30214==    by 0x404220: Model::Model(std::string) (in /home/kronus/Apollo/CoC)
==30214==    by 0x402617: main (in /home/kronus/Apollo/CoC)

As you can see I'm trying to call the constructor of this model class into the main method. Here's the code for the constructor

Model::Model(std::string filename)
{
m_TotalFaces = 0;
m_model = lib3ds_file_load(filename.c_str());
    // If loading the model failed, we throw an exception
    if(!m_model)
    {
           throw strcat("Unable to load ", filename.c_str());
    }
}

When it's called it closes with a segmentation fault. Important: I have declared the class inside the header file. This is when I get the error. I put the class inside the source file and it runs fine. What am I doing wrong?

autistic
  • 1
  • 3
  • 35
  • 80
Larry Hart
  • 43
  • 5
  • 2
    the C++-no-need-to-use-C-string-manipulation approach for the throw would be throw std::runtime_error("Unable to load " + filename) – sylvain.joyeux Mar 24 '13 at 06:58

2 Answers2

9

strcat attempts to write the string pointed to by the second argument at the end of the string pointed to by the first argument. Since the first argument is a string literal (which should be considered read only), you get a nasty segfault.

I suggest learning C++ as though it's an entirely different language to Java, because otherwise you may think that similar features function the same. That's dangerous. A monkey can learn Java by mashing it's face on the keyboard. C++ has undefined behaviour which may appear to function correctly on your machine, but launch nuclear missiles on another.

autistic
  • 1
  • 3
  • 35
  • 80
  • almost third answer with you, and you explain very nice. – Grijesh Chauhan Mar 24 '13 at 06:49
  • You're amazing haha. Thanks a ton. Gonna try not to flood this site with my silly questions, but I learned something new! Hahah. Think you can tell me why it works when it's called from the source? As this was called from the header file. Cause it keeps happening to me.. – Larry Hart Mar 24 '13 at 08:31
  • Undefined behaviour... Sometimes undefined behaviour *appears to work*, other times it doesn't. This is partly why it's so dangerous. Which book are you reading? – autistic Mar 24 '13 at 10:20
  • This one. http://www.bookholders.com/images/books/super/0135289106.jpg It's just.. every time I try to make a class it does this. Now it looks like any sort of class that has some kind of memory being changed. So I cannot do that to a class in C++? – Larry Hart Mar 24 '13 at 10:32
  • You can't, nor you shouldn't, at least in C++. strcat would be acceptable in C code, but in C++ code there are different ways to do things (see sylvian.joyeux's comment to your question, for example). "*The following error was encountered while trying to retrieve the URL: http://www.bookholders.com/images/books/super/0135289106.jpg* ...". Please tell me the title of the book, and the authors who wrote it. – autistic Mar 24 '13 at 10:38
  • C++ How To Program by H.M Dietel/P.J Dietel. So with this class. I want it in it's own separate source. How would I go about being able to do this? – Larry Hart Mar 24 '13 at 10:49
  • So, put it in it's own separate source file. What's the problem with that? Do you need to know where the manual for your compiler is? Which compiler are you using? – autistic Mar 24 '13 at 10:53
  • I do that but when I add a header file to it so I can use it as an 'entity' (if I'd call it that..) that's when I get this error. – Larry Hart Mar 24 '13 at 10:57
  • Are you getting any warnings when you compile your code? Please answer my questions... *all* of them. – autistic Mar 24 '13 at 11:05
  • I am sorry haha I'm using GCC, and no warnings. – Larry Hart Mar 24 '13 at 11:09
2

You are appending to a constant string that is wrong:

strcat("Unable to load ", filename.c_str());
         ^ you can't append to constant

Read this: c++ exception : throwing std::string
You may want to avoid using strings as exception classes because they themselves can throw an exception during use.

second: What type should I catch if I throw a string literal?

Community
  • 1
  • 1
Grijesh Chauhan
  • 57,103
  • 20
  • 141
  • 208