Array addresses

Discussion in 'Programmer's Corner' started by mentaaal, Sep 2, 2008.

  1. mentaaal

    Thread Starter Senior Member

    Oct 17, 2005
    Hey guys, i've been reading up on arrays and the book i am reading explains that the array's address is really the address of the first element and when you want to access any other elements of the array, the computer works out the location of the element based upon what type of array it is (int,float etc) and multiplies this by the element number to work out the address of the requuired element.

    I understand this and it makes sence for contiguous memory. But what happens when a large array is being dealt with and the memory it takes up is not contiguous? How does the computer know how to deal with this?
  2. Mark44

    Well-Known Member

    Nov 26, 2007
    A C or C++ array is stored in a contiguous block of memory. Period. If the program you're writing needs more storage than is available in a single, contiguous block, you need to use something other than an array.

    One alternative that gets around the contiguous memory requirement is a two dimension array, which is an array, each member of which is an array. Presumably the range of values of the first index would be a relatively small number, i.e., a[0], a[1], ..., a[m]. These elements would be contiguous in memory, but instead of holding an int or float or whatever, they would hold the address of a block of memory. IOW, a[0], a[1], and so on would be pointers. Each block of memory one of these pointers points to would be a contiguous block, but the set of these memory blocks wouldn't necessarily be contiguous. Each of the memory blocks would be allocated from the heap (AKA free store or store) using a call to malloc() or related C runtime function (if you're writing a C program) or to new if you're writing a C++ program.

    When your program is finished you need to deallocate the memory using free() (C program) or dispose (C++ program).

    Assuming someone has done the work of allocating the m + 1 blocks of memory, you could store a value in this way:

    a[1][3] = 2.6;

    If your storage requirements are even larger than the technique above can handle, you'll probably need to go to disk storage, with one possibility being storing your data on disk and swapping it in and out of memory. There are other techniques, but I don't have any experience with them.

    Hope that helps.
  3. mentaaal

    Thread Starter Senior Member

    Oct 17, 2005
    thanks alot mark was just curious because the book i am reading says that arrays are stored in contiguous arrays but never accounted for the situation of not enough memory.

    Another question for you if you dont mind:

    I was going to post this on another thread but anyway:
    Why is it that when I make the pointer *ptr below point to string[2], the value of ptr is "diamonds" and not some memory address?
    I have included the lines beneath but they are only to illustrate my point in that the value of ptr2 is a memory address. A copy of the output text is below the code.

    Code ( (Unknown Language)):
    1. #include <iostream>
    2. using namespace std;
    3. void copy(char *,char[]);
    4. int main()
    5. {
    6.     char *string[4] = {"spades","heartts","diamonds","clubs"};
    7.     char *ptr = string[2];
    8.     cout << "the value of *ptr is: " << *ptr << endl;;
    9.     cout << "the value of ptr is: " << ptr << endl;
    10.     int test[4] = {1,2,3,4};
    11.     int *ptr2 = &test[2];
    12.     cout << "the value of *ptr2 is : " <<  *ptr2 << endl;
    13.     cout << "the value of ptr2 is : " << ptr2 << endl;
    16.     return 0;
    17. }
    the value of *ptr is: d
    the value of ptr is: diamonds
    the value of *ptr2 is : 3
    the value of ptr2 is : 0012FF00
    Press any key to continue . . .
  4. Mark44

    Well-Known Member

    Nov 26, 2007
    In answer to your question, your code sets ptr to element 2 of the string array, which is the string "diamonds". The value of a string literal, unlike those for other types of literals, is an address.

    Much of that is hidden from you when you use cout to do your output. As you can see, when you sent ptr2 to cout, what was displayed was an address in memory. When you sent ptr to cout, however, the compiler thought about it a while, and determined that since ptr was also an address, and the address of a character, it would give you the entire string (IOW, up until it found the null character that marks the end of the string).

    All of this would be a little more obvious if you had used the C-style printf function to do output. You can control how the output is displayed by the use of what are called conversion specifiers in the format string. For example,
    Code ( (Unknown Language)):
    2. printf("%s", ptr);   // print diamonds
    3. printf("%c", *ptr); // print d
    4. printf("%p", ptr);   // print an address similar to 0x12FF00
  5. beenthere

    Retired Moderator

    Apr 20, 2004
    As an area of further study, try looking into "linked lists". Things like tables of pointers to offset values that do let you get to the next item in the array.
  6. Papabravo


    Feb 24, 2006
    This statement is flat our wrong on every modern processing platform with memory management. Virtual memory management allows an array to occupy a continuous string of virtual memory addresses. Different parts of this virtual address space may reside in the cache, main memory or secondary stroage all at the same time and not necessarily in contiguous blocks. The illusion of contiguous physical memory is just an illustion.
  7. mentaaal

    Thread Starter Senior Member

    Oct 17, 2005
    Ok, i understand what you're saying (i think!), the main difference being that the first array is a char array whereas the 2nd array was an int array.

    But what i am still unsure of is WHY when the address of a character array element is sent to cout like this:

    char test[] = {"hello"};
    cout << &test[2];

    it gives the array from test[2]. Why does it simply not just return a memory address?

    If, for some reason, jsut the address was required, how would that be found out?

  8. Ratch

    New Member

    Mar 20, 2007

    Something you should understand. On a computer with a sophisticated operating system, you have a memory management system also known as a dynamic allocator. A programmer is interested in logical addresses which are contiguous program addresses. These addresses are mapped into physical addresses by the operating system which manages where the physical addresses will be and are of little or no concern to the programmer. The CPU hardware assists in doing this by having special registers called by various names like paging registers, segment registers, offset registers, etc. The memory management usually offers virtual memory which will swap core memory out to a disk if things become too crowded or the programmers asks for a large amount of memory. All this memory management usually take place in the background without the programmers intervention.

    What does it give? One element from the array or the rest of the array starting from the third element? Anyway that is a C++ question which I am not expert at.

  9. mentaaal

    Thread Starter Senior Member

    Oct 17, 2005
    Hi ratch, thanks for the reply, yeah the dynamic allocator along with what Mark said earlier certainly explains that.

    In answer to your question the value of the pointer (without deferencing it) just returns the rest of the string, in this case llo.
  10. Mark44

    Well-Known Member

    Nov 26, 2007
    My answers to your questions are:
    1. &test[2] is a memory address, but just as before, cout knows that this is the address of a char and "thinks" that what you really want is the character at this address plus all the rest up to a terminating null character.
    2. If you want the address to be displayed as an address, I don't think it can be done using cout. If I'm wrong on this, I welcome being corrected. Instead of using cout for output, you can use printf() to display a character address either as a string or as an address, following the examples I gave earlier.
  11. mentaaal

    Thread Starter Senior Member

    Oct 17, 2005
    Yup, that'll keep me happy! Thanks again mark