Thread: Good site to learn about Prog. language concept?

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    16

    Good site to learn about Prog. language concept?

    Hi there, I was wondering what's a good CS site for tutorial to learn about the BASICS of "dynamic allocation" "garbage collection" "life-time varible" and "run-time stack" "heap" and "memory managment"? Or, would someone give me an explanation of what what these are? The CS book that I'm reading for my class just seems a little too confusing and I did try to search on the web for some good sites but most weren't that good. Thanks.

  2. #2
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,823
    Here?

  3. #3
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    If you define a variable in your code like...

    int Variable;

    ... the compiler creates space for that variable at compile time, and when the program is run, the variable is created on the stack. This is non-dynamic static allocation.

    If you create a pointer to a variable...

    int *pVariable;

    ... the compiler creates space for the pointer, but not for the variable itself. When the program is run, again, the pointer is created on the stack. Now you have the pointer, you can create a variable that the pointer will point to with the "new" statement...

    pVariable = new int;

    ... that is dynamic allocation, and the new variable is created on the heap not the stack.

    When you manually allocate memory, (i.e. dynamic allocation), you have to manually deallocate the memory...

    delete pVariable;

    ... you do not need to do that with static allocation.

    Do you understand that?
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  4. #4
    Registered User
    Join Date
    Mar 2003
    Posts
    16
    Thanks for answering my questions.

    "that is dynamic allocation, and the new variable is created on the heap not the stack."

    I'm still unclear on what you mean by "heap"?

    "When you manually allocate memory, (i.e. dynamic allocation), you have to manually deallocate the memory..."

    Is this an example of manually allocating memory:
    int*array = malloc(int*(sizeof(...)) (something like that)? So, then, how do I manually deallocate the memory?

  5. #5
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    >Is this an example of manually allocating memory?

    He actually gave an example of it in C++, using new and delete.

    In C it would be:
    Code:
    int *pVariable;
    pVariable = malloc( sizeof( int ) );
    then to de-allocate it, you would use
    Code:
    free( pVariable );
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  6. #6
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    I moved this thread to the Tech board as it is not really in the same class as the junk on GD.

    Yes, new/delete are the C++ way of allocating and deallocating memory. The older C way was with malloc, (or one of the variants thereof), and free. If you are using C then use these, if not use the other. There are several advantages to new/delete, for example, XSquared code snippets are for example only, the malloc() call would not compile on most systems because malloc() returns a void pointer which has to be cast to the variable type. new already "knows" how big things are, you don't need to sizeof(). Once you get into using classes, malloc() does not call the constructor of a dynamically created class, new does - it goes on...

    heap is part of memory. In a modern NT cored system, all processes have a 4GB virtual address space. Simplistically, this is divided into the memory that your actual code is sitting in, the stack where your statically defined variables etc. are located, and the rest, the heap. Note I said simplistically, it is a little more complicated than that, it has not always been like that, and on some systems is probably not like that. The code segment tends to be pretty much fixed size, the stack starts out the same size every time, but every time you, for example, call a function, there is data which is "pushed" onto the stack making it larger, and when the routine returns, things are "popped" off the stack. A poorly written program, (often peoples first attempts at recursive programming for example!), can use the entire virtual address range as stack, when that happens you have no heap.

    Without knowing the Operating System you are targetting or the language, this kind of debate is necessarily general.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I need a good site
    By rneto in forum C Programming
    Replies: 2
    Last Post: 05-21-2003, 04:35 PM
  2. any good resource site
    By kas2002 in forum C++ Programming
    Replies: 1
    Last Post: 06-04-2002, 05:38 PM
  3. Anyone have a good site to get avatars at?
    By Malcar Morab in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 05-05-2002, 12:28 AM