Is the a memory issue? c++ [code blocks][Windows] - c++

My code blocks of able to compile the code below and worked well:
unsigned long int a=100000000000;
My code blocks of able to compile the code below But my program crashed immediately after i enter the same number(100000000000):
unsigned long int a;
cin>>a;
how to fix? Why the fist one is working !why not the second case?
if i entered a number more than max unsigned long int size! then why did the first work?

There is no reason for crash here.
The initialization causes a warning on my compiler because of truncation of an unsigned long long value, and the read attempt simply result in an failed read and does not change the value of the variable.
Code demonstrating it (on a 32 bit machine):
#include <iostream>
#include <iomanip>
using std::cin;
using std::cout;
using std::endl;
int main() {
unsigned long int b=100000000000; // warning here
unsigned long int a = 0;
cin>>a;
cout << "b:" << b << "(" << std::hex << b << ")" << " a:" << std::dec << a << endl;
return 0;
}
The output is as expected
b:1215752192(4876e800) a:0
because as an ULL on my architecture 100000000000 is 0x174876E800 and the high order bytes have been truncated...

Related

%X format specifier prints value only up to 4 bytes?

Hex value of 6378624653 is : 0x17C32168D
But this code prints : 0x7C32168D
#include<iostream>
int main()
{
int x = 6378624653;
printf("0x%x", x);
}
can anyone explain why this happens ? and what should I do to get the right output?
The obtained result means that an object of the type int can not store such a big value as 6378624653.
Try the following test program.
#include <iostream>
#include <limits>
int main()
{
std::cout << std::numeric_limits<int>::max() << '\n';
std::cout << 6378624653 << '\n';
std::cout << std::numeric_limits<unsigned int>::max() << '\n';
}
and see what the maximum value that can be stored in an object of the type int. In most cases using different compilers you will get the following output
2147483647
6378624653
4294967295
That is even objects of the type unsigned int can not store such value as 6378624653.
You should declare the variable x as having the type unsigned long long int.
Here is a demonstration program.
#include <cstdio>
int main()
{
unsigned long long int x = 6378624653;
printf( "%#llx\n", x );
}
The program output is
0x17c32168d

In C++, log2() and log2l() returning different values

While I was coding in C++ I stumbled upon something, the return value of log2(n) and log2l(n) are completely different for a certain value of n :
#include<bits/stdc++.h>
using namespace std;
int main(){
int d= log2l(288230376151711743);
cout<<d;
return 0;
}
This above code displays a value of 57 whereas the following code:
#include<bits/stdc++.h>
using namespace std;
int main(){
int d= log2(288230376151711743);
cout<<d;
return 0;
}
Displays a value of 58 upon execution.
Why is this occurring can someone please explain me ?
It's because you are typecasting double to integer datatype which is rounding it down.
#include <bits/stdc++.h>
using namespace std;
int main() {
cout << log2(288230376151711743) << "\n";
cout << log2l(288230376151711743) << "\n";
cout << (int) log2(288230376151711743) << "\n";
cout << (int) log2l(288230376151711743) << "\n";
cout << (long) log2(288230376151711743) << "\n";
cout << (long) log2l(288230376151711743) << "\n";
cout << (double) log2(288230376151711743) << "\n";
cout << (double) log2l(288230376151711743) << "\n";
return 0;
}
try to run this code and observe the results.
log2 docs
Try using the following expression for do-it-yourself:
auto log2 = log2(288230376151711743); // log2 => double | log2l() => long double
You'll get to know that variable d is a datatype set to double, again if you do the same thing with log2l(), you'll find that d is set to long double, i.e. the function log2l() returns a long double, but the value is rounding down while typecasting implicitly from a long double into int.
That's why, it'll show 58 for log2(). And in contrary, 57 for log2l() when the variable is an integer.

Using std::bitset for double representation

