C++ 大型数据集的堆栈溢出异常

C++ 大型数据集的堆栈溢出异常,c++,stack-overflow,C++,Stack Overflow,我试图解决这个问题:但是下面的程序由于集合大小较大(>20)而中断,引发堆栈溢出异常。是否发生内存泄漏?你能指出在哪里吗?另外,如果下面的实现涉及糟糕的编码实践,请让我知道。我正在努力提高我的业余编程技能。提前谢谢。 编辑:我编辑了下面的代码。我现在没有任何堆栈溢出异常。但是有谁能给我推荐一个更好的算法吗?谢谢 #include "stdafx.h" #include <iostream> #include <cmath> #include <winsock2.h&

我试图解决这个问题:但是下面的程序由于集合大小较大(>20)而中断,引发堆栈溢出异常。是否发生内存泄漏?你能指出在哪里吗?另外,如果下面的实现涉及糟糕的编码实践,请让我知道。我正在努力提高我的业余编程技能。提前谢谢。 编辑:我编辑了下面的代码。我现在没有任何堆栈溢出异常。但是有谁能给我推荐一个更好的算法吗?谢谢

#include "stdafx.h"
#include <iostream>
#include <cmath>
#include <winsock2.h>
#define SET 100
#define SUBSET 50

class node
{
    public:
    int data;
    node* next;
};

using namespace std;
int sum(int A[SUBSET]);
void combCalc(int S[SET]);
bool search(node* head, int sum);
void appendIfUnique(int total, bool nullHeader);

int _tmain(int argc, _TCHAR* argv[])
{

    cout << "Hello World" << endl; 
   int S [SET];
   for(int i=1; i<=SET; i++)
   {
       S[i-1]= (int)pow((float)i,2);
   }
  combCalc(S);
   cin.get();
   return 0;
}

static node *head; 
static node *current = head;
void combCalc(int S[])
{
   int row=0, col=0;
   int elePos[SUBSET], B[SUBSET];
   bool nullHeader = true;
       for(int z=0; z<SUBSET; z++) // initializing elePos
       {
            elePos[z]=z;
       }

       bool notDone = true;
       while (notDone || col <(SUBSET-1))
       {B[col] = S[elePos[col]];
            if(col==(SUBSET-1)) //finished forming a subset
            {

                notDone = false;
                for(int q=(SUBSET-1); q>=0; q--) //incrementing from the last element
                {
                    if(elePos[q]<(SET-SUBSET+q)) //checking if the element has reached its maximum
                    {
                        notDone = true;
                        elePos[q]++;
                        for(int w=q+1; w<SUBSET; w++) //setting the trailing elements to its minimum
                        {
                            elePos[w]=elePos[q]+w-q;
                        }
                        break;
                    }
                }
                if(notDone){col=0;row++;}
                int total = sum(B);
                appendIfUnique(total,nullHeader);
                nullHeader = false;
            }
            else
            {
                col++;
            }
       }        
                int result = 0;
                for(node *pnode = head; pnode != current->next; pnode=pnode->next)
                    result = result + pnode->data;
                cout << result << endl; 
}


int sum(int A[])
{
    int total = 0;
    for(int i=0; i<SUBSET; i++)
    {
        total = total + A[i];
    }
    return total;
}

bool search(node* head, int sum)
{
    bool exists = false;
    node* pNode = head;
    while(pNode != NULL)
        {
            if(pNode->data == sum)  
            {
                exists = true;
                break;
            }
            pNode = pNode->next;
        }
    return exists;
}

void appendIfUnique(int total, bool nullHeader)
{
    if(nullHeader){head = NULL;}
                if(!search(head,total))
                {
                    node *temp;
                    /*temp=(node *) malloc(sizeof(node));*/
                    temp = new node();
                    temp->data = total;
                    temp->next = NULL;
                    if(head == NULL)
                    {
                        head = current = temp;
                    }
                    else
                    {
                        current->next = temp;
                        current = temp;
                    }
                }           
}
#包括“stdafx.h”
#包括
#包括
#包括
#定义集合100
#定义子集50
类节点
{
公众:
int数据;
节点*下一步;
};
使用名称空间std;
整数和(整数A[子集]);
void combCalc(int S[SET]);
布尔搜索(节点*头部,整数和);
void appendIfUnique(整数总计,bool空标头);
int _tmain(int argc,_TCHAR*argv[]
{
cout一些注意事项:

  • 断点(在我的系统中:cygwin g++)设置为=18(17个工程)
  • 问题是由于太多的递归[在调试器中运行它]您对
    combCalc
    的调用太多(在我的例子中,它在32K次调用后死亡)
  • 正如评论中所指出的,您可能应该重新考虑您的算法。同时,一个简单的修改是删除递归(因为它甚至不是一个正确的递归):

    int-tmain(int-argc,_-TCHAR*argv[]
    {
    
    cout似乎有点像数论,而不是蛮力可能会有所帮助…?堆栈溢出表明您使用了太多的堆栈空间。尝试动态分配一些堆栈分配的数组。现在,虽然这将解决眼前的问题,但您可能需要重新考虑算法。HMI无法帮助您,但认为这篇文章会更好我觉得静态变量很可疑。你有一个递归调用
    combCalc
    这取决于要获取的静态变量out@DavidRodríguez dribeas…堆栈溢出也表明堆栈太多。我按照你的建议修改了程序,在函数中使用while循环,而不是递归函数,现在没有任何异常。但是程序永远在运行。有人建议m吗e有更好的算法解决这个问题吗?谢谢!@user3651245您应该接受这个答案,因为它回答了您的问题。如果您现在有更好的问题,请务必提交一个新的问题。(编辑和编辑问题以完全改变它不是一件好事,因为人们已经花时间在它上面了)我如何接受这个答案?我是这个网站的新手。谢谢。@user3651245或一汽:
    int _tmain(int argc, _TCHAR* argv[])
    {
       cout << "Hello World" << endl; 
       int S [SET];
       for(int i=1; i<=SET; i++)
       {
           S[i-1]= (int)pow((float)i,2);
       }
       while(combCalc(S)) { } // <--- keep calling while combCalc is true
       cin.get();
       return 0;
    }
    
    bool combCalc(int S[]) // <--- MODIFY (check also the forward declaration)
    {
    
           ...
    
           if(notDone || col <(SUBSET-1))
           {
                ...
    
                return true; // <--- MODIFY return true... I need to keep calculating.
           }
           else
           {
                    int result = 0;
                    for(node *pnode = head; pnode != current->next; pnode=pnode->next)
                        result = result + pnode->data;
                    cout << result << endl;
                    return false; // <--- MODIFY return false.... we're done
           }
    }