Why is pointer dropping value in C++? [duplicate] - c++

I have my function and I am filling targetBubble there, but it is not filled after calling this function, but I know it was filled in this function because I have there output code.
bool clickOnBubble(sf::Vector2i & mousePos, std::vector<Bubble *> bubbles, Bubble * targetBubble) {
targetBubble = bubbles[i];
}
And I am passing the pointer like this
Bubble * targetBubble = NULL;
clickOnBubble(mousePos, bubbles, targetBubble);
Why it is not working?

Because you are passing a copy of pointer. To change the pointer you need something like this:
void foo(int **ptr) //pointer to pointer
{
*ptr = new int[10]; //just for example, use RAII in a real world
}
or
void bar(int *& ptr) //reference to pointer (a bit confusing look)
{
ptr = new int[10];
}

You are passing the pointer by value.
Pass a reference to the pointer if you want it updated.
bool clickOnBubble(sf::Vector2i& mousePos, std::vector<Bubble *> bubbles, Bubble *& t)

if you write
int b = 0;
foo(b);
int foo(int a)
{
a = 1;
}
you do not change 'b' because a is a copy of b
if you want to change b you would need to pass the address of b
int b = 0;
foo(&b);
int foo(int *a)
{
*a = 1;
}
same goes for pointers:
int* b = 0;
foo(b);
int foo(int* a)
{
a = malloc(10); // here you are just changing
// what the copy of b is pointing to,
// not what b is pointing to
}
so to change where b points to pass the address:
int* b = 0;
foo(&b);
int foo(int** a)
{
*a = 1; // here you changing what b is pointing to
}
hth

You cannot change the pointer unless you pass it by (non const) reference or as a double pointer. Passing by value makes a copy of the object and any changes to the object are made to the copy, not the object. You can change the object that the pointer points to, but not the pointer itself if you pass by value.
Have a read of this question to help understand the differences in more detail When to pass by reference and when to pass by pointer in C++?

Related

swap function not works as expectd [duplicate]

I have my function and I am filling targetBubble there, but it is not filled after calling this function, but I know it was filled in this function because I have there output code.
bool clickOnBubble(sf::Vector2i & mousePos, std::vector<Bubble *> bubbles, Bubble * targetBubble) {
targetBubble = bubbles[i];
}
And I am passing the pointer like this
Bubble * targetBubble = NULL;
clickOnBubble(mousePos, bubbles, targetBubble);
Why it is not working?
Because you are passing a copy of pointer. To change the pointer you need something like this:
void foo(int **ptr) //pointer to pointer
{
*ptr = new int[10]; //just for example, use RAII in a real world
}
or
void bar(int *& ptr) //reference to pointer (a bit confusing look)
{
ptr = new int[10];
}
You are passing the pointer by value.
Pass a reference to the pointer if you want it updated.
bool clickOnBubble(sf::Vector2i& mousePos, std::vector<Bubble *> bubbles, Bubble *& t)
if you write
int b = 0;
foo(b);
int foo(int a)
{
a = 1;
}
you do not change 'b' because a is a copy of b
if you want to change b you would need to pass the address of b
int b = 0;
foo(&b);
int foo(int *a)
{
*a = 1;
}
same goes for pointers:
int* b = 0;
foo(b);
int foo(int* a)
{
a = malloc(10); // here you are just changing
// what the copy of b is pointing to,
// not what b is pointing to
}
so to change where b points to pass the address:
int* b = 0;
foo(&b);
int foo(int** a)
{
*a = 1; // here you changing what b is pointing to
}
hth
You cannot change the pointer unless you pass it by (non const) reference or as a double pointer. Passing by value makes a copy of the object and any changes to the object are made to the copy, not the object. You can change the object that the pointer points to, but not the pointer itself if you pass by value.
Have a read of this question to help understand the differences in more detail When to pass by reference and when to pass by pointer in C++?

emplace_back with pointer and unique pointers