In my application i'm trying to display the bit representation of double variables.
It works for smaller double variables. Not working for 10^30 level.
Code:
#include <iostream>
#include <bitset>
#include <limits>
#include <string.h>
using namespace std;
void Display(double doubleValue)
{
bitset<sizeof(double) * 8> b(doubleValue);
cout << "Value : " << doubleValue << endl;
cout << "BitSet : " << b.to_string() << endl;
}
int main()
{
Display(1000000000.0);
Display(2000000000.0);
Display(3000000000.0);
Display(1000000000000000000000000000000.0);
Display(2000000000000000000000000000000.0);
Display(3000000000000000000000000000000.0);
return 0;
}
Output:
/home/sujith% ./a.out
Value : 1e+09
BitSet : 0000000000000000000000000000000000111011100110101100101000000000
Value : 2e+09
BitSet : 0000000000000000000000000000000001110111001101011001010000000000
Value : 3e+09
BitSet : 0000000000000000000000000000000010110010110100000101111000000000
Value : 1e+30
BitSet : 0000000000000000000000000000000000000000000000000000000000000000
Value : 2e+30
BitSet : 0000000000000000000000000000000000000000000000000000000000000000
Value : 3e+30
BitSet : 0000000000000000000000000000000000000000000000000000000000000000
My worry is why bitset always gives 64, zero for later 3. Interestingly "cout" for the actual values works as expected.
If you look at the std::bitset constructor you will see that it either takes a string as argument, or an integer.
That means your double value will be converted to an integer, and there is no standard integer type that can hold such large values, and that leads to undefined behavior.
If you want to get the actual bits of the double you need to do some casting tricks to make it work:
unsigned long long bits = *reinterpret_cast<unsigned long long*>(&doubleValue);
Note that type-punning like this is not defined in the C++ specification, but as long as sizeof(double) == sizeof(unsigned long long) it will work. If you want the behavior to be well-defined you have to go through arrays of char and char*.
With C++14, std::bitset now takes an unsigned long long constructor, so this might work:
union udouble {
double d;
unsigned long long u;
};
void Display(double doubleValue)
{
udouble ud;
ud.d = doubleValue;
bitset<sizeof(double) * 8> b(ud.u);
cout << "Value : " << doubleValue << endl;
cout << "BitSet : " << b.to_string() << endl;
}
This should give you the internal representation of a double. See the working sample code on IdeOne.

Why does cout not print extern "C" variable?

Consider the following code:
extern "C" {
#include <lib.h>
}
#include <iostream>
int main() {
unsigned char a='a';
unsigned char b=some_struct_in_libh->unsignedchar;
cout << a << " " << b << endl; //Prints only a
printf("%u\n",b); //Prints b
cout << static_cast<int>(b) << endl; //Also prints b
return 0;
}
Why does it behave like this?
It's not printing only a at all. What you're seeing is instead that cout prints character type data as characters not as numbers. Your b is some character that's non-printable so cout is helpfully printing it as a blank space.
You found the solution by casting it to int.
EDIT: I'm pretty sure your printf is only working by chance because you told it to expect an unsigned int and gave it a character (different number of bytes).

How do I use GMP properly to do this operation?

My code in C++
long long N=1000000000000LL;
long long a = N;
mpz_class v;
mpz_mul(v, a, a);
cout<<v<<endl; //I want this to show 1000000000002000000000001
long long U=((sqrt(4*N+v)-1)/4); //not sure how to do this in GMP at all
cout << U << endl; //should show 250000000000
This is a snippet that shows what kind of operations I want to do. But I am not experienced enough with GMP to get it down, and the documentation is unclear to me. How do I correct all this?
mpz_class has no constructor from long long (it only goes up to unsigned long), so you'd have to use an intermediate string:
#include <gmpxx.h>
#include <iostream>
#include <string>
int main()
{
long long N = 1000000000000LL;
mpz_class a(std::to_string(N).c_str());
mpz_class v = a*a;
std::cout << v << '\n'; // shows 1000000000000000000000000
std::cout << (a+1) * (a+1) << '\n'; // THIS shows 1000000000002000000000001
mpz_class U = ((sqrt(4*a+v)-1)/4);
std::cout << U << '\n'; // shows 250000000000
}