Thread: malloc() resulting in a SegFault?!

  1. #16
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Fair enough.

    Example:
    Code:
    inline template<typename AnyTypeYouWant>
    AnyTypeYouWant *malloc_ptr(size_t x)
    {
      return reinterpret_cast<AnyTypeYouWant *>(malloc(size_t * sizeof(AnyTypeYouWant)));
    }
    
    inline template<typename AnyTypeYouWant>
    AnyTypeYouWant &malloc_ref(size_t x)
    {
      AnyTypeYouWant *ptr = malloc_ptr(x);
    
      if(!ptr)
      {
        throw std::bad_alloc("Null reference");
      }
    
      return *ptr;
    }
    Alright, now everyone is happy. I have added a healthy level of sarcasm to a few lines of code (which always brightens my day). I still say you are just a quick one. Of course, your name is laserlight, I should expect you to be swift.

  2. #17
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Quote Originally Posted by laserlight View Post
    Note that that FAQ pertains to C. This is C++, so casting the return value of malloc() is not bad style, but rather is required.
    DOH! My bad! Of course, I agree that one should not be using malloc in a C++ environment any way

  3. #18
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Placement new... But then again, who likes to explicitly call a destructor?

    Example:
    Code:
    int *x = new(malloc(sizeof(*x))) int;
    
    // At which point this line is perfectly acceptable
    free(x);

  4. #19
    Registered User samGwilliam's Avatar
    Join Date
    Feb 2002
    Location
    Newport
    Posts
    382
    Quote Originally Posted by master5001 View Post

    Note: That is for the benefit of the OP and other people with similar misconceptions, not matsp who is well aware of these facts. It is important that people know by design you should not mix and match your C and C++ dynamic memory allocations.
    I must admit that my coding style is a hybrid of C and C++. I always use new and delete, but I've never bothered with templates or try/catch and I pass by reference the C way. I'm still in the habit of using the .h when including too.
    Current Setup: Win 10 with Code::Blocks 17.12 (GNU GCC)

  5. #20
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It doesn't really matter what the contents is in a header. The extension is optional. You can use .h in C++, as well as C. I wouldn't say it's wrong or bad practice.
    And especially in C++, I would avoid saying "passing by reference" if you aren't actually doing it (ie passing by pointer), because in C++ there are references and pointers.

    But you should really try out templates, at the very least. Very powerful stuff right there.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #21
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Elysia
    It doesn't really matter what the contents is in a header. The extension is optional. You can use .h in C++, as well as C. I wouldn't say it's wrong or bad practice.
    I suspect that samGwilliam is referring to a habit of including the C standard headers instead of their C++ versions.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #22
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Oh. Maybe you're right, now that I do think of it...
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. malloc + segmentation fault
    By ch4 in forum C Programming
    Replies: 5
    Last Post: 04-07-2009, 03:46 PM
  2. Malloc -segfault
    By ganesh bala in forum C Programming
    Replies: 8
    Last Post: 02-17-2009, 08:08 AM
  3. Is there a limit on the number of malloc calls ?
    By krissy in forum Windows Programming
    Replies: 3
    Last Post: 03-19-2006, 12:26 PM
  4. Malloc and calloc problem!!
    By xxhimanshu in forum C Programming
    Replies: 19
    Last Post: 08-10-2005, 05:37 AM
  5. malloc() & address allocation
    By santechz in forum C Programming
    Replies: 6
    Last Post: 03-21-2005, 09:08 AM

Tags for this Thread