在C/C++中关于内存问题的总结

一、内存分配方式

1.从静态存储区域分配。内存在程序编译的过程中就已经分配好,这块内存在程序的整个运行期间都存在。例如:全局变量和static变量。

2.在栈上创建。在执行函数时,函数内局部变量的存储单元都可以在栈上创建,函数运行结束时,这些存储单元会被自动释放,栈内存分配运算内置于处理器的指令集中,效率很高,但分配的内存数量有限。

3.从堆上分配,又被称为动态内存分配。程序在运行过程中用malloc和new申请任意大小的内存,程序员自己负责在何时用free或者delete释放内存。内存的存在周期由我们自己决定,比较灵活,但同时出现的问题也比较多。

二、经常会出现的内存问题以及问题分析和解决方法

1.内存重复释放(一般在出现double free时基本上都是这个原因)

*关于double free会在下篇博客中讲解

2.内存泄漏。申请的内存忘了释放。

3.内存越界使用

4.内存未分配成功确使用了它

5.内存分配成功却没有初始化就使用了内存

6.使用了无效指针

(1)已经释放对象,却继续操作改指针所指的对象

    a.程序当中的对象调用关系过于复杂,是在难以搞清哪个对象是否已经释放了内存,从根本上解决对象管理混乱的情况。

    b.函数的return语句写错了,注意不要返回指向“栈内存”的指针或者引用。

    c.使用free或者delete释放之后,没有将其置空,导致产生野指针。

(2)多线程中某一动态分配的对象同时被两个线程使用,一个线程释放了该对象,另一个线程却继续对该对象进行操作

三、使用规则

1.在malloc或者new之后应立即检查是否将其置为空,防止其使用指针值为NULL的空间。

2.不要忘记为指针和数组赋初始值,防止将未被初始化的内存作为右值使用

3.避免数组或者指针的下标越界,要特别注意多一,少一操作

4.动态内存的申请与释放必须配对使用,避免出现内存泄漏

5.用free或者delete之后要立即将其置为空,防止出现“野指针”

发布了49 篇原创文章 · 获赞 119 · 访问量 15万+
展开阅读全文

c++内存问题!!!!!!!

05-27

#include <iostream> #include <string> #include <vector> using namespace std; int main() { string a, b; int i, x, y; vector<string> v1, v2; while (cin >> a >> b) { for (i = 0; i < v1.size(); i++) { if (v1[i] == a) { x = i; break; } } if (i == v1.size()) x = i; for (i = 0; i < v2.size(); i++) { if (v2[i] == b) { y = i; break; } } if (i == v2.size()) y = i; if (x == v1.size() && y == v2.size()) { cout << b << " registered." << endl; v1.push_back(a); v2.push_back(b); } else if (v1[y] == a && v2[x] == b) { cout << b << " signed in." << endl; } else if (x != v1.size() && y == v2.size()) { cout << "GOTCHA: MAC address already occupied by "; cout << v2[x] << "." << endl; } else { cout << "GOTCHA: " << b << " MAC address not consistent." << endl; } } return 0; } Invalid read of size 8 | at 0x3F8CC9C0D0: std::string::size() const (in /usr/lib64/libstdc++.so.6.0.13) | by 0x40175D: __gnu_cxx::__enable_if<std::__is_char<char>::__value, bool>::__type std::operator==<char>(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) (basic_string.h:2243) | by 0x4012B5: main (main.cpp:30) | Address 0x4c29498 is 8 bytes before a block of size 8 alloc'd | at 0x4A075BC: operator new(unsigned long) (vg_replace_malloc.c:298) | by 0x402184: __gnu_cxx::new_allocator<std::string>::allocate(unsigned long, void const*) (new_allocator.h:89) | by 0x401F80: std::_Vector_base<std::string, std::allocator<std::string> >::_M_allocate(unsigned long) (stl_vector.h:140) | by 0x401BBB: std::vector<std::string, std::allocator<std::string> >::_M_insert_aux(__gnu_cxx::__normal_iterator<std::string*, std::vector<std::string, std::allocator<std::string> > >, std::string const&) (vector.tcc:322) | by 0x401877: std::vector<std::string, std::allocator<std::string> >::push_back(std::string const&) (stl_vector.h:741) | by 0x40122E: main (main.cpp:27) | |Invalid read of size 8 | at 0x3F8CC9C0D3: std::string::size() const (in /usr/lib64/libstdc++.so.6.0.13) | by 0x40175D: __gnu_cxx::__enable_if<std::__is_char<char>::__value, bool>::__type std::operator==<char>(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) (basic_string.h:2243) | by 0x4012B5: main (main.cpp:30) | Address 0xffffffffffffffe8 is not stack'd, malloc'd or (recently) free'd | | |Process terminating with default action of signal 11 (SIGSEGV) | Access not within mapped region at address 0xFFFFFFFFFFFFFFE8 | at 0x3F8CC9C0D3: std::string::size() const (in /usr/lib64/libstdc++.so.6.0.13) | by 0x40175D: __gnu_cxx::__enable_if<std::__is_char<char>::__value, bool>::__type std::operator==<char>(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) (basic_string.h:2243) | by 0x4012B5: main (main.cpp:30) | If you believe this happened as a result of a stack | overflow in your program's main thread (unlikely but | possible), you can try to increase the size of the | main thread stack using the --main-stacksize= flag. | The main thread stack size used in this run was 10485760. |66 bytes in 2 blocks are possibly lost in loss record 3 of 4 | at 0x4A075BC: operator new(unsigned long) (vg_replace_malloc.c:298) | by 0x3F8CC9C3C8: std::string::_Rep::_S_create(unsigned long, unsigned long, std::allocator<char> const&) (in /usr/lib64/libstdc++.so.6.0.13) | by 0x3F8CC9D19A: std::string::_Rep::_M_clone(std::allocator<char> const&, unsigned long) (in /usr/lib64/libstdc++.so.6.0.13) | by 0x3F8CC9D5EB: std::string::reserve(unsigned long) (in /usr/lib64/libstdc++.so.6.0.13) | by 0x3F8CC7B05A: std::basic_istream<char, std::char_traits<char> >& std::operator>><char, std::char_traits<char>, std::allocator<char> >(std::basic_istream<char, std::char_traits<char> >&, std::basic_string<char, std::char_traits<char>, std::allocator<char> >&) (in /usr/lib64/libstdc++.so.6.0.13) | by 0x401431: main (main.cpp:8) | |114 bytes in 2 blocks are possibly lost in loss record 4 of 4 | at 0x4A075BC: operator new(unsigned long) (vg_replace_malloc.c:298) | by 0x3F8CC9C3C8: std::string::_Rep::_S_create(unsigned long, unsigned long, std::allocator<char> const&) (in /usr/lib64/libstdc++.so.6.0.13) | by 0x3F8CC9D19A: std::string::_Rep::_M_clone(std::allocator<char> const&, unsigned long) (in /usr/lib64/libstdc++.so.6.0.13) | by 0x3F8CC9D5EB: std::string::reserve(unsigned long) (in /usr/lib64/libstdc++.so.6.0.13) | by 0x3F8CC7B05A: std::basic_istream<char, std::char_traits<char> >& std::operator>><char, std::char_traits<char>, std::allocator<char> >(std::basic_istream<char, std::char_traits<char> >&, std::basic_string<char, std::char_traits<char>, std::allocator<char> >&) (in /usr/lib64/libstdc++.so.6.0.13) | by 0x40141F: main (main.cpp:8) 怎么破! 求大神 问答

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 编程工作室 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览