C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 09-17-2008, 12:29 PM   #16
Banned
 
master5001's Avatar
 
Join Date: Aug 2001
Location: Visalia, CA, USA
Posts: 3,699
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.
master5001 is offline   Reply With Quote
Old 09-17-2008, 12:39 PM   #17
and the Hat of Ass
 
Join Date: Dec 2007
Posts: 731
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
rags_to_riches is offline   Reply With Quote
Old 09-17-2008, 12:46 PM   #18
Banned
 
master5001's Avatar
 
Join Date: Aug 2001
Location: Visalia, CA, USA
Posts: 3,699
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);
master5001 is offline   Reply With Quote
Old 09-18-2008, 07:12 AM   #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
samGwilliam is offline   Reply With Quote
Old 09-18-2008, 11:16 AM   #20
Mysterious C++ User
 
Join Date: Oct 2007
Posts: 14,099
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.
__________________
Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System
I dedicated my life to helping others. This is only a small sample of what they said:
"Thanks Elysia. You're a programming master! How the hell do you know every thing?"
Quoted... at least once.
Quote:
Originally Posted by cpjust
If C++ is 2 steps forward from C, then I'd say Java is 1 step forward and 2 steps back.
Elysia is offline   Reply With Quote
Old 09-18-2008, 11:20 AM   #21
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 10,368
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
Build + Version Control System: SCons + Bazaar

Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
laserlight is offline   Reply With Quote
Old 09-18-2008, 11:24 AM   #22
Mysterious C++ User
 
Join Date: Oct 2007
Posts: 14,099
Oh. Maybe you're right, now that I do think of it...
__________________
Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System
I dedicated my life to helping others. This is only a small sample of what they said:
"Thanks Elysia. You're a programming master! How the hell do you know every thing?"
Quoted... at least once.
Quote:
Originally Posted by cpjust
If C++ is 2 steps forward from C, then I'd say Java is 1 step forward and 2 steps back.
Elysia is offline   Reply With Quote
Reply

Tags
malloc, segementation fault, segfault

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 01:37 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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