Re: 2-dimensional arrays and functions
From: Micah Cowan (micah_at_cowan.name)
Date: 10/30/03
- Next message: Micah Cowan: "Re: denying inheritance"
- Previous message: Micah Cowan: "Re: 2-dimensional arrays and functions"
- In reply to: Jumbo: "Re: 2-dimensional arrays and functions"
- Next in thread: Jumbo: "Re: 2-dimensional arrays and functions"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Next message: Micah Cowan: "Re: denying inheritance"
- Previous message: Micah Cowan: "Re: 2-dimensional arrays and functions"
- In reply to: Jumbo: "Re: 2-dimensional arrays and functions"
- Next in thread: Jumbo: "Re: 2-dimensional arrays and functions"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|