how does the value of variable get changed through swap function? [duplicate] - c++

This question already has answers here:
C++ Swapping Pointers
(7 answers)
Closed 6 years ago.
Here I have two swap functions
void kswap(int* a, int* b)
{
int* temp = a;
a = b;
b = temp;
}
void kswap(int* a, int* b)
{
int temp = *a;
*a = *b;
*b = temp;
}
The value only changed inside of the first function,
and the second function change the value permanently..
Can anyone tell me the different between two functions?
I thought as both functions take pointer type through parameter, the value would be changed through both functions..

In function swap, a and b are int *, aka integer pointers, that means
they contain address of an integer in memory. As seen in diagram below:
Memory
==================
+----------------+
| |
+------> | num1 = 5 |
| | |
| +----> | num2 = 6 |
| | | |
| | | |
| | |================|
| | | Function swap |
| | | |
+-(------------ a |
| | |
+------------ b |
| |
+----------------+
Here,
`*a` : should be read as : `value at address contined in a`
`*b` : should be read as : `value at address contined in b`
In first example
In first kswap, after executing below statements,
int* temp = a; /* A pointer which points to same place as 'a' */
a = b; /* 'a' will now point to where 'b' is pointing */
b = temp; /* 'b' will now point to where 'temp' is pointing
* that means where 'a' was previously pointing */
the result is:
Memory
==================
+----------------+
| |
+------> | num1 = 5 | <------+
| | | |
| +----> | num2 = 6 | |
| | | | |
| | | | |
| | |================| |
| | | Function swap | |
| | | | |
+ +------------ a | |
| | | |
+-------------- b | |
| | |
| temp -----------------+
+----------------+
Note that, neither *a or *b is assigned any value, so neither of:
`*a` : that is : `value at address contined in a`
`*b` : that is : `value at address contined in b`
are changed.
So as seen in above picture, num1 is still 5, and num2 is still 6.
Only thing that has happended is that a is pointing to num2, and b is
pointing to num1.
In second example
In second kswap, after executing below statements,
int temp = *a; /* An int variable which will contain the same value as the
* value at adress contained in a */
*a = *b; /* value at address contained in 'a' will be equal to value
* at address contained in 'b' */
*b = temp; /* value at address contained in 'b' will be equal to value
* contained in 'temp' */
the result is:
Memory
==================
+----------------+
| |
+------> | num1 = 6 |
| | |
| +----> | num2 = 5 |
| | | |
| | | |
| | |================|
| | | Function swap |
| | | |
+-(------------ a |
| | |
+------------ b |
| |
| temp = 5 |
+----------------+
Note that, both *a or *b are assigned new value, so both:
`*a` : that is : `value at address contained in a`
`*b` : that is : `value at address contained in b`
are changed.
And as seen in above picture, num1 is now 6, and num2 is now 5. So in the second example, values of variables num1 and num2 are permanently changed.

Assume each function is called as:
void f()
{
int x = 101, y = 999;
kswap(&x, &y);
}
Remember that in C++ arguments are passed by value, so kswap receives the values of the addresses where x, y reside. The rest of the answer is inlined in the code comments below.
The kswap that works.
void kswap(int* a, int* b)
{
int temp = *a; // `a` is the address of `int x`
// `*a` is the integer value at address `a`
// i.e. the value of `x` so temp == 101 now
*a = *b; // same as above `*b` is the value of `y` i.e. 999
// now this integer value is copied to the address where `a` points
// effectively overwriting the old `x` value `101` with `999`
*b = temp; // finally, this copies the value in `temp` i.e. 101
// to the address where `b` points and overwrites
// the old `y` value `999`, which completes the swap
}
The kswap which does not work.
void kswap(int* a, int* b)
{
int* temp = a; // this copies `a` i.e. the address of `x`
// to local variable `temp`
a = b; // this copies `b` to `a`
// since arguments `a` and `b` are pointers and passed by value
// this only modifies the value of variable `a`
// it does **not** change `x` or its address in any way
b = temp; // this copies 'temp' to 'b', same comments as above
// now 'a' holds the address of `y` and `b` holds the address
// of `x` but **neither** 'x' nor 'y' values have been modified
// and pointer variables `a`, `b` go out of scope as soon as
// the function returns, so it's all a big no-op in the end
}

The first function swaps the addresses, but not outside the scope of the function.
The second function swaps the values, and outside the function's scope.
Adding the * to the name, means you want the value, not where it's at.

Related

How does this code work? The result doesnt make sense to me and doesnt appear in the debugger [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed last year.
Improve this question
#include <iostream>
using namespace std;
int f(int n, int m){
if (n==1)
return 0;
else
return f(n-1, m) + m;
}
int main()
{
cout << f(3874, 1000);
cout << endl;
return 0;
}
The result is 3873000. Why is n multiplied by m after deducting 1 and how does the function work in detail please?
The else block is executed at all levels of recursion, except the deepest one.
The number of levels in the recursion tree is n. So we have n-1 times that the else block is executed.
This else block first makes the recursive call, and then adds m to the result it gets back, and returns that sum to the caller, who will do the same, ...etc until the walk upwards the recursion tree is complete.
The original caller will thus see a base number (0) to which m was repeatedly added, exactly n-1 times.
So the function calculates m(n-1), provided that n is greater than 0. If not, the recursion will run into a stack overflow error.
Visualisation
To visualise this, let's split the second return statement into two parts, where first the result of the recursive call is stored in a variable, and then the sum is returned. Also, let's take a small value for n, like 3.
So this is then the code:
int f(int n, int m){
if (n==1)
return 0;
else {
int result = f(n-1, m)
return result + m;
}
}
int main()
{
cout << f(3, 10);
}
We can imagine each function execution (starting with main) as a box (a frame), in which local variables live their lives. Each recursive call creates a new box, and when return is executed that box vanishes again.
So we can imagine the above code to execute like this:
+-[main]----------------------------+
| f(3, 10) ... |
| +-[f]-------------------------+ |
| | n = 3, m = 10 | |
| | f(3-1, 10) ... | |
| | +-[f]-------------------+ | |
| | | n = 2, m = 10 | | |
| | | f(2-1, 10) ... | | |
| | | +-[f]-------------+ | | |
| | | | n = 1, m = 10 | | | |
| | | | return 0 | | | |
| | | +-----------------+ | | |
| | | result = 0 | | |
| | | return 0 + 10 | | |
| | +-----------------------+ | |
| | result = 10 | |
| | return 10 + 10 | |
| +-----------------------------+ |
| cout << 20 |
+-----------------------------------+
I hope this clarifies it.
The algorithm solves the recurrence
F(n) = F(n-1) + m
with
F(1) = 0.
(I removed m as an argument, as its value is constant).
We have
F(n) = F(n-1) + m = F(n-2) + 2m = F(n-3) + 3m = ... = F(1) + (n-1)m.
As written elsewhere, the recursion depth is n, which is dangerous.

Function printing strange values. C++

Consider this example:
class A
{
public:
int a;
public:
A():a(1){};
};
class B:public A
{
public:
int b;
public:
B():b(2){};
};
void print(A* a)
{
for(int i=0; i<10; ++i)
{
cout<<a[i].a<<" ";
}
}
int main()
{
B b[10];
print(b);
}
My program output is : 1 2 1 2 1 2 1 2 1 2 1 2.
But how it is accessing data b through a[i].a, because I never mentioned b in the output.
The problem is that sizeof(A) != sizeof(B).
And the print function thinks that is has an array (essentially) of A objects. It doesn't know anything about B or its members.
While B is an A, B[] is not an A[].
There are three natural ways to solve your problem:
Make print take a B* argument.
Make print a template function.
Create an overload of print that takes a B* argument.
Which one to pick is up to you, and depends on your use-case.
Why you seem to print values from B is because of the memory layout.
An array of A look something like this in memory:
+--------+--------+--------+--------+--------+--------+--------+-----+
| A[0].a | A[1].a | A[2].a | A[3].a | A[4].a | A[5].a | A[6].a | ... |
+--------+--------+--------+--------+--------+--------+--------+-----+
An array of B look like this
+--------+--------+--------+--------+--------+--------+--------+-----+
| B[0].a | B[0].b | B[1].a | B[1].b | B[2].a | B[2].b | B[3].a | ... |
+--------+--------+--------+--------+--------+--------+--------+-----+
Now if we put the two arrays next to each other:
+--------+--------+--------+--------+--------+--------+--------+-----+
| A[0].a | A[1].a | A[2].a | A[3].a | A[4].a | A[5].a | A[6].a | ... |
+--------+--------+--------+--------+--------+--------+--------+-----+
| B[0].a | B[0].b | B[1].a | B[1].b | B[2].a | B[2].b | B[3].a | ... |
+--------+--------+--------+--------+--------+--------+--------+-----+
With the above "overlay" it's easy to see why you seem to be printing the b member from the B class.
Though you cannot rely on this behavior, a probable explanation for the output,
Considering an array of objects of B
a|b | a|b
object1 object2
When you interpret it as A*
a | b | a
object1 object2 object3
Hence you get 1 2 1 2...

passing 2d array as pointer to pointers in c++ gives segmentation fault [duplicate]

i am trying to cast a void** pointer to an int** 2D array in C
here is the code that i am trying to work with (with all the extraneous bits removed):
\*assume that i have a data structure called graph with some
*element "void** graph" in it and some element "int order" */
void initialise_graph_data(graph_t *graph)
{
void **graph_data = NULL;
int (*matrix)[graph->order];
size_t size = (graph->order * graph->order) * sizeof(int);
graph_data = safe_malloc(size); /*safe malloc works fine*/
matrix = (int(*)[graph->order])graph_data;
graph->graph = graph_data;
}
when i compile that, it works fine, but gives me a warning that variable 'matrix' is set but not used. i dont really want to have to use the interim matrix variable because the function is just supposed to initialise the array, not put anything in it; but if i try to cast graph_data directly to an int** when i am assiging it to graph->graph like so:
graph->graph = (int(*)[graph->order])graph_data;
it gives me an assignment from incompatible pointer type warning.
am i just not casting it properly? does anyone have any suggestions as to how i can make it work without the interim "matrix" variable? or if not, what i can do with that variable so that it doesnt give me the warning that it is set but not used?
thanks
The compiler is right, an array of arrays (or a pointer to an array) is not the same as a pointer to a pointer. Just think about how they would be laid out in memory:
A matrix of size MxN in the form of an array of arrays:
+--------------+--------------+-----+----------------+--------------+-----+------------------+
| matrix[0][0] | matrix[0][1] | ... | matrix[0][N-1] | matrix[1][0] | ... | matrix[M-1][N-1] |
+--------------+--------------+-----+----------------+--------------+-----+------------------+
A and the same "matrix" in the form of pointer to pointer:
+-----------+-----------+-----------+-----+
| matrix[0] | matrix[1] | matrix[2] | ... |
+-----------+-----------+-----------+-----+
| | |
| | V
| | +--------------+--------------+-----+
| | | matrix[2][0] | matrix[2][1] | ... |
| | +--------------+--------------+-----+
| |
| V
| +--------------+--------------+-----+
| | matrix[1][0] | matrix[1][1] | ... |
| +--------------+--------------+-----+
|
V
+--------------+--------------+-----+
| matrix[0][0] | matrix[0][1] | ... |
+--------------+--------------+-----+
It doesn't matter if you allocate the correct size, the two variables simply are incompatible which is what your compiler is telling you.

Casting buffers as structures

I'm doing this as a learning exercise. The C++ book I'm studying from casts a buffer as a structure for easy manipulation and streaming. Everything seems fine until I try using an array (body) and look at the binary data in the buffer after assigning values. It doesn't match what I expect.
#include <iostream>
#include <bitset>
#include <netinet/in.h>
using namespace std;
struct dataStruct
{
uint32_t header;
uint32_t *body;
};
int main(int argc, char* argv[])
{
int size, streamSize;
// 4 bytes per size + 4 bytes for header
size = 1;
streamSize = (size * 4) + 4;
// Create a stream of bytes of appropriate size
uint8_t *buffer = new uint8_t[streamSize];
// Cast stream as structure
dataStruct *sStream = (dataStruct *)buffer;
// Populate structure with nice 101010... binary patterns
sStream->header = 2863311530;
sStream->body = new uint32_t[1];
sStream->body[0] = 2863311530;
cout << "Struct: " << sStream->header << ", " << sStream->body[0] << endl;
// Look at raw data in stream
for (int i=0; i<sizeof(buffer); i++)
{
std::bitset<8> x(buffer[i]);
cout << "[" << i << "]->" << x << endl;
}
return 0;
}
The output is:
Struct: 2863311530, 2863311530
[0]->10101010
[1]->10101010
[2]->10101010
[3]->10101010
[4]->00000000
[5]->00000000
[6]->00000000
[7]->00000000
Why is index 4-7 not the same as 0-3? Both sStream->header and sStream->body contain the same values. They are mapped to the buffer. Is this because body is an array? If so how would I manipulate the stream for this to work when using an array?
Thanks
You are using uninitialized varieable size in:
streamSize = (size * 4) + 4;
Everything after that depends on streamSize is suspect and is a cause for undefined behavior.
Update
Even after size is initialized to 1, there are problems. Let's me walk through the code and how it affects the memory you have allocated.
After you execute the line:
uint8_t *buffer = new uint8_t[streamSize];
you have buffer pointing to memory like this:
buffer
|
v
+---+---+---+---+---+---+---+---+
| | | | | | | | |
+---+---+---+---+---+---+---+---+
After you have executed the line:
dataStruct *sStream = (dataStruct *)buffer;
you have sStream pointing to the same memory like:
sStream
|
v
+---+---+---+---+---+---+---+---+
| | | | | | | | |
+---+---+---+---+---+---+---+---+
If your compiler does not add any padding to the members of dataStruct (the best case scenario), you'll have:
sStream.header sStream.body
| |
v v
+---+---+---+---+---+---+---+---+
| | | | | | | | |
+---+---+---+---+---+---+---+---+
If your compiler adds padding to dataStream.header, sStream.body will point to something different. Worst case scenario: You have a 64-bit compiler. It adds 32 bits of padding to dataStream.header. In that case, you will have:
sStream.header sStream.body
| |
v v
+---+---+---+---+---+---+---+---+
| | | | | | | | |
+---+---+---+---+---+---+---+---+
Then, you will end up using unathorized memory when you try to assign anything to sStream.body, like:
sStream->body = new uint32_t[1];
Best case scenario, you have 32 bit compiler and there is no padding added to dataStream.header. Looks like you have a 64-bit compiler. Even if you compiler does not add any padding to dataStream.header, you are still looking at a memory overrun problem if sizeof(void*) is 64 bits, which I think you do.
Let's take the best case scenario of a 32 bit compiler that doesn't add any padding and the member of sStream point to the allocated memory like:
sStream.header sStream.body
| |
v v
+---+---+---+---+---+---+---+---+
| | | | | | | | |
+---+---+---+---+---+---+---+---+
After you execute the line:
sStream->header = 2863311530;
the memory looks like:
sStream.header sStream.body
| |
v v
+---+---+---+---+---+---+---+---+
| 2863311530 | |
+---+---+---+---+---+---+---+---+
After you execute the line:
sStream->body = new uint32_t[1];
the memory looks like:
sStream.header sStream.body
| |
v v
+---+---+---+---+---+---+---+---+
| 2863311530 | SomeMemory |
+---+---+---+---+---+---+---+---+
SomeMemory
|
v
+---+---+---+---+
| |
+---+---+---+---+
After you execute the line:
sStream->body[0] = 2863311530;
SomeMemory gets populated and looks like:
SomeMemory
|
v
+---+---+---+---+
| 2863311530 |
+---+---+---+---+
I think you were surprised to see that the memory pointed to by buffer does not look like:
buffer
|
v
+---+---+---+---+---+---+---+---+
| 2863311530 | 2863311530 |
+---+---+---+---+---+---+---+---+
I hope it makes sense now why it does not.

Pointer-to-pointer access

Can somebody explain this to me:
(((((*(parent))->leaves))[7])->zone_id)
pointers to pointers are really confusing to me. This is the expression I got from the watch in debug mode. I am trying to access to the string zone_id inside the last element of tree (with 10 elements, each element for different number 0-9).
EDIT: this is the whole search function, hope it is enough to understand:
string Tree::search(string str, node** parent, int num) {
int value;
if (num < str.length()) {
value = boost::lexical_cast<int> (str.substr(num, 1));
if ((*parent)->leaves[value] != NULL && num != str.length() -1) {
search (str, &((*parent)->leaves[value]), num+1);
} else if (num == str.length() -1) {
if ( (*(parent)->leaves)[value]->zone_id.empty() )
cout<<"Yep.";
else
return (string) "No_results.";
}
}
}
and structure:
struct node {
string zone_id;
node* leaves [10];
};
Well let's get rid of some brackets to simplify it a bit:
(*parent)->leaves[7]->zone_id
Now it's much easier to understand. We are dereferencing parent (*parent) which gives us a pointer to some object that has an array member called leaves. So we access the element of that array with index 7, which gives us another pointer, this time pointing to an object that has a member called zone_id. We then access that zone_id member.
This is all assuming there's no operator overloading involved.
Diagrammatically (an arrow is "points to"):
________ _________ ___________ ___________
| parent |-->| *parent |-->| struct: | ,-->| struct: |
|________| |_________| | leaves[0] | | | zone_id |
| leaves[1] | | | ... |
| leaves[2] | |
| leaves[3] | |
| leaves[4] | |
| leaves[5] | |
| leaves[6] | |
| leaves[7] | --'
| leaves[8] |
| ... |
Removing the parenthesis makes it actually easier to read, in my mind:
(*parent)->leaves[7]->zone_id
So, we have a pointer to a pointer of leaves. (*parent) makes a dereference to that pointer (so fetches what it the pointer points at). So now we have a pointer to leaves, which is an array of 10, so we use element 7, and the pointer here is used to fetch the zone_id.
It does get a bit complicated, but this is far from the most complicated structure I have seen. If it helps you, you could break it down:
Parent *this_parent = *parent;
Leave *leaf = this_parent->leaves[7];
... use leaf->zone_id;
(
(
(
(
(
*(parent)
)
->leaves
)
)
[7]
)
->zone_id
)
dereference parent
access the leaves member
index the 7th element
access the zone_id member.