structure padding in C/C++ [duplicate] - c++

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.

Related

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.

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

Size of object containing reference variable [duplicate]

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;
}

Determine if subtracting from an unsigned int will roll over [duplicate]

This question already has answers here:
Checking for underflow/overflow in C++?
(5 answers)
Saturating subtract/add for unsigned bytes
(11 answers)
Closed 3 years ago.
How can it be possible to determine if subtracting n from an unsigned int will roll over to a negative value, considering that casting to a signed int can result in a negative value already?
Example
#include <iostream>
using namespace std;
int main(){
unsigned int i = 2147483647*2;
if((int)i - 1 < 0){
cout << "rolled over";
}
else {
i = 0;
}
return 0;
}
In order to check if subtracting from an unsigned int would roll over, you could cast it to an int first. However, if the unsigned int is > the max int value, you will already end up negative. So how can I prevent an unsigned int from rolling over?

About the pow function in C++ [duplicate]

This question already has answers here:
Why does pow(n,2) return 24 when n=5, with my compiler and OS?
(4 answers)
Closed 6 years ago.
I just tried three pieces of code:
#include<cstdio>
#include<cmath>
#include<cstring>
int main()
{
int a = 3;
int b = pow(10,a);
printf("%d",b);
return 0;
}
//Output:1000
#include<cstdio>
#include<cmath>
#include<cstring>
int main()
{
int a = 3;
int b = pow(10,a-1);
printf("%d",b);
return 0;
}
//Output:99
#include<cstdio>
#include<cmath>
#include<cstring>
int main()
{
int a = 3;
int b = pow(10,a-2);
printf("%d",b);
return 0;
}
//Output:10
I would like to know why the second block of code will output 99, is it because of floating point precision? Or is it because I should use float numbers in the pow function?(Such as 10.0)
I'm usually confused about the accuracy of C++, I will be grateful for your help.
For integer exponents the following template can be very handy:
template <typename T> inline constexpr T pow( T base, int exponent )
{
return (exponent == 0) ? static_cast<T>(1.0) : ( (exponent>0) ? base*pow(base, exponent-1) : pow( static_cast<T>(1.0)/base, -exponent ) );
}
If you plan to use it with a C++ standard prior C++11, just remove the constexpr keyword.
Conversion of a floating-point value to an integer is done by truncation — you get the next integer closest to zero. If pow is imprecise and too low, then truncation will exacerbate it.
lround(pow(10,2)) might be more appropriate.