Why this code doesn't run in Windows? C++ | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Why this code doesn't run in Windows? C++

I have this code, where I got the time that takes some sort methods and print that time. Because I work in Ubuntu, it works perfectly, but when I tried to run it in Windows, doesn't work. I know that Ubuntu and windows are different, and I really tried to code the most neutral possible, but in this case, I cannot see what part of the code takes away portability. How can I solve this problem? This is the first time that something like this happens to me, what considerations do I need to take to code the most neutral possible (if you don't work in Windows) https://code.sololearn.com/cehOJYLf42WT/?ref=app This code needs to be compiled in an IDE doesn't work for sl

9th Mar 2019, 7:52 AM
Eduardo Perez Regin
Eduardo Perez Regin - avatar
3 Answers
+ 1
Inside SortMethods: static const int MAX = 100000; T elements[MAX]; Your class is 0.4 mb big, and you have 6 of em, it's overflowing the stack. The stack really isn't that big and the only reason why it doesn't crash on linux is because the default stack size is a bit bigger there... usually. Either decrease the size of the array or switch over to heap allocation. Also from: https://en.cppreference.com/w/cpp/numeric/random/random_device " std::random_device may be implemented in terms of an implementation-defined pseudo-random number engine if a non-deterministic source (e.g. a hardware device) is not available to the implementation. In this case each std::random_device object may generate the same number sequence. "
9th Mar 2019, 9:29 AM
Dennis
Dennis - avatar
+ 1
Yes, with heap allocation I mean that you should allocate the array using new if it's that big. I'm not sure how to check what the stack size is, you can change it, I guess a good rule of thumb is to assume that the maximum stack size is 1mb. Might have to do some googling on that. :) std::random_device is not guaranteed to give a random number every time you run the program. Some compilers, like gcc, return the same number every time ( at least for me on windows it constantly returns 3499211612 ). Unless you do some tests yourself on your own platform and are sure that it returns different numbers every time you should probably avoid it. Normally, when I'm in a hurry and I just seed std::mt19937 with time(0) otherwise I seed it with the rdtsc instruction of the cpu. This instruction returns the number of clock cycles your cpu has performed since startup. You can access it with inline asm: uint64_t rdtsc() { uint32_t l, h; __asm__ __volatile__( "rdtsc" : "=a"(l), "=d"(h) ); return (uint64_t)h << 32 | l; } Just keep in mind that some compilers implement inline asm a little different, gcc uses (), strings and AT&T syntax and visual studio uses {}, no strings, and intel syntax by default. ( and few other differences )
9th Mar 2019, 3:12 PM
Dennis
Dennis - avatar
0
With heap allocation you mean dynamic memory? Also, I'm new trying to use std:: random_device , how can I use it correctly in this case? I guess one consideration when I'm managing big arrays is the stack size of the operating system right? But how can I know that stack size?
9th Mar 2019, 2:21 PM
Eduardo Perez Regin
Eduardo Perez Regin - avatar