Thread: help with memory

  1. #1
    Unregistered
    Guest

    help with memory

    I am new to programming and have a couple of questions.

    Is there a place (a link) which would help me to understand memory and how it is used?

    For example, what is a stack and bin, etc...

    Furthermore, when using C style strings, if you initialize 3 char arrays in a row with max length 16, how will the memory be allocated.

    What I mean is does string1 get the lowest memory address or highest of the three.

    The reason I ask is because if the second or middle string is changed during the program to something larger than size 16, I know it will over write some of the memory for what ever variable is next to it. So if you over write string 2 will it destroy string 3 or string 1??

    Any help would be appreciated. I just hope you understand what I mean.

  2. #2
    Unregistered
    Guest
    you may be trying to get too technical for practical purposes here. You can think of information as a house. To remember where the house is you can give it an address that has been selected from a map. If the size of the house is too big for the address given, you look for a new address until you find one that is big enough. Then you you want to see what's in the house you just go the address.

    In general a stack is a collection of items wherein your access to the items is last one in, first one out, just like if you were to use a stack of plates. The compiler creates a segment of memory to hold all the variables and functions used in your program and this memory is called the stack or static memory. The size of the stack is something you usually don't get too excited about. If you overflow the stack all the time, then you can use dynamic memory, which is limited only by the amount of RAM available on the computer you are using.

    An array is a variable that has enough memory to hold a given number of variables of a given type. The memory for each element of a given array is gauranteed to be sequential, but if you declare three arrays, even if you declare them "all at once", you are not gauranteed that the memory segment for array2 will be sequential to the memory segment for array1, etc. It may be, but don't count on it.

    When I use the term bin it is another name for a container to hold variables and may be a list, an array, a stack, a vector, a queue, or whatever; just so it can hold the array type used.


    The size of a given array cannot be changed during a given program. You can declare a larger array, copy the old array into the new array, delete the old array, and give the new array the same name as the old array so it looks like you expanded the old array, but you didn't. This may be done for you if you use expandable containers like the vector or string class in STL or you can do it yourself.

  3. #3
    Unregistered
    Guest
    Thanks for the reply that helps.

    But can I show you some code and the output, and then maybe you can help me to understand it better.

    code:

    #define MAXCSTRLEN 16

    #include <iostream.h>
    #include <string> // include for C++ standard string class
    #include <string.h>
    #include <stdlib.h>
    #include <stdio.h>

    int main()
    {
    // declare some C strings
    char cString1[MAXCSTRLEN], cString2[MAXCSTRLEN], cString3[MAXCSTRLEN];

    // declare some C++ strings
    string string1 = "Nutcracker",
    string2 = "Sleeping Beauty",
    string3 = "Sleeping Ugly";

    strcpy(cString1, "Nutcracker");
    strcpy(cString2, "Sleeping Beauty");
    strcpy(cString3, "Sleeping Ugly");

    cout << "** Initial values" << endl;
    cout << "C++ strings: " << string1 << " " << string2
    << " " << string3 << endl;
    cout << "C strings: " << cString1 << " " << cString2
    << " " << cString3 << endl;

    cout << "** Values after assigning long string to 2nd string" << endl;
    string2 = "123456789012345678901234567890";
    strcpy(cString2, "123456789012345678901234567890");
    cout << "C++ strings: " << string1 << " " << string2
    << " " << string3 << endl;
    cout << "C strings: " << cString1 << " " << cString2
    << " " << cString3 << endl;

    printf ("address string1: %p \n", cString1);
    printf ("address string1: %p \n", cString2);
    printf ("address string1: %p \n", cString3);



    system("PAUSE");
    return 0;
    }


    OUTPUT:

    ** Initial values
    C++ strings: Nutcracker Sleeping Beauty Sleeping Ugly
    C strings: Nutcracker Sleeping Beauty Sleeping Ugly
    ** Values after assigning long string to 2nd string
    C++ strings: Nutcracker 123456789012345678901234567890 Sleeping Ugly
    C strings: 78901234567890 123456789012345678901234567890 Sleeping Ugly
    address string1: 0241FF50
    address string1: 0241FF40
    address string1: 0241FF30

    You can see how cString1 gets overrun

    is it because the end of string or null character gets overridden?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 01-13-2008, 02:14 AM
  2. Question regarding Memory Leak
    By clegs in forum C++ Programming
    Replies: 29
    Last Post: 12-07-2007, 01:57 AM
  3. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  4. Shared Memory - shmget questions
    By hendler in forum C Programming
    Replies: 1
    Last Post: 11-29-2005, 02:15 AM
  5. What's the best memory (RAM) type?
    By Unregistered in forum A Brief History of Cprogramming.com
    Replies: 17
    Last Post: 12-15-2001, 12:37 AM