两例栈溢出导致的core dump

本文通过两个示例展示了在C++与C中进行大量内存分配时可能遇到的问题。在C++示例中,尝试创建包含大量字符的字符串导致了`std::length_error`异常;而在C示例中,尝试为数组分配过大的内存则触发了段错误。这两个例子突显了资源管理和内存分配的重要性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

       main.cpp:

#include <iostream>
#include <string>
using namespace std;

int main()
{
	unsigned int m = (1<<31);
	string s(m, 'a');
	string s1(m, 'a');	
	string s2(m, 'a');	
	return 0;
}
       main.c:

#include <stdio.h>
int main()
{
	int a[1024*1024*100] = {0};
	return 0;
}
       结果:

taoge@localhost Desktop> g++ main.cpp
taoge@localhost Desktop> ./a.out 
terminate called after throwing an instance of 'std::length_error'
  what():  basic_string::_S_create
Aborted (core dumped)
taoge@localhost Desktop> gcc main.c
taoge@localhost Desktop> ./a.out 
Segmentation fault (core dumped)
taoge@localhost Desktop>





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值