On Monday, November 08, 2010 11:22:36 Ali Çehreli wrote: > Steven Schveighoffer wrote: > > On Mon, 08 Nov 2010 13:35:38 -0500, Daniel Gibson > > > >> If you pass a dynamic array to a function and chance it's size within > >> the function, you have undefined behaviour - you never know if it will > >> affect the original array (from the calling function) or not. > > > > Not exactly. If you happen to change its size *and* change the original > > data afterwards, then it's somewhat undefined > > Let's also note that appending to the array qualifies as "change its > size *and* change the original data afterwards." We cannot be sure > whether appending affects the passed-in array.
Yes you can. It _never_ alters the array which was passed in. Sure, it _could_ alter the memory just off the end of the passed in array if no arrays refer to that memory, but that doesn't cause any problems. It doesn't alter the original array at all. It just means that when you resize that array, it may have to reallocate whereas before it might have been able to resize in place. And if the array in the called function reallocates instead of resizing in place, then the original array would either have been forced to reallocate anyway or it may be able to resize in place depending on how much you try and resize it and whether any other arrays refer to the memory passed its end. In _no_ case does appending or concatenating to an array alter any other arrays, even if they point to the same memory. They may or may not end up pointing to the same memory afterwards (depending on whether a reallocation takes place), but you never alter any other arrays. - Jonathan M Davis