Size of object containing reference variable [duplicate] - c++

This question already has answers here:
Why isn't sizeof for a struct equal to the sum of sizeof of each member?
(13 answers)
why reference size is always 4 bytes - c++
(2 answers)
Closed 2 years ago.
This is printing 16 and 4 as the answer but it should be printing as 8 and 4 because both a and b are integer type variables. So can we think this as the compiler stores the address of the variable a in a separate variable while copying to b that is why it is resulting in 4 + 4 + 8 = 16? If not then what is it?
#include <iostream>
using namespace std;
class C {
public:
int a = 45;
int &b = a;
};
int main() {
C ob1;
cout << sizeof(ob1) << endl;
cout << sizeof(ob1.b);
return 0;
}

Related

structure padding in C/C++ [duplicate]

This question already has answers here:
Structure padding and packing
(11 answers)
Why isn't sizeof for a struct equal to the sum of sizeof of each member?
(13 answers)
Closed 18 days ago.
# include <stdio.h>
# include <iostream>
using namespace std;
struct complex{
int real;
int img;
char x[5];
int add(struct complex c2){
real += c2.real;
img += c2.img;
}
void display(){
printf("%d + %d i",real,img);
}
};
int main(){
struct complex c1={10,15
};
struct complex c2 = {12,15
};
cout<<sizeof(c1)<<endl;
c1.add(c2);
c1.display();
}
I am getting 16 as its size in sizeof();how does it work ,Can you guys please help me?,I am getting 16 as its size for character array from size 5 to 8,from 9 it is 20.

characters & pointers and Arithmetic? [duplicate]

This question already has answers here:
Pointer addition vs subtraction
(8 answers)
Pointer subtraction confusion
(8 answers)
Closed 7 months ago.
#include<bits/stdc++.h>
using namespace std;
int main(){
float arr[5] = {12.5, 10.0, 13.5, 90.5, 0.5};
float *ptr1 = &arr[0];
float *ptr2 = ptr1 + 3;
cout<<*ptr2<<" ";
cout<<ptr2 - ptr1;
return 0;
}
Correct output of this code is -> 90.5 3
But I am unable to get the concept behind it. please if anybody can explain how we got this output.

return function values as pointer [duplicate]

This question already has answers here:
What does the comma operator , do?
(8 answers)
How does the Comma Operator work
(9 answers)
Returning a second value through pointer or reference C++
(4 answers)
Closed 6 months ago.
Question:
Make function which will sum two integer variables (a,b) and then return result in variable rez , using pointer. Also, in the same function, return sum a+b+10 in another variable rez_a using pointer.
Well, here is the code. It returns only the first value (*p1):
#include <iostream>
using namespace std;
int vrati(int a, int b) {
int rez = a + b;
int rez_a = a + b + 10;
int* p1 = &rez;
int* p2 = &rez_a;
return *p1,*p2;
}
int main() {
int a = 4;
int b = 6;
cout << vrati(a, b);
return 0;
}
I think you are taking return too literally. I expect the function you are meant to write is this
void vrati(int a, int b, int *rez, int* rez_a) {
*rez = a + b;
*rez_a = a + b + 10;
}
int main() {
int a = 4;
int b = 6;
int rez, rez_a;
vrati(a, b, &rez, &rez_a);
cout << rez << ' ' << rez_a;
return 0;
}
This function returns values, but it doesn't use return. I can understand why you were confused.

Weird behaviour when using the modulo operator between a negative int and std::size_t [duplicate]

This question already has answers here:
Why doesn't a negative number modulo a vector size give a negative number? [duplicate]
(5 answers)
Closed 1 year ago.
This code snippet:
#include <iostream>
#include <cstddef>
int main()
{
int a{-4};
std::size_t b{3};
std::cout << a % b;
return 0;
}
prints
0
while this code snippet:
#include <iostream>
int main()
{
int a{-4};
int b{3};
std::cout << a % b;
return 0;
}
prints
-1
So, why does the first code snippet returns 0? Why does changing b from std::size_t to int print the right result?
Oh, it's because a implicitly converted into std::size_t becuase b is std::size_t, which is 4294967292(on my machine), and 4294967292 % 3 == 0

What is " int x,y,z = 1 " doing, if not assigning 1 to each? [duplicate]

This question already has answers here:
How can I declare and define multiple variables in one line using C++?
(10 answers)
Value assignment in C while declaring multiple variable in 1 line
(1 answer)
What happens in C when you print a declared, but unassigned variable?
(2 answers)
Why does the C standard leave use of indeterminate variables undefined?
(5 answers)
(Why) is using an uninitialized variable undefined behavior?
(7 answers)
Closed 1 year ago.
I am new to C/C++ and I noticed my program calculating wrong values. I found the problem to be my understanding of how the declaration of variables works in C/C++.
x,y,z = 1,2,3 works fine in Python where as x,y,z = 1 does not.
int x,y,z = 1,2,3 doesnt not work in C/C++ but int x,y,z = 1 does, kind of, since cout << x; outputs 16 and I expected it to be 1.
#include <iostream>
using namespace std;
int main() {
int x, y, z = 1;
cout << x;
return 0;
}
// The output is 16
What is the operation int x,y,z = 1 doing, if not assigning 1 to each?
It is shorthand for:
int x;
int y;
int z = 1;
x and y are left unassigned.
int x,y,z = 1 initializes ONLY z to 1, x and y are left uninitialized.