Data execution in Win64 [duplicate] - c++

This question already has answers here:
How to alloc a executable memory buffer?
(5 answers)
__asm__ gcc call to a memory address
(1 answer)
Closed 3 years ago.
When programming in Win32, one can execute data as follows:
#include <iostream>
using namespace std;
typedef double(*func)(void);
int main()
{
// The sample program to display the PI number
uint8_t code[] = { 0xD9,0xEB,0xC3 }; // fldpi; ret
uint8_t* p = code;
func f = reinterpret_cast<func>(p);
cout << f() << endl;
return 0;
}
This trick allows to speed up some numeric calculations when the expression to calculate is initially unknown.
The only thing you need to do this in Win32 is to disable data execution protection by using the /NXCOMPAT:NO Visual Studio Option.
But how to do this in Win64? Is this even possible?

Related

Error : 'For statement require a suitable begin.' [duplicate]

This question already has answers here:
Passing Arrays to Function in C++
(5 answers)
Variable length arrays (VLA) in C and C++
(5 answers)
What is array to pointer decay?
(11 answers)
Closed 4 months ago.
This program is to find Frequency of different elements in array using unordered map. But I am unable to debug it, as this error is occuring for the first time in my code. Please help me debug this code.
#include<iostream>
#include<unordered_map>
using namespace std;
void frequency(int arr[] , int n)
{
unordered_map<int,int> m;
for(int x : arr) /* This line is showing error(for statement require a suitable begin function and none was found .)*/
m[x]++;
for( auto x : m)
cout<<x.first<<" : "<<x.second;
}
int main()
{
int n = 8;
int arr[n] = {10,12,10,15,10,20,12,12};
frequency(arr,n);
}

Garbage Value of a defined namespace [duplicate]

This question already has answers here:
How to print function pointers with cout?
(7 answers)
Why am I always getting output as 1 when printing a function?
(2 answers)
Closed 12 months ago.
So, I just started with C++ and was doing random things with a snippet of code I found on the net. The code is a simple use of defining namespace. After my changes in the code, it looked like,
#include <iostream>
using namespace std;
namespace ns1 { int value() {return 5;}}
namespace ns2 { int value() {return -5;}}
int main() {
cout << ns1::value<< '\n'; //5 will be displayed
cout << ns2::value<< '\n'; // -5 will be displayed
}
Now, i know that i have called the wrong function and it should be ns1::value() instead of ns1::value , but my question is why is the code still working? And why is it giving an output of 1?

Increase recursion depth [duplicate]

This question already has answers here:
Change stack size for a C++ application in Linux during compilation with GNU compiler
(5 answers)
Closed 1 year ago.
How to increase stack/recursion depth of g++ in Linux?
You need to include a header file
#include <sys/resource.h>
and in the main() function you need to write these lines as...
int main(){
rlimit R; // Unsigned integral type used for limit values.
getrlimit(RLIMIT_STACK, &R);
R.rlim_cur = R.rlim_max; // Limit on stack size.
setrlimit(RLIMIT_STACK, &R);
// your code
}
Learn more about <sys/resource.h> here.

where does struct object get memory in main function? [duplicate]

This question already has answers here:
Are data members allocated in the same memory space as their objects in C++?
(6 answers)
Closed 4 years ago.
how does tmp get memory from the machine, from heap or stack?
I thought it was from the stack, but it seem that the code can run properly
#include<bits/stdc++.h>
using namespace std;
struct node {
int a[1000000];
};
int main() {
node tmp;
memset(tmp.a, -1, sizeof(tmp.a));
cout << tmp.a[0];
return 0;
}
In stack, since it's an automatic variable to the main function.
PS: This code doesn't compile, for example with this error: error: type 'node' does not provide a subscript operator: cout << tmp[0];.

Using const_cast<> and changing the value at the address does not change original variable [duplicate]

This question already has answers here:
How is a variable at the same address producing 2 different values? [duplicate]
(4 answers)
Closed 7 years ago.
#include <iostream>
using namespace std;
int main()
{
const int kiNum = 100;
int* ptr = const_cast<int*>(&kiNum);
*ptr = 200;
cout<<"kiNum: "<<kiNum; // The value still prints 100 on the console??
return 0;
}
output:
kiNum = 100
In the above code snippet , i am trying to change the value of a const integer, after const_cast and then change the value at the address, but the console still prints the old value (i am using visual studio 2012)
Writing to something which is defined as const is undefined (assuming you cast away the const of course).
http://en.cppreference.com/w/cpp/language/const_cast
It's a pretty accurate website. If you have issues with a language feature its always worth looking up there IMHO.