stl 映射,设置错误:内存在分配块的末尾被破坏

发布于 2024-12-09 03:51:47 字数 2103 浏览 1 评论 0原文

当我试图解决这个面试问题时:找到数组中连续元素的总和等于目标数字,所以我想出了以下代码。但是,我真的不明白为什么它有一些内存分配问题。这是完整代码的链接。当我尝试将 currSum 的第二个元素插入集合和映射时,它会出现一些错误消息,例如“内存破坏了分配块的末尾”。不是设置了,map动态分配用于插入吗?我真的不明白为什么它没有像我想象的那样工作。

我还将完整的代码粘贴在这里:

        #include <map>
        #include <set>
        #include <iostream>
        using namespace std;

        void print_array(int arr[], int start, int end)
        {
            for(int i=start;i<=end;i++)
                cout<<arr[i]<<" ";
            cout<<endl;
        }

        //given an array, find the continous sum of the array which is a target number
        void findTargetSum(int arr[], int target, int sizeArr)
        {
            map<int, int> valIdxMap;
            set<int> prevSet;
            int* currSum= new int(sizeArr+1);
            currSum[0]=0;   
            for(int i=1;i<=sizeArr;i++)
            {
                currSum[i]=currSum[i-1]+arr[i-1];
            }

            //now the problem is to find two elements i, j in array currSum,where currSum[j]-currSum[i]=target && j>i
            for(int i=0; i<=sizeArr;i++)
            {
                int tmp=currSum[i]-target;
                set<int>::iterator iter=prevSet.find(tmp);
                if (iter !=prevSet.end())
                {
                    cout<<"located one range of array elements with sum to"<<target<<endl;
                    int startIdx=valIdxMap[*iter];
                    print_array(arr,startIdx,i-1);
                }
                else
                {
                    prevSet.insert(currSum[i]);
                    valIdxMap.insert(make_pair(currSum[i],i));
                }
            }
            delete currSum;
        }

        void testfindTargetSum()
        {
            int tst_arr[]={2,4,5,-1,3,8};
            int target=11;
            findTargetSum(tst_arr,11,6);
        }

        int main()
        {
           testfindTargetSum();
           return 1;
        }

As I tried to solve this interview problem: find the sum of continuous element in an array which equals to a target number, so I come up with the following code. However, I really don't understand why it has some memory allocation problem. Here is the link to the full code. when I tried to insert the second element of currSum into the set and map, it will have some error message like "memory clobbered past end of allocated block". Isn't set, map dynamically allocated for insert? I really don't understand why it's not working as I think.

I also paste the full code here:

        #include <map>
        #include <set>
        #include <iostream>
        using namespace std;

        void print_array(int arr[], int start, int end)
        {
            for(int i=start;i<=end;i++)
                cout<<arr[i]<<" ";
            cout<<endl;
        }

        //given an array, find the continous sum of the array which is a target number
        void findTargetSum(int arr[], int target, int sizeArr)
        {
            map<int, int> valIdxMap;
            set<int> prevSet;
            int* currSum= new int(sizeArr+1);
            currSum[0]=0;   
            for(int i=1;i<=sizeArr;i++)
            {
                currSum[i]=currSum[i-1]+arr[i-1];
            }

            //now the problem is to find two elements i, j in array currSum,where currSum[j]-currSum[i]=target && j>i
            for(int i=0; i<=sizeArr;i++)
            {
                int tmp=currSum[i]-target;
                set<int>::iterator iter=prevSet.find(tmp);
                if (iter !=prevSet.end())
                {
                    cout<<"located one range of array elements with sum to"<<target<<endl;
                    int startIdx=valIdxMap[*iter];
                    print_array(arr,startIdx,i-1);
                }
                else
                {
                    prevSet.insert(currSum[i]);
                    valIdxMap.insert(make_pair(currSum[i],i));
                }
            }
            delete currSum;
        }

        void testfindTargetSum()
        {
            int tst_arr[]={2,4,5,-1,3,8};
            int target=11;
            findTargetSum(tst_arr,11,6);
        }

        int main()
        {
           testfindTargetSum();
           return 1;
        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

乖乖兔^ω^ 2024-12-16 03:51:47

错误出现在这一行中:

int* currSum= new int(sizeArr+1);

该行获取单个 int 并将其初始化为值 sizeArr+1。您可能的意思是:

int* currSum= new int[sizeArr+1];

这将获取一个 int 类型的 sizeArr+1 元素块。此外,您还必须将 delete currSum; 行更改为 delete [] currSum;

另一方面,我的建议是不要手动管理内存,而是使用标准容器,例如:

std::vector<int> currSum( sizeArr+1 );

基本上将是当前实现的就地替换,并且它将自动管理内存。

在实现时,我相信您实际上可以通过在迭代时累积三个变量中的开始索引、结束索引和值的总和,在 O(N) 中无需任何额外内存即可完成此操作。当累加值小于目标值时,增加结束索引并添加该值。当累计值高于目标值时,增加起始索引并按该量减少累计值。

The error is in this line:

int* currSum= new int(sizeArr+1);

That line acquires a single int and initializes it to the value sizeArr+1. You probably meant:

int* currSum= new int[sizeArr+1];

That will acquire a block of sizeArr+1 elements of type int. Additionally you will have to change the line delete currSum; to be delete [] currSum;.

My advice, on the other hand, would be not to manage memory manually but use a standard container, for example:

std::vector<int> currSum( sizeArr+1 );

Will basically be an in-place replacement for your current implementation and it will manage memory automatically.

As of the implementation, I believe that you can actually do it without any extra memory in O(N) by accumulating the start index, end index and sum of the values in three variables while iterating. While the accumulated value is smaller than the target, increment the end index and add the value. When the accumulated value grows higher than the target, increment the start index and decrement the accumulated value by that amount.

嗫嚅 2024-12-16 03:51:47

你写的

int* currSum= new int(sizeArr+1);

可能是你的意思

int* currSum= new int[sizeArr+1];

you wrote

int* currSum= new int(sizeArr+1);

you probably meant

int* currSum= new int[sizeArr+1];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文