Never returning a NULL pointer

S

Suraj

Can someone suggest me a way of never returning a NULL pointer.

Say I have a class
class X
{
int x;
X();
int GetValue(){return x;}
};

I am allocating memory for this structure dynamically. I want to
implement a GetPointer method which returns the pointer to the
allocation memory.

something like:-
ptr = GetPointer();

Its possible that the allocation failed, Its OK for me. Now throughout
the code i need to check for (ptr!=NULL) before using it.
I want to avoid this.
 
A

Alf P. Steinbach

* Suraj:
Can someone suggest me a way of never returning a NULL pointer.

Say I have a class
class X
{
int x;
X();
int GetValue(){return x;}
};

I am allocating memory for this structure dynamically. I want to
implement a GetPointer method which returns the pointer to the
allocation memory.

something like:-
ptr = GetPointer();

Its possible that the allocation failed, Its OK for me. Now throughout
the code i need to check for (ptr!=NULL) before using it.
I want to avoid this.

There's too little information to give any recommendation, other than try to
describe what problem you're trying to solve by this hypothetical solution.

It may be that you need a singleton, or that you need a never-null smart
pointer, or something else entirely.

So, try to describe what you're trying to solve.


Cheers & hth.,

- Alf
 
L

litb

Can someone suggest me a way of never returning a NULL pointer. ....
Its possible that the allocation failed, Its OK for me. Now throughout
the code i need to check for (ptr!=NULL) before using it.
I want to avoid this.

Is it ok or not ok? It's unclear to me from your question. If it is
not OK, why not just do nothing? Normal new never returns NULL, but it
throws if there is a problem with getting enough memory. If you can't
live with that, install a new_handler that's called by the new
operator to get more memory.

If NULL *is* OK for you, but you want to guard from null pointer
accesses, use a smart pointer that checks in its operator* and
operator->
 
Z

ZikO

Suraj said:
Its possible that the allocation failed, Its OK for me. Now throughout
the code i need to check for (ptr!=NULL) before using it.
I want to avoid this.

The alternative way is for example, if it's ok for you, trying to catch
the exception bad_alloc which new throws if allocation is unsuccessful.

struct A {
// ....
}

A* GetPointer() {
return new A;
}

int main() {
try {
A* structA = GetPointer();
} catch(bad_alloc ba) {
// ... handling the exception
}
return 0;
}
 
S

Suraj

The alternative way is for example, if it's ok for you, trying to catch
the exception bad_alloc which new throws if allocation is unsuccessful.

struct A {
   // ....

}

A* GetPointer() {
   return new A;

}

int main() {
   try {
     A* structA = GetPointer();
   } catch(bad_alloc ba) {
     // ... handling the exception
   }
   return 0;

}

Alf, Ziko Thanks for the replies.
I did implement something that helps me achieve this. I am copying an
example.
struct strSP
{
int x;
float y;

strSP(int a,float b)
{
x=-9999;
y=-9999.9999;
}

strSP()
{
x=0;
y=0.0;
}
};

void* GetPointer()
{
void * pTempPtr;// = NULL;

if(NULL != ptrDynMem)
{
pTempPtr = ptrDynMem;
}
else
{
static strSP strSPInstance(0,0);
pTempPtr = &strSPInstance;
}
return pTempPtr;
}


Can you comment on this piece of code. I mean what are the obvious
loopholes that you see here?
With this there will be an instance of the struct always in the static
memory.

In the actual code, the default values of -9999 etc will be indicative
enough of usability of the pointer. I would not check for anything but
the values will be such that it will make the pointer returned
"unusable" or useless.

~Suraj
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top