Suppose I have this code. Your basic "if the caller doesn't provide a value, calculate value" scenario.
void fun(const char* ptr = NULL)
{
if (ptr==NULL) {
// calculate what ptr value should be
}
// now handle ptr normally
}
and call this with either
fun(); // don't know the value yet, let fun work it out
or
fun(something); // use this value
However, as it turns out, ptr can have all kinds of values, including NULL, so I can't use NULL as a signal that the caller doesn't provide ptr.
So I'm not sure what default value to give ptr now instead of NULL. What magic value can I use? Does anybody have ideas?
void fun()
{
// calculate what ptr value should be
const char* ptr = /*...*/;
// now handle ptr normally
fun(ptr);
}
Depending on your platform, a pointer is likely either a 32 or 64-bit value.
In those cases, consider using:
0xFFFFFFFF or 0xFFFFFFFFFFFFFFFF
But I think the bigger question is, "How can NULL be passed as a valid parameter?"
I'd recommend instead having another parameter:
void fun(bool isValidPtr, const char* ptr = NULL)
or maybe:
void fun( /*enum*/ ptrState, const char* ptr = NULL)
I agree with all the other answers provided, but here's one more way of handling that, which to me personally looks more explicit, if more verbose:
void fun()
{
// Handle no pointer passed
}
void fun(const char* ptr)
{
// Handle non-nullptr and nullptr separately
}
You should use the nullptr for that. Its new in the C++11 standart. Have a look here for some explanation.
Using overloaded versions of the same function for different input is best, but if you want to use a single function, you could make the parameter be a pointer-to-pointer instead:
void fun(const char** ptr = NULL)
{
if (ptr==NULL) {
// calculate what ptr value should be
}
// now handle ptr normally
}
Then you can call it like this:
fun();
.
char *ptr = ...; // can be NULL
fun(&ptr);
If you want a special value that corresponds to no useful argument, make one.
header file:
extern const char special_value;
void fun(const char* ptr=&special_value);
implementation:
const char special_value;
void fun(const char* ptr)
{
if (ptr == &special_value) ....
}
1?
I can't imagine anyone allocating you memory with that address.
Related
Under what circumstances might you want to use multiple indirection (that is, a chain of pointers as in Foo **) in C++?
Most common usage as #aku pointed out is to allow a change to a pointer parameter to be visible after the function returns.
#include <iostream>
using namespace std;
struct Foo {
int a;
};
void CreateFoo(Foo** p) {
*p = new Foo();
(*p)->a = 12;
}
int main(int argc, char* argv[])
{
Foo* p = NULL;
CreateFoo(&p);
cout << p->a << endl;
delete p;
return 0;
}
This will print
12
But there are several other useful usages as in the following example to iterate an array of strings and print them to the standard output.
#include <iostream>
using namespace std;
int main(int argc, char* argv[])
{
const char* words[] = { "first", "second", NULL };
for (const char** p = words; *p != NULL; ++p) {
cout << *p << endl;
}
return 0;
}
IMO most common usage is to pass reference to pointer variable
void test(int ** var)
{
...
}
int *foo = ...
test(&foo);
You can create multidimensional jagged array using double pointers:
int ** array = new *int[2];
array[0] = new int[2];
array[1] = new int[3];
One common scenario is where you need to pass a null pointer to a function, and have it initialized within that function, and used outside the function. Without multplie indirection, the calling function would never have access to the initialized object.
Consider the following function:
initialize(foo* my_foo)
{
my_foo = new Foo();
}
Any function that calls 'initialize(foo*)' will not have access to the initialized instance of Foo, beacuse the pointer that's passed to this function is a copy. (The pointer is just an integer after all, and integers are passed by value.)
However, if the function was defined like this:
initialize(foo** my_foo)
{
*my_foo = new Foo();
}
...and it was called like this...
Foo* my_foo;
initialize(&my_foo);
...then the caller would have access to the initialized instance, via 'my_foo' - because it's the address of the pointer that was passed to 'initialize'.
Of course, in my simplified example, the 'initialize' function could simply return the newly created instance via the return keyword, but that does not always suit - maybe the function needs to return something else.
If you pass a pointer in as output parameter, you might want to pass it as Foo** and set its value as *ppFoo = pSomeOtherFoo.
And from the algorithms-and-data-structures department, you can use that double indirection to update pointers, which can be faster than for instance swapping actual objects.
A simple example would be using int** foo_mat as a 2d array of integers.
Or you may also use pointers to pointers - lets say that you have a pointer void* foo and you have 2 different objects that have a reference to it with the following members: void** foo_pointer1 and void** foo_pointer2, by having a pointer to a pointer you can actually check whether *foo_pointer1 == NULL which indicates that foo is NULL. You wouldn't be able to check whether foo is NULL if foo_pointer1 was a regular pointer.
I hope that my explanation wasn't too messy :)
Carl: Your example should be:
*p = x;
(You have two stars.) :-)
In C, the idiom is absolutely required. Consider the problem in which you want a function to add a string (pure C, so a char *) to an array of pointers to char *. The function prototype requires three levels of indirection:
int AddStringToList(unsigned int *count_ptr, char ***list_ptr, const char *string_to_add);
We call it as follows:
unsigned int the_count = 0;
char **the_list = NULL;
AddStringToList(&the_count, &the_list, "The string I'm adding");
In C++ we have the option of using references instead, which would yield a different signature. But we still need the two levels of indirection you asked about in your original question:
int AddStringToList(unsigned int &count_ptr, char **&list_ptr, const char *string_to_add);
Usually when you pass a pointer to a function as a return value:
ErrorCode AllocateObject (void **object);
where the function returns a success/failure error code and fills in the object parameter with a pointer to the new object:
*object = new Object;
This is used a lot in COM programming in Win32.
This is more of a C thing to do, in C++ you can often wrap this type of system into a class to make the code more readable.
I have the following code which has a function VariadicWrite and this function accepts a pointer that it can modify by incrementing it to it points to a different memory location depending on how much data is written to it:
#include <iostream>
#include <cstring>
template<typename T>
T Read(void* &ptr) noexcept
{
T result;
memcpy(&result, ptr, sizeof(result));
ptr = static_cast<T*>(ptr) + 1;
return result;
}
template<typename T>
void Write(void* &ptr, T result) noexcept
{
memcpy(ptr, &result, sizeof(result));
ptr = static_cast<T*>(ptr) + 1;
}
template<typename... Args>
auto VariadicWrite(void*& ptr, Args&&... args)
{
(Write(ptr, args), ...); //unpack Args
return 0;
}
int main()
{
void* ptr = malloc(1024);
memset(ptr, 0, 1024);
void* temp = ptr;
VariadicWrite(ptr, 1, 2, 3);
std::cout<<Read<int>(ptr)<<"\n";
std::cout<<Read<int>(ptr)<<"\n";
std::cout<<Read<int>(ptr)<<"\n";
free(temp);
return 0;
}
The problem here is that the code prints out 0, 0, 0, 0 if I use: void*& ptr.
If I do void* ptr it prints out 0, 1, 2, 3 but the ptr pointer is never incremented.
How can I modify the ptr pointer of VariadicWrite? I thought that void*& would have worked but in this case it doesn't :S
The problem is your VariadicWrite() call will modify ptr to point to the end of your written data. Then you call Read() without resetting the pointer back to the start, so you read zeros from the uninitialized portion of your buffer that follows the data already written.
Insert ptr = temp; between the write and read and see if that fixes it.
The reason void* ptr does not work is that each call to Write(ptr, ...) will increment the local copy of the argument in the scope of the Write() function. The variable ptr in VariadicWrite() does not change after a call to Write() so the next call will use the same value.
If you change to VariadicWrite(void* ptr, …) and Write(void*& ptr, …), you might get the behavior you want. But I would suggest this a bad idea.
As we can see from the bug in your example, knowing if the function will modify the pass-by-reference parameter or not is of critical importance, yet not readily apparent from the code using the function. This tends to invite bugs just like the one you have created here. An inconsistent interface, where VariadicWrite() does not modify its argument but Write() does, will only make it doubly hard to avoid this kind of bug.
Generally, it's better to avoid non-const references because they often result in bugs like this. I suggest returning the new pointer instead of modifying the argument.
template<typename T>
void* Write(void* ptr, const T& arg)
{
return static_cast<T*>(ptr) + 1;
}
template<typename... Args>
void* WriteV(void* ptr, Args&&... args)
{
((ptr = Write(ptr, args)), ...);
return ptr;
}
I have a function (func1) that gets as a parameter a const pointer.
The value of this pointer is used for a second function (func2).
Depending on a boolean I want to modify this value before handing it to func2 or not. If I have to modify it I allocate new memory to store the modified version of it. Because in reality the const int pointer is a big array, I don't want to copy it.
I tried to solve it that way:
void func1(const int* value, bool change)
{
int* valueToUse;
if(change)
{
int changedValue = (*value)++;
valueToUse = &changedValue;
}
else
{
valueToUse = value; // <= here the Error occurs
}
func2(valueToUse);
}
void func2(const int* foo)
{
// ...
}
But if I do it this way, I get an error because I assign a const pointer to a simple pointer:
error: invalid conversion from 'const int* to int*'
Is there any easy way to solve this?
I can imagine a solution with two extra functions for each case or a version that calls func2 at two points. But because this presented code is only a very simplified version of my real code, I'm wandering if there is an easier solution to that.
Best would be a solution that works for C and C++.
Thanks guys in advance!
This works?
void func1(const int* value, bool change)
{
const int* valueToUse;
int changedValue;
if(change)
{
changedValue = (*value) + 1;
valueToUse = &changedValue;
}
else
{
valueToUse = value;
}
func2(valueToUse);
}
A const int* is a pointer to const int, which is not a const pointer to int. Therefore the pointer itself is free to change.
Also, incrementing (*value) is an error because *value is a const int.
In func1, you can make 2 calls to func2, this way there is no copy when not needed and the original value is never changed:
void func1(const int* value, bool change)
{
if(change)
{
int changedValue = *value+1;
func2(&changedValue);
}
else
{
func2(value);
}
}
void func1(const int* value, int change)
{
int* valueToUse;
if(change)
{
(*(int*)value)++;
valueToUse = (int*)value;
}
else
{
valueToUse = (int*)value; // <= here the Error occurs
}
func2(valueToUse);
}
But you don't really need this valueToUse, it's what i think, so.
void func1(const int* value, int change)
{
if(change)
(*(int*)value)++;
func2(value);
}
const int* value
This means pointer to a constant int value. It is not permitted to change the value of a constant int.
There are two ways of solving this.
Change signature of first function void func1(int* value, bool change)
Pass arguments to func1 after constant casting using const_cast and change
int changedValue = (*value)++; to int changedValue = *value + 1;
Well, running this code i receive just a warning, though i think that type conversion will help you.
valueToUse = (int*)value;
I'm having issues passing an array struct into a function for processing.
I think I need to pass the function the address of the array, however have got multiple compiler errors and am running out of combinations to try.
The function then needs to return a value to a new struct member.
Here is my best shot!
//-----------------
void Function(struct MyStruct* ptr);
//------------------
int main(){
MyStruct array[MAX];
for (int i=0; i<MAX; i++)
{
File>>array[i].V1;
File>>array[i].V2;
File>>array[i].V3;
File>>array[i].V4;
MyStruct* ptr = &array[i];
array[i].V5 = Function(ptr);
}
}
//-----------------------
void Function(struct MyStruct* ptr)
{
// do something with the struct, how to I access each element in here?
}
Thanks!
You have a void type for Function, which is why you can't return anything. You should make it return whatever V5's type is instead.
Also, to access a element of ptr, use the arrow notation:
ptr->V5
Under what circumstances might you want to use multiple indirection (that is, a chain of pointers as in Foo **) in C++?
Most common usage as #aku pointed out is to allow a change to a pointer parameter to be visible after the function returns.
#include <iostream>
using namespace std;
struct Foo {
int a;
};
void CreateFoo(Foo** p) {
*p = new Foo();
(*p)->a = 12;
}
int main(int argc, char* argv[])
{
Foo* p = NULL;
CreateFoo(&p);
cout << p->a << endl;
delete p;
return 0;
}
This will print
12
But there are several other useful usages as in the following example to iterate an array of strings and print them to the standard output.
#include <iostream>
using namespace std;
int main(int argc, char* argv[])
{
const char* words[] = { "first", "second", NULL };
for (const char** p = words; *p != NULL; ++p) {
cout << *p << endl;
}
return 0;
}
IMO most common usage is to pass reference to pointer variable
void test(int ** var)
{
...
}
int *foo = ...
test(&foo);
You can create multidimensional jagged array using double pointers:
int ** array = new *int[2];
array[0] = new int[2];
array[1] = new int[3];
One common scenario is where you need to pass a null pointer to a function, and have it initialized within that function, and used outside the function. Without multplie indirection, the calling function would never have access to the initialized object.
Consider the following function:
initialize(foo* my_foo)
{
my_foo = new Foo();
}
Any function that calls 'initialize(foo*)' will not have access to the initialized instance of Foo, beacuse the pointer that's passed to this function is a copy. (The pointer is just an integer after all, and integers are passed by value.)
However, if the function was defined like this:
initialize(foo** my_foo)
{
*my_foo = new Foo();
}
...and it was called like this...
Foo* my_foo;
initialize(&my_foo);
...then the caller would have access to the initialized instance, via 'my_foo' - because it's the address of the pointer that was passed to 'initialize'.
Of course, in my simplified example, the 'initialize' function could simply return the newly created instance via the return keyword, but that does not always suit - maybe the function needs to return something else.
If you pass a pointer in as output parameter, you might want to pass it as Foo** and set its value as *ppFoo = pSomeOtherFoo.
And from the algorithms-and-data-structures department, you can use that double indirection to update pointers, which can be faster than for instance swapping actual objects.
A simple example would be using int** foo_mat as a 2d array of integers.
Or you may also use pointers to pointers - lets say that you have a pointer void* foo and you have 2 different objects that have a reference to it with the following members: void** foo_pointer1 and void** foo_pointer2, by having a pointer to a pointer you can actually check whether *foo_pointer1 == NULL which indicates that foo is NULL. You wouldn't be able to check whether foo is NULL if foo_pointer1 was a regular pointer.
I hope that my explanation wasn't too messy :)
Carl: Your example should be:
*p = x;
(You have two stars.) :-)
In C, the idiom is absolutely required. Consider the problem in which you want a function to add a string (pure C, so a char *) to an array of pointers to char *. The function prototype requires three levels of indirection:
int AddStringToList(unsigned int *count_ptr, char ***list_ptr, const char *string_to_add);
We call it as follows:
unsigned int the_count = 0;
char **the_list = NULL;
AddStringToList(&the_count, &the_list, "The string I'm adding");
In C++ we have the option of using references instead, which would yield a different signature. But we still need the two levels of indirection you asked about in your original question:
int AddStringToList(unsigned int &count_ptr, char **&list_ptr, const char *string_to_add);
Usually when you pass a pointer to a function as a return value:
ErrorCode AllocateObject (void **object);
where the function returns a success/failure error code and fills in the object parameter with a pointer to the new object:
*object = new Object;
This is used a lot in COM programming in Win32.
This is more of a C thing to do, in C++ you can often wrap this type of system into a class to make the code more readable.