Can i do this to remove allocations


is this valid/stable method of passing pointers, may be a silly question but i need the memory on some recursive functions. This seems to work fine on all my machines and devices but haven't tested it across to many platforms.

example

int * myCount = NULL;
//removed allocation version in question
1
2
3
4
5
6
void ExampleFunction(int & count)
{
    if(&count != NULL)//can i do this in event that its null?? is this stable
    {
    }
}

//called like this
ExampleFunction(*count);


//old method i know is stable
1
2
3
4
5
6
void ExampleFunction(int * count)
{
    if(count != NULL)
    {
    }
}

//called like this
ExampleFunction(count);


thanks in advance
Last edited on
In your first snippet the if() statement is probably not necessary because you're dealing with a reference parameter. Here is what my compiler says about that if().

"warning: the compiler can assume that the address of ‘count’ will never be NULL"


in my example the reference passed in is null, as you can see in the if statement im pointing to the address of the int to check for null, syntactically its ok but i dont know if this is going to behave across many platforms.

-im compiling visual studio and android command line ndk for this test, im about to try the mac but everytime i do that im downloading mass sdk updates to test on my phone, lol
Last edited on
> in my example the reference passed in is null

The program already has undefined behaviour. (Dereferencing a null-pointer is undefined behaviour.)
i was worried about this as i never did it before, it seemed dangerous to me. but would have saved me time :(

but thanks for the clarification


is it the same scenario with pointer references? or is it the same thing

1
2
3
4
5
6
7
8
9
10
int * myCount = NULL;

void ExampleFunction(int *& count)
{
    if(count != NULL)//can i do this in event that its null?? is this stable
    {
    }
}

ExampleFunction(count);


thanks
This is fine. Ideally, use nullptr instead of NULL.
thanks
Topic archived. No new replies allowed.