Thread: What's the difference?

  1. #1
    Registered User Stonehambey's Avatar
    Join Date
    Jan 2008
    Location
    Kent, UK
    Posts
    118

    What's the difference?

    Hi everyone,

    An old maths teacher once said to me "You don't have to understand it, you just have to be able to do it". Which is a surprisingly useful motto and has worked for me even through university ^_^

    This question is sort of along those lines; in that I'm not really having any problems in this particular area with regards to my code not compiling/bugs in the program etc, but rather just interested in understanding why it must be done this particular way.

    C++ doesn't like stuff like this

    Code:
    long aFunction(long n)
    {
        char anArray[n];
    
    //some more code
    }
    because when declaring an array the size must be constant and not a number not known until the program is run. However you can get around this by using something like this

    Code:
    long aFunction(long n)
    {
        char* anArray;
        anArray = new char[n];
    
        //some more code
    
       delete[] anArray
    }
    Now I don't really understand what the big difference here is. The line "anArray = new char[n]" is simply requesting a block of n pieces of memory each the size of a char. So why can it do this at runtime and not create an array in the first block of code?

    I'm still new to the language so I'm sure there must be something going over m head here, but if someone could explain it to me I would be most grateful

    Kind Regards,

    Stonehambey

  2. #2
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    C++ has a MUCH better alternative to arrays -- the std::vector class, which can grow automatically, so you don't even need to know the size at compile time and don't need to worry about allocating & deleting memory...

  3. #3
    Registered User
    Join Date
    Oct 2004
    Posts
    151
    Because that's just the way the language is designed.

    C can have arrays like that, mind you.
    System: Debian Sid and FreeBSD 7.0. Both with GCC 4.3.

    Useful resources:
    comp.lang.c FAQ | C++ FQA Lite

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    But only C99, the newer version. C++ inherited the restriction from C89 and pre-standard C versions.

    The reason is that it is far simpler for the compiler to generate code for arrays if their size is always known at compile time. C has quite a number of things that were done mostly to make compilers simpler. Another one among these is that you have to declare all local variables at the start of a block. (Also lifted in C99.)
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Code:
    char anArray[n];
    This creates an array of n elements on the stack.

    Code:
    char* anArray = new char[n];
    This creates an array of n elements on the heap.

    That's the difference.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Not really, Elysia. The names stack and heap are really academic these days: they date from a decade or two back, when memory was scarce, and when primitive operating systems were designed to support no more than a few hundred kilobytes of total memory (as 1MB was considered to be more memory than any application would EVER need). When memory became a bit cheaper, those operating systems had to be tweaked to support larger amounts of memory (and, in fact, some machines had two distinct physical areas of memory, on physically different chips). On those systems, stack was the name given to the "low end" of memory (the memory the operating systems were designed to access) and the "high end" (memory that the operating systems were not designed to access, but special drivers would allow usage). The "low end" of memory was referred to be various names (eg "system memory", "stack") and the "high end" of memory was referred to by various names (eg "high memory", "extended memory", "special memory", "heap"). After a while, they were most commonly known as stack and heap. In practice, dynamically allocated memory (eg managed with malloc() in C) were able to be allocated from either heap or stack but, after a while it became more common to use the "stack" for program variables and "heap" for dynamically allocated memory.

    Modern 32 bit (and 64 bit) operating systems make no distinction between areas of memory, except in terms of what process is allowed to access what memory. So the terms "heap" and "stack" are irrelevant: they are the same physical memory.

    In any event, the differences are that the lifetime of dynamically allocated memory (eg char *AnArray = new char[n];) is explicitly managed by the programmer (eg destroyed with operator delete []). The lifetime of variables (eg char AnArray[n];) is managed by the compiler (in C or C++, the array will be destroyed at the end of the enclosing scope) or runtime support of the language, and is not controlled directly by the programmer.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I believe processor's have a stack implementation in hardware.
    It's true that both stack and heap are just placed somewhere in virtual memory, but as we know, that doesn't mean it all ends up in physical memory.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    But the stack and the heap still have different properties in a modern system where memory is uniform. For example, data stored on the stack gets "lost" when you leave the function where it was instantiated/created.

    As a consequence, heap allocated data needs to be freed at some point.

    Also, even thought stack and heap use the same type of memory, the stack is much more limited in space (even a 64-bit machine defaults the stack-size to a few megabytes), whilst the heap is normally only limited by the amount of virtual space allowed for the application (and amount of virtual space the OS can deal with), and even in 32-bit OS's this reaches into the gigabytes sizes.

    There is also a certain speed difference, as the heap-allocated space is referenced through a pointer, and although an optimizing compiler can reduce the overhead of getting to the data, it will still be some overhead compared to the stack allocated data that is available at a set offset on the stack.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Elysia View Post
    I believe processor's have a stack implementation in hardware.
    It's true that both stack and heap are just placed somewhere in virtual memory, but as we know, that doesn't mean it all ends up in physical memory.
    There is some hardware to accellerate stack accesses, particularly a short list of return addresses, but at least for x86 and such like, the stack is implemented simply as a stack-pointer and it refers to virtual memory which has to be backed by physical memory if you ever access it [which you do at some point or another if you write C or C++ code]. (Of course, it's likely that the virtual memory backed by the physical address is actually copied into the cache and thus the accesses to the physical memory does not actually happen very often or at all - but this is no different from any other part of memory being accessed in the system).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Stack and heap are not C++ terms. The terms are "automatic storage" and "dynamic storage" (and "static storage" for globals and statics). Those are clumsy terms, so everyone calls them stack and heap.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Review required for program of date difference
    By chottachatri in forum C Programming
    Replies: 6
    Last Post: 10-31-2008, 11:46 AM
  2. Difference Equations / Recurrence Relations
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 10-05-2007, 10:26 AM
  3. What's the difference between var++ and ++var
    By ulillillia in forum C Programming
    Replies: 6
    Last Post: 05-31-2007, 02:27 AM
  4. Difference between macro and pass by reference?
    By converge in forum C++ Programming
    Replies: 2
    Last Post: 02-26-2002, 05:20 AM
  5. how to get difference of digits
    By Leeman_s in forum C++ Programming
    Replies: 5
    Last Post: 12-20-2001, 08:32 PM