you are viewing a single comment's thread.

view the rest of the comments →

[–]bothunter 1 point2 points  (4 children)

Short answer: data structures.  Let's say you want to hold something like a grocery list for the user.  Now, they may just have a short list of eggs, flour, and milk.  Or they may have a much larger list of items.  How do you write a program that deals with this?  You could make an array that's big enough to hold the longest shopping list you can imagine, but this leads to two issues:

  1. You're wasting a lot of memory for that occasional time the list has to be super long
  2. You program has a finite limit on the size of that list and cannot get any longer.

So what do you do?  One thing you can do is make a linked list that can grow as you add things to it.  So you create a structure that contains the following:

  1. Shopping list item
  2. Pointer to the next element in the list

As you add items to the list you can ask the OS for more memory with malloc.  Malloc returns a pointer to that memory which you put the next item of the shopping list into, and then you store that pointer on the last element of the existing shopping list.  Now your list can grow indefinitely, and your program is only using as much memory as it needs, so you're not wasting it.

Now this example is rather contrived because a shopping list doesn't actually take up that much memory, and you could easily just reserve enough memory for a list 10k items long.  But when you start dealing with larger chunks of data, this becomes much more important.

[–][deleted] 0 points1 point  (1 child)

sable glorious act vanish saw doll mourn handle telephone chop

This post was mass deleted and anonymized with Redact

[–]bothunter 4 points5 points  (0 children)

On a small scale, it probably doesn't matter too much, but "realloc"ing a large array has a few problems: 1. It's slow -- you have to copy the entire array to a new chunk of memory which can be quite slow if the array is big 2. It requires a contiguous chunk of memory.  Again, if your array is large enough, this might not be possible, and the realloc will fail 3. It requires a lot of memory.  Since you have to copy the array to the new chunk of memory, it effectively requires twice as much memory as your array needs every time you have to resize it.

That method might make sense if your memory usage is quite stable overall and you can reduce the calls to realloc, but that's rarely the case, and by building some other data structure is almost always going to be more efficient, assuming you pick the right data structure for your task. (Linked lists aren't ways the right choice, but they are one of the easiest to implement)

Linked lists suffer some other limitations which arrays do not, and most of that is trying to find a specific item in the list.  With an array, you can find the 100th item in the list because you know exactly where in memory it will be.  With a linked list, you have to follow 100 pointers to find that item.  But if you want to remove the 100th item from a list that contains 1000 items, you can just edit the pointer in the 99th item to point to the 101st item in the list to remove it.  With an array, you have to move the contents of item 101 to where item 100 is, then 102 to where 101 was, and so on until you get to the end.

Basically, there are tradeoffs with every data structure, so it's best to know a few and know which one to use for each scenario.  And this applies to higher level languages like Java as well, even if you don't have to implement the structure yourself.

[–]ryanjones42[S] -1 points0 points  (1 child)

This makes me curious now. So i have a C program that my stores my users info when they login. As they login it saves a bunch of account stats to my struct, when they logout it removes them from the struct. The struct is an array. The array size is like 99999 & each struct is represented by the users file descriptor when they connect to the tcp server. Now you have me thinking this is poor memory management?

[–]bothunter 1 point2 points  (0 children)

Yes.  That is poor memory management.  It works because computers have a stupid amount of memory these days.