Results 1 to 6 of 6

Thread: Join a string to an array

  1. #1

    Thread Starter
    Fanatic Member Toph's Avatar
    Join Date
    Oct 2014
    Posts
    655

    Join a string to an array

    Code:
    Module Module1
    
        Sub Main()
    
            Dim str As String = "It worked"
            Dim words() As String = {GetWords(), str}
    
            Console.ReadLine()
        End Sub
    
        Private Function GetWords() As String()
            Dim toReturn(5) As String
    
            toReturn(0) = "Hello"
            toReturn(1) = "My"
            toReturn(2) = "Name"
            toReturn(3) = "Is"
            toReturn(4) = "Toph"
            toReturn(5) = "Beifong"
    
            Return toReturn
        End Function
    End Module

    So what I want is even though the GetWords() returns an array with 6 elements in it, is there a way to do.

    Code:
    Dim str As String = "It worked"
    Dim words() As String = {GetWords(), str}
    So esentially adds str to the end of the array, I want to do this without using ReDim or using List(Of T)
    I wanted it to be resized automatically, same way
    Code:
    Dim words() As String = GetWords()
    or
    Dim words() As String = {"Hey", "Name", "is", "Toph"}
    is re sized automatically.

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    Re: Join a string to an array

    You can't do that because arrays can't be resized. You may think that you are resizing an array whey you use something like ReDim Preserve, but what you are actually doing is creating a new array of the new size and moving the existing values over into it. So, in all cases, you will not be resizing the existing array.

    That being the case, what is it you really want to do? I would use a List(of String), because that would be the most efficient, but you have explicitly ruled that out. Why is that?
    My usual boring signature: Nothing

  3. #3

    Thread Starter
    Fanatic Member Toph's Avatar
    Join Date
    Oct 2014
    Posts
    655

    Re: Join a string to an array

    I know how ReDim works . But yeah, I dunno, I just wanted to see if it were possible to do it without using List(Of T) or Array.Resize or ReDim. Just curious that's all.

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    Re: Join a string to an array

    Can't be done, due to immutability of arrays.

    Technically, it could be done in a lower level language, because the array would just be a consecutive series of bytes, and a pointer would iterate over those bytes until it reached the end of the array. If the array were located in memory such that the memory beyond the current end of the array were unused, you would be able to move back the current end of the array and insert something into the new space. That introduces several interesting problems, and the general description has been the source of a few interesting viruses and bugs over the years (mostly the 90s and earlier). These days, most OS don't allow that kind of a thing anyways.

    On the other hand, a List(of T) does very close to what you are describing. After all, if you have a List(of T) with 11 items in it, the actual underlying array is probably sized at around 16 items (some power of 2, anyways, but there is likely a minimum size). So, the memory is already there in the object, and adding a new item doesn't change it, it just does what you describe: Adds it to the end of the existing array. Of course, you can keep doing that until you add the 17th item, at which point the underlying array has run out of space and the List doubles the size of the array. So you aren't allocating new memory until you exceed the amount that the List already has available, and the List always doubles in size whenever it allocates new memory, so you will often be simply extending the list rather than re-allocating.
    My usual boring signature: Nothing

  5. #5

    Thread Starter
    Fanatic Member Toph's Avatar
    Join Date
    Oct 2014
    Posts
    655

    Re: Join a string to an array

    Right I see, I thought the List(Of T) behaved like a linked list? Wouldn't that be more efficient? The way you described it, it seems like it's wasting potential memory space... Why can't it just mimic the Linked list data type, but without actually showing the user the extra details like Pointer, Nodes etc which the actual LinkedList data type in VB.NET does if you get what I mean.

    Surely that would be a better way right? Or am I wrong..

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    Re: Join a string to an array

    It does behave like a linked list (just look at the Insert method, which is hard to do in an array), and it IS wasting potential memory space. Nobody cares about memory space anymore. Even back in the early 90s, you sacrificed memory to get imporved performance when it mattered. For example, games that used trig equations would build lookup tables for the cos, sin, and tan functions such that they could precompute the values for 360 degrees rather than needing to do so during the game. Another point that came out in those days was that allocating memory was expensive (in terms of time), so you wanted to allocate all your memory at one time, then manage it internally. There were plenty of gaming books (games were always pushing the envelope like few other applications) that talked about custom memory management, because the cost of allocating memory was so high. So, that was an area where a fair amount of research was done regarding optimization of CPU time, though with no regard to memory efficiency. The strategy that MS adopted is the one that came out of that research: Allocate a reasonable amount to start, if you use up that reasonable amount, then double the allocation. Naturally, this gets fairly extreme. If you start with 16 bytes, then once you add the 17th you reallocate to 32 bytes. When you then add the 33 byte you reallocate to 64, and so on. Such a series gets HUGE really fast. Pretty soon, you are at 1MB and when you add one more byte you allocate a whopping 2MB for that one extra byte. The cost of allocating one byte or 1MB is the same (as long as the system has the RAM to comply with the request), but you'd be wasting a whole lot of memory in that scenario. Or, more accurately, you'd be wasting a whole lot of memory if you really only needed one more byte. However, if you have already used up one million bytes, the odds are pretty good that you won't ONLY need one more byte, but will need many more bytes. So, the strategy will be wildly wasteful in some cases, but will be reasonable in most cases. In all cases, it will waste memory to conserve CPU cost.

    Everything in .NET works that way. You might create a dozen objects, then dispose them all, then create one more. You might think that the new object will be created in the memory that had been used for the first dozen, but that may or may not be true. If the app has plenty of memory, it won't bother trying to recover the memory used by the dozen objects. Instead, it will just allocate new memory for the new object, because that's faster. That can make it look like the app is growing in size steadily, when, in fact, what is growing in size is chunks of abandoned memory where the app has decided that getting new memory is cheaper in processing time than recovering the abandoned memory. The Garbage Collector is the process that recovers the abandoned memory, and can even move things around to make memory usage more efficient, but the GC is costly to run, so it only runs when it has to. As long as the app has plenty of unused memory to draw from, it will always choose to waste memory to save processor time.
    My usual boring signature: Nothing

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width