Re: "delete" causes prog to crash!



Hi Bob!
OKAY, I now understand... So if we are to use the delete statement, we must
use it to a pointer that points to the heap.

However if I add in the line:

> pTempVal = alarmCode;

I get the following error:

c:\DATASOURCE_EXTRA\Programming\VC++CodeTesting\Robert2_CodeTesting_HeapPointers_MSQ\Robert2_CodeTesting_HeapPointers.cpp(41):
error C2440: '=' : cannot convert from 'int' to 'int *'

and isn't a pointer supposed to hold the address of some data location. In
the line of code ( pTempVal = alarmCode;) we are trying to assign the value
of a variable to a pointer????

Why would we even think of doing this?

Robert


"Bob Milton" wrote:

> Robby,
> The problem is the statement:
>
> pTempVal = &alarmCode;
>
> This resets the address in pTempVal to the address of a stack variable.
> These cannot be deleted! If you just do
>
> pTempVal = alarmCode;
>
> Then your heap variable will contain the same value as the stack variable,
> and can be deleted.
>
> Bob Milton
>
> "Robby" <Robby@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
> news:59EDA6D8-CA8A-4ECE-98A2-4018F935C984@xxxxxxxxxxxxxxxx
> > Hi Victor,
> >
> > I just used one "new" statement in my code at define time, and so why the
> > crash! here is the whole code:
> >
> > #include <iostream>
> > using namespace std;
> >
> > class codes
> > {
> > public:
> > codes();
> > ~codes();
> > void progCode(int n1);
> > int returnCode() const;
> >
> > private:
> > int theCode;
> > };
> >
> > codes::codes():
> > theCode(9999)
> > {}
> >
> > codes::~codes()
> > {}
> >
> > void codes::progCode(int n1)
> > {
> > theCode = n1;
> > }
> >
> > int codes::returnCode() const
> > {
> > return theCode;
> > }
> >
> > int main()
> > {
> > int alarmCode;
> > int * pTempVal = new int;
> >
> > codes myCodes;
> > alarmCode = myCodes.returnCode();
> > pTempVal = &alarmCode;
> > cout << "Current alarm code is:" << alarmCode << "\n" << "******:" <<
> > *pTempVal ;
> > cin >> alarmCode;
> > myCodes.progCode(alarmCode);
> > alarmCode = myCodes.returnCode();
> > cout << "Current alarm code is now:" << alarmCode << "\n"
> > << "******:" << *pTempVal ;
> > cin >> alarmCode;
> > delete pTempVal;
> > return 0;
> > }
> >
> > --
> > Best regards
> > Robert
> >
> >
> > "Victor Bazarov" wrote:
> >
> >> Robby wrote:
> >> > I have declared a pointer on the heap (freeStoreMemory) as:
> >>
> >> No, you have declared a pointer and initialised it with the address of
> >> an int from the heap.
> >>
> >> > int * pTempVal = new int;
> >> >
> >> > I assign the pointer an address,
> >>
> >> Where? How? Why? It already has a value -- the address of an integer
> >> you allocated on the heap.
> >>
> >> > then I dereference it and all is fine!
> >> > However, in the book I am reading, they strongly suggest to delete your
> >> > pointer so you free up the memory.
> >> >
> >> > however when the program gets to the following line, the program
> >> > crashes!
> >> >
> >> > delete pTempVal;
> >> >
> >> > Here is the error:
> >> >
> >> > Debug assertion failed.....
> >> > _BLOCK_TYPE_IS_VALID(phead->nBlockUse)....
> >> > See C++ documentation....
> >> >
> >> > Why does it react this way?
> >>
> >> Because if you assign another value to the pointer you first obtained
> >> from 'new', you (a) lose the previous pointer and (b) probably attempt
> >> to 'delete' what was never allocated using 'new' in the first place.
> >>
> >> > And the program compiles without errors or
> >> > warnings!
> >>
> >> Undefined behaviour (like freeing memory you didn't obtain from 'new')
> >> never shows up at compile-time.
> >>
> >> V
> >>
>
>
>
.



Relevant Pages

  • Re: "delete" causes prog to crash!
    ... > class codes ... > int returnCode() const; ... > int alarmCode; ... >>> I assign the pointer an address, ...
    (microsoft.public.vc.language)
  • Re: "delete" causes prog to crash!
    ... int codes::returnCodeconst ... >> I have declared a pointer on the heap as: ... > an int from the heap. ...
    (microsoft.public.vc.language)
  • Re: "delete" causes prog to crash!
    ... an int from the heap. ... you allocated on the heap. ... > then I dereference it and all is fine! ... pointer so you free up the memory. ...
    (microsoft.public.vc.language)
  • Re: Passing an object pointer to function
    ... > pointer to an object on the heap using the following line: ... > and then passing only the pointer of the object to my function: ... // not a pointer to an int ... Prev by Date: ...
    (microsoft.public.vc.language)
  • heap question
    ... If I create a space in the heap as the following, since the return pointer ... int* f{ ... int* ptr; ...
    (alt.comp.lang.learn.c-cpp)