Re: 2-dimensional arrays and functions

From: Micah Cowan (micah_at_cowan.name)
Date: 10/30/03


Date: 30 Oct 2003 12:09:54 -0800


"Jumbo" <(nospam)@12freeukisp.co.uk> writes:

> That is completely different from...
> (memory addresses machine specific obviously)
> int** foo(){
> int* j = new int;
> //address = 0x0012fde8, value = 0x00321160, dereferenced value =
> random/unassigned.
> *j = 5;
> //address = 0x0012fde8, value = 0x00321160, dereferenced value = 5;
> return &j;
> //returns memory address 0x0012fde8, which is a valid memory address, as it
> was created with the new keyword when the function returns and j is disposed
> of and the memory area persists.
> }

The fact that it still contains the same address value as it did
when it was valid does not imply that it is still valid.

Even if it did, what possible use would your example be? Because
once j no longer exists, then the value at address 0x0012fde8 may
not be 0x00321160 anymore, rendering the whole thing useless.

But even the act of evaluating the return value of foo() will
cause undefined behavior, as far as the Standard is concerned.

>
> I quote again..
> C++ library reference:
> Objects allocated with the new operator are not destroyed when the scope in
> which they are defined is exited.

This has absolutely nothing to do with anything. The address
being returned was not allocated by new.

> [snip]
> > returning the address of a local variable.
> > Bad idea.
>
> I didn't return the address of a local variable

Are you saying that pArray is not a local variable? If you had
done:

  return pArray;

You'd be correct (ignoring type discrepencies). But you returned

  return &pArray;

&pArray is a pointer value with the address of pArray, and pArray
is a local variable.

> Objects allocated with the new operator are not destroyed when the scope in
> which they are defined is exited.

Right. The memory you've allocated with new can now never be
deleted, since you have lost the address new returned.

> How many times do I have to repeat,
> It is NOT a local variable !

You're absolutely insane.

This thread is plonked. I have no fear of newbies believing your
crap on this particular subject.

-- 
Micah J. Cowan
micah@cowan.name


Relevant Pages

  • Re: 2-dimensional arrays and functions
    ... The system is free to free that memory area. ... So what if the pointer has gone out of scope. ... >> This returns the address of pArray, as pArray is a pointer it returns ... But still if we assume that your system uses a heap ...
    (alt.comp.lang.learn.c-cpp)
  • Help with memory leak
    ... I'm new to COM so this could be a silly question, but I have a memory leak somewhere, and I really can't find it, here's my code (I removed ... VARIANT varChild, *pArray = NULL; ... This other function leaks the same amount of memory, and of course I ALWAYS call SysFreeStringon bName after reading it. ...
    (microsoft.public.win32.programmer.ole)
  • Re: 2-dimensional arrays and functions
    ... > I know it doesn't return the address contained by pArray and I don't think I ... > You are stating that it returns a pointer but it doesn't. ... The system is free to free that memory area. ... C++ doesn't have a heap. ...
    (alt.comp.lang.learn.c-cpp)
  • Re: 2-dimensional arrays and functions
    ... > The pointer is gone but the address is returned correct. ... It doesn't return the address contained by pArray, ... >> the allocated memory. ...
    (alt.comp.lang.learn.c-cpp)