Ze BOy
Ze BOy

Reputation: 1

How to deal with large sizes of data such as array or just number that causing stack in Cpp?

its my first time dealing with large numbers or arrays and i cant avoid over stacking i tried to use long long to try to avoid it but it shows me that the error is int main line :

CODE:

#include <iostream>
using namespace std;
int main()
{
   long long n=0, city[100000], min[100000] = {10^9}, max[100000] = { 0 };
   cin >> n;
   for (int i = 0; i < n; i++) {
       cin >> city[i];
   }
   for (int i = 0; i < n; i++) 
   {//min
       for (int s = 0; s < n; s++) 
       {
           if (city[i] != city[s]) 
           {
               if (min[i] >= abs(city[i] - city[s])) 
               {
                   min[i] = abs(city[i] - city[s]);
               }
           }
       }
   }
   for (int i = 0; i < n; i++) 
   {//max
       for (int s = 0; s < n; s++) 
       {
           if (city[i] != city[s]) 
           {
               if (max[i] <= abs(city[i] - city[s])) 
               {
                   max[i] = abs(city[i] - city[s]);
               }
           }
       }
   }
   for (int i = 0; i < n; i++) {
       cout << min[i] << " " << max[i] << endl;
   }
}
**ERROR:**
Severity    Code    Description Project File    Line    Suppression State
Warning C6262   Function uses '2400032' bytes of stack:  exceeds /analyze:stacksize '16384'.  Consider moving some data to heap.

then it opens chkstk.asm and shows error in : test dword ptr [eax],eax ; probe page.

Upvotes: 0

Views: 155

Answers (1)

Marcus M&#252;ller
Marcus M&#252;ller

Reputation: 36433

Small optimistic remark:

100,000 is not a large number for your computer! (you're also not dealing with that many arrays, but arrays of that size)

Error message describes what goes wrong pretty well:

You're creating arrays on your current function's "scratchpad" (the stack). That has very limited size!

This is C++, so you really should do things the (modern-ish) C++ way and avoid manually handling large data objects when you can.

So, replace

long long n=0, city[100000], min[100000] = {10^9}, max[100000] = { 0 };

with (I don't see any case where you'd want to use long long; presumably, you want a 64bit variable?) (10^9 is "10 XOR 9", not "10 to the power of 9")

constexpr size_t size = 100000;
constexpr int64_t default_min = 1'000'000'000;
uint64_t n = 0;
std::vector<int64_t> city(size);
std::vector<int64_t> min_value(size, default_min);
std::vector<int64_t> max_value(size, 0);

Additional remarks:

  • Notice how I took your 100000 and your 10⁹ and made them constexpr constants? Do that! Whenever some non-zero "magic constant" appears in your code, it's a good time to ask yourself "will I ever need that value somewhere else, too?" and "Would it make sense to give this number a name explaining what it is?". And if you answer one of them with "yes": make a new constexpr constant, even just directly above where you use it! The compiler will just deal with that as if you had the literal number where you use it, it's not any extra memory, or CPU cycles, that this will cost.
  • Matter of fact, that's even bad! You pre-allocating not-really-large-but-still-unneccesarily-large arrays is just a bad idea. Instead, read n first, then use that n to make std::vectors of that size.
  • Don not using namespace std;, for multiple reasons, chief among them that now your min and max variables would shadow std::min and std::max, and if you call something, you never know whether you're actually calling what you mean to, or just the function of the same name from the std:: namespace.
    Instead using std::cout; using std::cin; would do for you here!
  • This might be beyond your current learning level (that's fine!), but
for (int i = 0; i < n; i++) {
  cin >> city[i];
}

is inelegant, and with the std::vector approach, if you make your std::vector really have length n, can be written nicely as:

for (auto &value: city) {
    cin >> value;
}

This will also make sure you're not accidentally reading more values than you mean when changing the length of that city storage one day.

  • It looks as if you're trying to find the minimum and maximum absolute distance between city values. But you do it in an incredibly inefficient way, needing multiple loops over 10⁵·10⁵=10¹⁰ iterations.
    • Start with the maximum distance: assume your city vector, array (whatever!) were sorted. What are the two elements with the greatest absolute distance?
    • If you had a sorted array/vector: how would you find the two elements with the smallest distance?

Upvotes: 2

Related Questions