In relation to someFunctionOne and someFunctionTwo.
Is the end result still the same when each is emplaced into the map ?
Does the pointer get upgraded to a smart pointer ?
struct MyStruct {
int x = 0;
};
std::unordered_map<int, std::unique_ptr<MyStruct>> m_map;
void someFunctionOne(int id, std::unique_ptr<MyStruct>& myStruct){
m_map.emplace(id, std::move(myStruct));
}
void someFunctionTwo(int id, MyStruct * myStruct){
m_map.emplace(id, myStruct);
}
int main()
{
someFunctionOne(452, std::make_unique<MyStruct>());
someFunctionTwo(10, new MyStruct);
m_map.clear();
return 0;
}
Does the pointer get upgraded to a smart pointer ?
The correct term is Implicit type conversion. But yes, that is what would happen in this line:
m_map.emplace(id, myStruct);
This is because unique_pointer has a single value constructor which has the signature:
explicit unique_ptr( pointer p ) noexcept;
This is what would be called to convert the pointer to a unique_ptr.

c++ in function- an error for "a reference .. can not be initialized with a value"

I 'm still confused for my problem after spending an amount of time to digging related posts/online resources.
My sample codes (test.cc) are :
void testsub(const int* &xx );
int main ()
{
int* xx;
xx= new int [10];
testsub(xx);
}
void testsub(const int* & xx){}
The compiling error message(pgcpp) read
"test.cc", line 7: error: a reference of type "const int *&" (not const-qualified)
cannot be initialized with a value of type "int *"
testsub(xx);
^
1 error detected in the compilation of "test.cc"."
Why? Your help is appreciated.
Best wishes,
Ting
int* cannot be used where the argument type is const int* &.
Say you have:
const int a = 10;
void foo(const int* & ip)
{
ip = &a;
}
int main()
{
int* ip = NULL;
foo(ip);
*ip = 20; // If this were allowed, you will be able to
// indirectly modify the value of "a", which
// is not good.
}
As the error message says, the argument type is incompatible; the function wants a pointer to const int, while you supply a pointer to int.
If you're asking why that's incompatible: allowing it would allow you to break const-correctness, as in this example:
void testsub(const int* &xx ) {
static const int x;
xx = &x;
}
int* xx;
testsub(xx); // Shouldn't be allowed, because...
*xx = 666; // BOOM! modifying a constant object.
Maybe try this
void testsub(const int* xx );
int main ()
{
int xx [10];
testsub(xx);
}
void testsub(const int* xx){}
You don't need the &, because you are passing a pointer as argument.
When you forward a "C-Array" (your int[10]), you will have a pointer to the first element of this array in your function.
void testsub(const int* xx );
int main ()
{
int* xx;
xx= new int [10];
testsub(xx);
}
void testsub(const int* xx){}
I think you got confused by your book, because they always write something like "Call by reference". That doesn't mean to pass the parameter as a reference with the &.
Often it is useful to pass also the size of the array to the function ... so it would like:
void testsub(const int* xx, size_t arraySize);
int main ()
{
int* xx;
xx= new int [10];
testsub(xx, 10);
}
void testsub(const int* xx, size_t arraySize){}
Now you can access the array in your function and you have the possibility to check the index, if you want to access the array with an index.
void testsub(int* xx, size_t arraySize)
{
for(size_t i=0; i<arraySize; ++i)
// ^ this way you will never try to access
// memory, which does not belong to the array
// => no seg fault, or whatever happens
{
// do sth. with the array ... for example setting values to 0
xx[i] = 0;
}
}

