#Swaping addresses of pointers

120 messages · Page 1 of 1 (latest)

pearl temple
#

I'm creating Conway's Game of life in c++ and I have 2 vectors inside a Class:

#include<vector>
using namespace std;
class Board
{
private:
    std::vector<std::vector<bool>>* CurrentFrame;
    std::vector<std::vector<bool>>* NextFrame;
    
public:
    Board();
    ~Board();
};

Board::Board()
{
}

Board::~Board()
{
}

I was wondering if there is any way to change what they point to without creating a reference to them?
ei:

std::vector<std::vector<bool>> CurrentFrame;
Auto* PtrToFrame = &CurrentFrame;
obtuse barnBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For more information use !howto ask.

static spear
#

Yes

#

But what exactly are you referring to, what exactly do you want to swap

pearl temple
#

what the pointers are pointing to

static spear
#

CurrentFrame = something else will change what the pointer points to

pearl temple
#

like if int a was pointing to Ox00000000 and b was pointing to 0x0000000F, I want b to point to Ox00000000

#

I don't want it to memory copy the entire array

static spear
#

CurrentFrame = something else won't copy the entire vector

queen wind
static spear
#

it only sets what vector is being pointed to

static spear
#

std::vector<std::vector<bool>>* CurrentFrame;

pearl temple
#

oh

queen wind
#

if not the board

static spear
#

If CurrentFrame is not a pointer, you can swap the pointers to the underlying buffer with .swap

pearl temple
#

so ```cpp
std::swap(CurrentFrame,NextFrame)

queen wind
queen wind
#

i mean, it's basically just

auto temp = p1;
p1 = p2;
p2 = temp;
pearl temple
#

I don't want it to copy all of the values of the vector

queen wind
#

it doesn't

#

it swaps pointers

#

those pointers point to the vectors

#

i'm not sure you really want pointers to vectors anyway

#

where do those pointers even come from

#

do they point to some local variables?

pearl temple
#

yes

#

I just forget to write new()

queen wind
pearl temple
#

how do I allocate then?

queen wind
#

also then your "yes" answer doesn't make sense.

queen wind
#

because you don't

pearl temple
#

Ok....

pearl temple
#

X and Y I mean

#

A and B could be Anything, but swaping a and b would switchit?

queen wind
pearl temple
#

yeah

queen wind
# pearl temple yeah

so why didn't you make CurrentFrame and NextFrame std::vector<std::vector<bool>>

#

instead of std::vector<std::vector<bool>>*

pearl temple
#

And how would I swap it

queen wind
#

yes, even if they are vectors

#

std::swap is just that smart

#

i would still like you to answer my question.

pearl temple
#

And that was?

pearl temple
#

I really haven't ever swaped addresses and like nothing on stack exchange helps

queen wind
#

why did you use pointers

#

why did you declare those data members as pointers

pearl temple
#

So I could swap addresses

Object 1 -------------------> 0x00000000
        \ /
         X
        / \
Object 2 -------------------> 0x0000000F
#

Rough diagram

queen wind
pearl temple
#

I probably am

queen wind
#

when you do

int* a = &x;
int* b = &y;
swap(a, b);

then x is still x and y is still y, it's only as if you declared them as

int* a = &y;
int* b = &x;
pearl temple
#

Then How come when I pass it through a swap funvtion, the address doesn't change?

queen wind
#

that's the effect of swapping pointers.

queen wind
#

the pointers?

pearl temple
#

yes

queen wind
# pearl temple yes

the pointers just contain addresses. swapping them has no effect on the objects they are pointing to.

pearl temple
#

?

queen wind
pearl temple
queen wind
#

..... ok wait

#

English is an unhelpful language when explaining this stuff

queen wind
pearl temple
#

A pointer is a variable that contains an address of another part of ram

queen wind
#

good.

#

okay

#

maybe i can show you something

#

;compile

#include <iostream>
#include <iomanip>
#include <utility>
using namespace std;

int main() {
    int x = 1;
    int y = 3;
    int* p1 = &x;
    int* p2 = &y;
    cout << boolalpha;
    cout << (p1 == &x) << endl;
    cout << (p2 == &y) << endl;
    cout << x << ' ' << y << endl;
    std::swap(p1, p2);
    cout << (p1 == &y) << endl;
    cout << (p2 == &x) << endl;
    cout << x << ' ' << y << endl;
}
compact hollowBOT
#
Program Output
true
true
1 3
true
true
1 3
pearl temple
#

;compile

#include <iostream>
#include <iomanip>
#include <utility>
using namespace std;

int main() {
    int x = 1;
    int y = 3;
    int* p1 = &x;
    int* p2 = &y;
    cout << boolalpha;
    cout << p1 << endl;
    cout << p2 << endl;
    std::swap(p1, p2);
    cout << p1 << endl;
    cout << p2 << endl;
}```
compact hollowBOT
#
Critical error:

You must provide a valid language or compiler!

;compile c++
```
int main() {}
```

pearl temple
#

;compile cpp

#include <iostream>
#include <iomanip>
#include <utility>
using namespace std;

int main() {
    int x = 1;
    int y = 3;
    int* p1 = &x;
    int* p2 = &y;
    cout << boolalpha;
    cout << p1 << endl;
    cout << p2 << endl;
    std::swap(p1, p2);
    cout << p1 << endl;
    cout << p2 << endl;
}```
compact hollowBOT
#
Program Output
0x7ffd12c6198c
0x7ffd12c61988
0x7ffd12c61988
0x7ffd12c6198c
pearl temple
#

ohh

#

but does this work with countainers?

queen wind
#

btw you can just edit your message instead of typing another one

pearl temple
#

ohh

queen wind
#

after the edit, the compiler bot will just recompile

pearl temple
#

you can?

queen wind
#

yes, reduces spam

pearl temple
#

neat

queen wind
#

so please do that

pearl temple
#

will do

queen wind
pearl temple
#

ok

queen wind
#

if the type of the objects that we wanna swap has a special swap() method, it will use that

#

otherwise it will just create a temporary and move the two objects between them

pearl temple
#

so vector::swap()?

queen wind
pearl temple
#

ok

queen wind
#

idk what it exactly does, but it doesn't matter

#

it will probably just swap the internal pointers

#

doesn't matter.

#

the point is:

#

pointers are also just objects that contain addresses.

#

so when you swap pointers, you never affect the objects that they point to. you just change which object they point to.

#

p1 points to what p2 just pointed to.
and p2 points to what p1 just pointed to.

pearl temple
#

Thanks!

queen wind
pearl temple
#

yup

queen wind
#

so when you wanna swap the vectors, you swap them directly

#

like std::swap(v1, v2);

#

or whatever they are called

pearl temple
#

do I use the one in the vector class?

queen wind
#

doesn't matter.

pearl temple
#

ok then

queen wind
#

just don't use new, that would be a completely avoidable source of potential mistakes