请在下面的问题上帮助我,并且没有关于内存损坏问题的先验知识 [英] please help me on below issue and have no prior knowledge on memory corrpution issue

查看:81
本文介绍了请在下面的问题上帮助我,并且没有关于内存损坏问题的先验知识的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在创建两个类似的类对象.

例如:

We are creating two similar class objects.

Ex:

Class A; // the class is having some pointer member variables.

A obj;
A obj1;



Obj:构造正确,成员变量指向有效的地址和值.
Obj1:在构造阶段,指向有效地址和值的成员变量一旦完成构造,成员变量就指向zunk值.


请帮助我如何进一步解决此问题.我对内存损坏没有任何先验知识.



Obj: is constructed properly and member variables are pointing to valid addresses and values.
Obj1: during construction stage the member variables pointing to valid addresses and values once complete the construction the member variables are pointing to zunk values.


Please help me how to proceed further on this issue. I have no prior knowledge on memory corruption.

推荐答案

这种事情的正常问题是您通过引用局部变量来创建指针-因为这些变量是在局部变量上创建的堆栈,离开函数后将不会维护它们.如果希望它们持久化,则需要使用new关键字或malloc函数在堆上创建它们.
The normal problem with this kind of thing is that you create the pointers by referencing local variables - since these are created on the stack, they are not maintained once you leave the function. If you want them to persist, the you need to create them on the heap, using the new keyword, or the malloc function.


这篇关于请在下面的问题上帮助我,并且没有关于内存损坏问题的先验知识的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