|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
euhm.... memory usage minimum ?
I don't know if that is in the good place. Cause there is no General discussion or anything but.....
what do you think would take less memory usage ? a table with 2 dimension (either same or different variables doesn't matter) or 2 table of 1 dimension ? I was just wondering that. I know that computers of modern age are fast and can take a lot of things in memory, it's not like in the old times. But still I love to reduce speed and memory usage at it's maximum. So what do you guys think ? (if it's in the wrong place, delete or move it please)
__________________
|
|
#2
|
||||
|
||||
|
Depends on how/what you use the arrays for. C programmers can relate to this example in particular. Consider an array of strings. In C, strings are considered as arrays of characters. So an array of strings is really an array of arrays of characters (i.e.) a 2 dimensional array. Let's say we want to store 10 strings in an array. Now, there are two ways to store such an array. The first method is to declare an array like this:
char array[10][200]; This will declare a square (or rectangular) array, where there are 10 elements, each of which can store up to 200 characters. The problem with this type of array is that there may be lots of bytes wasted, especially if each string is not 200 characters long. Another way is to declare a ragged array. You would declare an array of 10 character pointers like this: char *array[10]; Then, as you read each string, you'd allocate only enough memory to store each actual string. Then you'd assign each string array to an element of the array (something like this). char *ptr; ptr = strdup("small string"); array[0] = ptr; ptr = strdup("This is a longer string"); array[1] = ptr; ptr = strdup("This is an even longer string"); array[2] = ptr; ... and so on. This ragged array is a little more complex, but is more efficient because each string (i.e.) char array is of a different length, thus you don't have extra wasted memory. Now, if all elements were of equal length, then you could go with a simple rectangular array. However, if the elements are of varying length and you want to save a lot of memory, ragged arrays are probably the way to go.
__________________
Up the Irons What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home. "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest Down with Sharon Osbourne Puzzle of the Month solved by Keath and KevinADC, superior perl programmers of the month |
|
#3
|
|||
|
|||
|
Thank you for bringing me up to that !
I didn't tought of that and it's stupid I used that in the past when I was coding in C ! damn I am now too old should I say ![]() |
![]() |
| Viewing: Dev Shed Forums > Programming Languages - More > Software Design > euhm.... memory usage minimum ? |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|