Swapping pointers [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Swapping objects using pointers
I know how to do swapping using pointers, but then, if I try a different approach like this:
/* Pointers */
#include <stdio.h>
int main ()
{
int a=4,b=6;
swap(&a,&b);
printf("A is %d, and B is %d\n",a,b);
return 0;
}
int swap(int *a, int *b)
{
int *temp;
temp = a;
a = b;
b = temp;
return 0;
}
It doesn't work. Basically the swap function is changing the address, like 'a' now has the address of 'b', and vice-versa.. If I print out the values in swap function, it gives swapped values, but it is not reflected in main function. Can any one tell me why?
Because
the swap function is changing the address, like 'a' now has the address of 'b', and vice-versa
is not true. It doesn't change their addresses (that would make absolutely no sense whatsoever). The function changes the values of the pointers - those pointers are copies of the addresses, and these pointers, since they're function arguments, are local to the function. What you have to do is:
int swap(int *a, int *b)
{
int temp;
temp = *a;
*a = *b;
*b = temp;
return 0;
}
Or you can use references (only in C++), like this:
int swap(int &a, int &b)
{
int temp;
temp = a;
a = b;
b = temp;
return 0;
}
and call it without the addressof operator:
int a = 4, b = 6;
swap(a, b);
However, if this is for an actual implementation, and not a "write a swap function"-style homework, then you should use the std::swap() function from <algorithm>.
If you simply take them as inout using the standard method, you will receive a COPY of the variable, not the actual variable. However when you pass a *variable you give it a variable that points to the actual variable. You can then set the memory location using swap because a copy of a memory location is still the same.
So try this code for your function:
int swap(int *a, int *b)
{
int tmp;
tmp = *a;
*a = *b;
*b = tmp;
return 0;
}
'a' and 'b' in swap aren't the 'a' and 'b' in main. Within swap, these are pointers. If you want to swap the values pointed by a,b, you need:
int swap( int* pa, int *pb )
{
int temp;
temp = *pa;
*pa = *pb;
*pb = temp;
return 0;
}
note I'm using more appropriate variable names. Also, you need to allocate temp (vs allocating pointer to temp).
You can try using:
int swap(int*& a, int*& b)
{
int *temp;
temp = a;
a = b;
b = temp;
return 0;
}
Note that stack addresses cannot be moved so you'll need a different main:
int main ()
{
int a=4,b=6;
int *A = &a;
int *B = &b;
swap(A, B);
printf("A is %d, and B is %d\n",*A, *B);
return 0;
}

Function does not change passed pointer C++

I have my function and I am filling targetBubble there, but it is not filled after calling this function, but I know it was filled in this function because I have there output code.
bool clickOnBubble(sf::Vector2i & mousePos, std::vector<Bubble *> bubbles, Bubble * targetBubble) {
targetBubble = bubbles[i];
}
And I am passing the pointer like this
Bubble * targetBubble = NULL;
clickOnBubble(mousePos, bubbles, targetBubble);
Why it is not working?
Because you are passing a copy of pointer. To change the pointer you need something like this:
void foo(int **ptr) //pointer to pointer
{
*ptr = new int[10]; //just for example, use RAII in a real world
}
or
void bar(int *& ptr) //reference to pointer (a bit confusing look)
{
ptr = new int[10];
}
You are passing the pointer by value.
Pass a reference to the pointer if you want it updated.
bool clickOnBubble(sf::Vector2i& mousePos, std::vector<Bubble *> bubbles, Bubble *& t)
if you write
int b = 0;
foo(b);
int foo(int a)
{
a = 1;
}
you do not change 'b' because a is a copy of b
if you want to change b you would need to pass the address of b
int b = 0;
foo(&b);
int foo(int *a)
{
*a = 1;
}
same goes for pointers:
int* b = 0;
foo(b);
int foo(int* a)
{
a = malloc(10); // here you are just changing
// what the copy of b is pointing to,
// not what b is pointing to
}
so to change where b points to pass the address:
int* b = 0;
foo(&b);
int foo(int** a)
{
*a = 1; // here you changing what b is pointing to
}
hth
You cannot change the pointer unless you pass it by (non const) reference or as a double pointer. Passing by value makes a copy of the object and any changes to the object are made to the copy, not the object. You can change the object that the pointer points to, but not the pointer itself if you pass by value.
Have a read of this question to help understand the differences in more detail When to pass by reference and when to pass by pointer in C++?