Skip to main content
GameDev.net gamedev.net
🔒 Locked

templated new

Started by IFooBar Dec 16, 2002 at 5:02 PM 9 replies 450+ views
Original Post
IFooBar
IFooBar
Hello Im trying to make a template function that will make a new object and return an hresult based on success or failure. (I want to try and do this without overloading new)
    
template<class type>
inline HRESULT t_s_new(type *p)
{
    if((p = new type) == NULL)
        return E_OUTOFMEMORY;
    return S_OK;
}

// Macros for creating and deleteing new stuff.

#define s_new(type, obj) t_s_new<type>(obj)

#define s_delete(obj) if(obj){delete obj; obj = null;}
  

//now when I create I do the following it gives me an access violation

      
int *i = NULL;
s_new(int, i);

*i = 5;

s_delete(i);
    
can anyone fix this?... thanks
::: Al:::
[Triple Buffer V2.0] - Resource leak imminent. Memory status: Fragile [edited by - alfmga on December 16, 2002 6:06:00 PM]
[size=2]aliak.net
Neophyte
Neophyte
Your problem is that the p = new type in t_s_new only sets the local p, and not the i in your example.
You''ll need to either use a pointer to a pointer, or a pointer-reference.
For instance:

  
template <class type>
inline HRESULT t_s_new(type*& p) {
if((p = new type) == NULL)
return E_OUTOFMEMORY;
return S_OK;
}

// Or

template <class type>
inline HRESULT t_s_new(type** p) {
if ((*p = new type) == NULL)
return E_OUTOFMEMORY;
return S_OK;
}




-Neophyte
Void
Void
On a compliant C++ compiler (non VC++ 6), the new operator will throw an OutOfMemory exception when it cannot allocate memory so your assertion macro is quite redundant.

Learn about exception safety and use exceptions instead of return codes for things like this.
Neophyte
Neophyte
You''re partly right there. The ANSI standard specifies that the default behaviour of failure on a call to ''new'' should be to throw a bad_alloc exception. But that is only the default behaviour, and Visual C++ is not the only compiler out there that''ll return a NULL-pointer rather than throw an exception on failure (most, if not all, the other compilers are rather older though).
But your point is a good one, there doesn''t seem to be a very good reason for that kind of wrapping, especially since there is no guarantee that the function will actually be inlined.


-neophyte
IFooBar
IFooBar
But I''m checking the value of p after trying to create it. Wont p == null if new cannot allocate memory for it? And why do you say that there is no guarentee that the function will be inlined? So would it be better if I did this instead

  
template <class type>
inline HRESULT t_s_new(type **p)
{
try {
*p = new type;
} catch (bad_alloc b ) {
return E_OUTOFMEMORY;
}
return S_OK;
}

thanks for teh warning. Although could you please explain why the inline thing is not guarenteed? what if use __forceinline instead?

thanks for replies


:::Al:::
[Triple Buffer V2.0] - Resource leak imminent. Memory status: Fragile
[size=2]aliak.net
Qw3r7yU10p!
Qw3r7yU10p!
some compilers will throw, some won''t.


  
template <class type>
inline HRESULT t_s_new(type **p)
{
*p = new(std::nothrow) type;
if(!*p)
return E_OUTOFMEMORY;
return S_OK;
}

this will make sure it does what you want.

all this is only useful for types with a default constructor.

i don''t worry about the whole inlining thing unless i can show it''s a problem.
Void
Void
Yah, the OutofMemory exception should be std::bad_alloc. Getting forgetful here!

The problem is you are trying to force the portable new operator to return a Win32 specific "Out of memory" error code. Let the exception be thrown. If you need, catch it somewhere more appropriate.

Read Effective C++. (I forget which item)
civguy
civguy
quote:
Original post by alfmga
And why do you say that there is no guarentee that the function will be inlined?
Perhaps because it''s a fact. "inline" is just a suggestion for the compiler to inline that function. If it doesn''t want to inline it, it doesn''t have to.
Neophyte
Neophyte
Even __forceinline won''t force the compiler to inline the function, but it is a stronger hint to the compiler than inline is. But AFAIK __forceinline is a Visual C++ only extension.
On the other hand, the function you''ve written is a good candidate for inlining, and I doubt a compiler with optimization turned on will refuse to inline it.

-Neophyte
IFooBar
IFooBar
OK thanks . Ill keep on this in mind. Also on another note, If you use VC++ you can tell if a function is not inlined at compile time, (useful no?), but you have to use __forceinline

Form MSDN:
"If the compiler cannot inline a function declared with __forceinline, it generates a level 1 warning (4714)."


:::Al:::
[Triple Buffer V2.0] - Resource leak imminent. Memory status: Fragile
[size=2]aliak.net

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.