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.
Related
This question already has answers here:
Accessing an array out of bounds gives no error, why?
(18 answers)
Closed 10 months ago.
When creating a pointer using new keyword or <stdlib.h>'s malloc, and put its size 0; what ever is the index number it doesnt give an error and works. ( i am not sure if this works if the size bigger then 0, because some times when i allocate a memory block and place an element outside its range program crashes).
My question : is this a c++ thing? Or just from my compiler? Is it safe to use it for arrays i don't want to limit it with specific size. Because i am thinking to use this in my game to make unlimited world generation and save it ( Not all terrain just the breakable and placable objects)
#include <iostream>
using namespace std;
int main() {
int* x = new int[0];
x[100] = 0;
cout << x[100];
return 0;
}
is this a c++ thing? Or just from my compiler?
It's a bug in your program. The behaviour of the program is undefined.
Is it safe to use it
It is not safe. The program is broken.
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?
This question already has answers here:
Change stack size for a C++ application in Linux during compilation with GNU compiler
(5 answers)
Closed 5 years ago.
WRT my question: Code execution breaking during recursion
I don't want to increase the stack size for every application running in my account. Just one C++ executable.
What is the way to do it?
If you really need huge amount of stack space you can make use of Boost Context and temporarily allocate a custom amount of stack space. This works on a per-thread level so it might not answer your question. However, as already said in comments, you should probably improve your algorithm as increasing stack size is hardly ever needed.
#include <boost/context/fixedsize_stack.hpp>
#include <boost/context/continuation.hpp>
void recurse(int depth)
{
std::cout << depth << std::endl;
recurse(depth + 1); // infinite recursion to test how far you can go
std::cout << depth << std::endl; // just to avoid tail recursion optimization.
}
int main(int argc, char* argv[])
{
namespace ctx = boost::context;
ctx::fixedsize_stack my_stack(100 * 1024 * 1024); // adjust stack size for your needs
ctx::continuation source =
ctx::callcc(std::allocator_arg, my_stack, [](ctx::continuation&& sink) {
// everything in here runs within a context with your custom stack.
recurse(1);
return std::move(sink);
});
// This starts the context and executes the lambda above.
source = source.resume();
return 0;
}
On Linux using GCC or Clang you could also replace fixedsize_stack with segmented_stack, which automatically grows until you run out of memory.
Boost Context is portable among most major platforms.
This question already has answers here:
Define bitset size at initialization?
(7 answers)
Closed 5 years ago.
Now I've this code:-
The code is about taking in an integer and providing its binary form in the given number of bits.
#include <iostream>
#include <bitset>
using namespace std;
int main(){
//creating instance using bitset (6 bit). here you can specify the length such as 8,16,32,64...
int n=5;
bitset< 6 > btFlaged;
//assigning integer value to instance
btFlaged = 7;
//print bit string in the string
for(int i=btFlaged.size()-1;i>-1;i--)
{
cout <<btFlaged.test(i);
}
}
How do I use an integer(E.g. n) in the place of '6' so that a value entered at run-time can be used in the code?
I've done some research on the net and I know that bitset needs a value at compile time and so instead of bitset, I should use vector bool but I don't know how I should incorporate that in the program?
If any of you guys can tell me how to use vector or if you have an altogether different theory on how to the the task done, please do share.
Also I cant use boost:dynamic_bitset as the code is going to be judged by an online judge which may not have the seperate header file.
a std::bitset's size must be set at compile time as it is a template parameter. If you need a dynamic bitset you can look at boost:dynamic_bitset
This question already has answers here:
Declaring an array of negative length
(3 answers)
Closed 8 years ago.
#include <iostream>
using namespace std;
int main()
{
cout<<"started "<<endl;
int n= -2;
int array[n];
array[0]=100;
array[1]=200;
cout<<array[0]<<endl;
cout<<array[1]<<endl;
cout<<"over"<<endl;
return 0;
}
Why does this compile and run? I expected a compilation error because value of n is negative.
Variable-length arrays are not a C++ feature, so this line will cause a compilation error in a compiler that is ordered to strictly adhere to the C++ specification, regardless of the value of n (unless n is const and can be determined at compile-time):
int array[n];
Likely you are using a compiler that supports variable-length arrays as an extension. Therefore, the rules regarding what is valid depend entirely on the specific compiler and compiler options, and any code that you write using this extension will not be portable to compilers that don't support this non-standard feature.