## # Microsoft C++ 异常: std::bad_alloc，位于内存位置 0x00CDD8DC 处，我写的单调栈实现连续矩形最大面积的算法，报错如上，求大神指点

``````#include "pch.h"
#include <iostream>
#include<stdio.h>
#include<string.h>
typedef struct LNode{
int data;
struct LNode*next;
S = NULL;
}
while (S) {
p = S;
S = S->next;
delete p;
}
}
if (!S)
return false;
e = S->data;
return true;
}
void Push_L(LinkStack &S, int e) {
p = new LNode;
p->data = e;
p->next = S;
S = p;
}
bool Pop_L(LinkStack &S, int &e) {
if (!S)
return false;
p = S;
S = S->next;
e = p->data;
delete p;
return true;
}
if (S = NULL)
return true;
return false;
}
int main()
{
int n, b, c, max = 0, sum = 0, L, R, i, a[100005];
memset(a, 0, sizeof(a));
InitStack_L(S);
scanf_s("%d", &n);
for (i = 0; i < n; i++) {
scanf_s("%d", &a[i],1);
}
i = 0;
while (i < n) {
if (i = 0) {
Push_L(S, i);
i++;
continue;
}
GetTop_L(S,b);
if (a[i] > b) {
Push_L(S, i);
i++;
}
else if (a[i] = b) {
continue;
}
else {
Pop_L(S, c);
p = S;
L = p->next->data + 1;
R = i - 1;
sum = a[c] * (R - L + 1);
}
if (sum > max) {
max = sum;
}
}
while (!StackEmpty_L(S)) {
Pop_L(S, c);
if (StackEmpty_L(S))
L = 0;
else {
p = S;
L = p->next->data + 1;
}
R = i - 1;
sum = a[c] * (R - L + 1);
if (sum > max) {
max = sum;
}
}
DestroyStack_L(S);
printf("%d", max);
}
``````
``````
``````