Why vector.size()-1 gives garbage value? [duplicate] - c++

This question already has answers here:
How do unsigned integers work
(3 answers)
Closed 5 years ago.
I had tried running this code
// vector::size
#include <iostream>
#include <vector>
int main ()
{
std::vector<int> myints;
std::cout << "size: " << myints.size() << '\n';
std::cout << "size: " << myints.size()-1 << '\n';
return 0;
}
And Surprisingly the output came
0
garbage Value
It should be
0
-1
Here's the :code

myints.size() is an unsigned type: formally a std::vector<int>::size_type. Subtracting 1 from an unsigned type with a value of 0 will cause wrap-around effects, in your case, to
std::numeric_limits<std::vector<int>::size_type>::max()
It would not have printed "garbage value": but the number above, which will be one less than a large power of 2.

Related

Is it necessary to check range in bit representation C++ [duplicate]

This question already has an answer here:
`std::bitset` with and without boundary checks
(1 answer)
Closed 1 year ago.
Some data are stored in a 64 bit integer. As you can see, inside the getDrvAns function I check if the bit in the position drvIdx-1 is 0 or 1. One cannot be sure if the drvIdx will have a value in the right range (1-64). However, I noticed that if we put a value higher that 64, we have a wrap-around effect as demonstrated in the code below.
My question is, is this a standard behavior? Is it safe to leave it as it is without boundary checks?
Note: If the bit is set to 0 the drive has answered.
uint64_t mDrvAns = 48822;
bool getDrvAns(int drvIdx) {
std::bitset<64> bitRepOfmDrvAns{mDrvAns};
return (bitRepOfmDrvAns[drvIdx - 1] != 0ull);
};
std::string yesOrNo(bool val) {
return ((val==false) ? "Yes" : "No");
}
int main()
{
int drvIdx = 1;
std::bitset<64> bitsOfDrvAns{mDrvAns};
std::cout << bitsOfDrvAns << std::endl;
std::cout << "Has drv " << drvIdx << " answered? " << yesOrNo(getDrvAns(drvIdx))
<< std::endl;
drvIdx = 65;
std::cout << "Has drv " << drvIdx << " answered? " << yesOrNo(getDrvAns(drvIdx))
<< std::endl;
return 0;
}
According to the documentation, out of bounds access using operator[] is Undefined Behaviour. Don't do it.
If you don't want to check the bounds yourself, call test() instead, and be prepared to handle the exception if necessary.

Size of Vector yielding negative on substraction [duplicate]

This question already has answers here:
How do unsigned integers work
(3 answers)
Closed 5 years ago.
I had tried running this code
// vector::size
#include <iostream>
#include <vector>
int main ()
{
std::vector<int> myints;
std::cout << "size: " << myints.size() << '\n';
std::cout << "size: " << myints.size()-1 << '\n';
return 0;
}
And Surprisingly the output came
0
garbage Value
It should be
0
-1
Here's the :code
myints.size() is an unsigned type: formally a std::vector<int>::size_type. Subtracting 1 from an unsigned type with a value of 0 will cause wrap-around effects, in your case, to
std::numeric_limits<std::vector<int>::size_type>::max()
It would not have printed "garbage value": but the number above, which will be one less than a large power of 2.

How to change value of const variable via its address? [duplicate]

This question already has answers here:
How is a variable at the same address producing 2 different values? [duplicate]
(4 answers)
behavior of const_cast in C++ [duplicate]
(3 answers)
Closed 7 years ago.
I'm trying to change value of const variable via its address.
following this code:
#include <iostream>
#include <string>
#include <stdlib.h>
#include <stdint.h>
#include <time.h>
#include <stdio.h>
using namespace std;
int main(void)
{
uint64_t const x = -1;
uint64_t *b = reinterpret_cast<uint64_t*>(0x28ff10);
cout<< x << endl;
cout<< &x << " " << b << " " << *b << endl;
printf("%p\n", &x);
*b = 10;
cout<< &x << " " << x << " " << b << " " << *b << " " << *(reinterpret_cast<uint64_t*>(0x28ff10)) <<endl;
return 0;
}
Compiled with MinGW 4.8.1:
g++ -g main.cpp && ./a.exe
And this is output:
18446744073709551615
0x28ff10 0x28ff10 18446744073709551615
0028FF10
0x28ff10 18446744073709551615 0x28ff10 10 10
Could anyone explain it ?
EDIT:
Figured out! compile still optimized my variable although I compiled it with -O0. Looked at ASM generated, I saw that printf and cout put directly the value instead of the variable symbol.
So, to make my code do right behavior, I have to declared it with volatile static
I'm trying to change value of const variable via its address.
You've already gone wrong by this point.
const is short for "constant".
You cannot mutate a constant.
Sometimes you can get it to sort of look like you did, but doing so has undefined behaviour. You told your compiler that it can make all sorts of assumptions about x (including optimising it out from your binary entirely!) because you promise that you'll never change it. Then you change it.
No dinner for you tonight, says Mr. Compiler!

max possible int in c++ [duplicate]

This question already has answers here:
maximum value of int
(7 answers)
Closed 9 years ago.
I thought that unsigned int max possible value is 65535 ( in C++ ) but I created a programm which can use an int which is equal with 100000 for example. Is it safe to use int with values up to 10000000 or the program may crash? In that case is long the only solution?
Thank you for your time!
Use std::numeric_limits<unsigned int>::max() to know for certain what this value is.
Please check the code below for limits:
#include <iostream> // std::cout
#include <limits> // std::numeric_limits
int main () {
std::cout << "Minimum value for int: " << std::numeric_limits<int>::min() << '\n';
std::cout << "Maximum value for int: " << std::numeric_limits<int>::max() << '\n';
std::cout << "int is signed: " << std::numeric_limits<int>::is_signed << '\n';
std::cout << "Non-sign bits in int: " << std::numeric_limits<int>::digits << '\n';
std::cout << "int has infinity: " << std::numeric_limits<int>::has_infinity << '\n';
return 0;
}
INT_MAX is implementation defined. That means it's up to your compiler vendor to decide, as long as it's no less than 32767, and greater or equal to a short. You can use the climits definitions to discover your implementation's limits:
#include <iostream>
#include <climits>
int main () {
std::cout << INT_MAX << std::endl;
return 0;
}
On my installation of gcc/g++ v4.8.1 targeting x86_64-linux-gnu, this snippet produces:
2147483647
And as has been mentioned in the followup replies to this answer, you may (and probably should) use the more semantically proper (for C++) method:
#include <iostream>
#include <limits>
int main () {
std::cout << std::numeric_limits<int>::max() << std::endl;
return 0;
}
...which ought to produce the same output.

prepending number with 0's [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Print leading zeros with C++ output operator (printf equivalent)?
#include <iostream>
#include <iomanip>
int main()
{
int n = 16;
std::cout << ???(5) << n << std::endl;
}
I want the output to be 00016
setw() prepends with spaces. Isn't it configurable what characters to prepend with setw()?
My eventual goal is to print a hex 2-byte number in 4 positions. Something like this:
#include <iostream>
#include <iomanip>
int main()
{
unsigned short n = 0xA7;
std::cout << std::hex << ???(4) << n << std::endl;
}
and I am expecting to get this output: 00A7
You also need setfill('0').