malloc() resulting in a SegFault?!

This is a discussion on malloc() resulting in a SegFault?! within the C++ Programming forums, part of the General Programming Boards category; 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> ...

  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
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,650
    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
    Budding Synth Programmer samGwilliam's Avatar
    Join Date
    Feb 2002
    Location
    Trefforest
    Posts
    368
    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.
    MSVC++ 6.0

  5. #20
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,543
    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
    21,632
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #22
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,543
    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.

Page 2 of 2 FirstFirst 12
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, 07: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, 11:26 AM
  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, 08:08 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21