![]() |
| | #16 |
| Banned Join Date: Aug 2001 Location: Visalia, CA, USA
Posts: 3,699
| 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;
}
|
| master5001 is offline | |
| | #17 |
| and the Hat of Ass Join Date: Dec 2007
Posts: 731
| |
| rags_to_riches is offline | |
| | #18 |
| Banned 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 | |
| | #19 |
| Budding Synth Programmer Join Date: Feb 2002 Location: Trefforest
Posts: 368
| 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 | |
| | #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:
| |
| Elysia is offline | |
| | #21 | |
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 10,368
| Quote:
__________________ 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 | |
| | #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:
| |
| Elysia is offline | |
![]() |
| Tags |
| malloc, segementation fault, segfault |
| Thread Tools | |
| Display Modes | |
|
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 |