any news on const/invariant?

Frank Benoit keinfarbton at googlemail.com
Tue Nov 27 02:01:43 PST 2007


Regan Heath schrieb:
> Walter Bright wrote:
>> Regan Heath wrote:
>>> void foo(char[] pbuffer)
>>> {
>>>   //assume this causes reallocation
>>>   //assume there is no more space in place
>>>   pbuffer ~= "a";
>>> }
>>>
>>> void main()
>>> {
>>>   char[] buffer = "test".dup;
>>>   foo(buffer);
>>>   buffer[0] = 'a' //crash
>>> }
>>>
>>> Right?
>>
>> No. foo() modifies its copy of pbuffer, which is not the same as buffer.
> 
> Yes, but the underlying memory is reallocated so buffer no longer points
> to valid memory, right?
> 
> Regan

You missed a semicolon, so it does not compile :)
Realloc does not destroy the original buffer. So your foo just make a
modified copy, that is not visible for the caller. buffer will stay
unchanged.



More information about the Digitalmars-d mailing list