I know it is a very basic question. I have read memory management from Wikipedia and couple of MSDN pages. But still I’m not sure how does below processing happens during a program execution.
When I declare a long string within my program
- Which memory compiler use to store the value? (I think it is RAM)
- How much memory spaces does it takes? (Considering it is a 8 bit machine and string like ‘I’m a novice in computer programming’)
- When I update the string value in run time how compiler knows in which memory address string value is present
- When I create a matrix then how individual values get stored into memory?
Please consider I’m using C# in .Net and here is my string s = ‘I’m a novice in computer programming’
Thanks in advance.
First part, the strings.
As many people have told you, there are many ways to represent a string. The most common thing to do is to store the string as an array. However, you need some extra information: the length of this array.
Each (procedural or imperative) language solves this problem differently.
There “pascal strings”, where the first few bytes of the array store the length of the string.
There are “C string or null-terminated strings”, where there are no additional bytes to store the length, but the last byte in the array has zero value.
In COM a hybrid approach is used, the BSTR. It uses 4 bytes at the beginning to store the string’s length and a two-byte marker (two zero bytes) at the end. It allows to pass the string to C procedures and at the same time to get the length quickly.
Functional languages are a whole different store.
Second part, the matrices.
To store a multidimensional (two-dimensional in your case) array one has to “linearize” it, i.e., turn it to the one-dimensional array to use the computer’s memory hardware.
So to store the
you allocate at least
bytes of memory to store the data. The access to A[i][j] is done by accessing the memory bytes at the address
or the address
These are two approaches employed by C and Fortran.
However, if you need to store some sparse matrix, there might be some other options. You could, for example, store triples (i,j,value) representing the non-zero elements of the matrix.