using new

This is a discussion on using new within the C++ Programming forums, part of the General Programming Boards category; comment from a book: “ With dynamic memory allocation, however, the compiler doesn’t know how much storage you’re going to ...

  1. #1
    Android
    Join Date
    Mar 2004
    Location
    Kurunegala Colony, Sri Lanka, Sri Lanka
    Posts
    386

    using new

    comment from a book: “ With dynamic memory allocation, however, the compiler doesn’t know how much storage you’re going to need, and it doesn’t know the lifetime of that storage. That is, the storage doesn’t get cleaned up automatically. ”

    I don’t fully understand above statement.
    If I say:
    Code:
    new unsigned char[100];
    Does that mean the compiler do not know that I want 100 new bytes?
    And what’s the meaning of that the compiler doesn’t know the lifetime of that new storage? If I create a variable using new inside a function, isn't it's life time is that function?
    Please explain.

    Thanks a lot.

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    It just means the compiler doesn't know how much memory you're going to need any more than it knows the initial value of a variable. You have to tell it both values.

    The scope of a variable declared with static memory is the length of the function it is declared in or throughout the program if it's global. Not so with dynamic memory. With dynamic memory the memory and the value stored at that address are available until you free the memory manually as long as you pass the variable back and forth between functions.

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Normally, when you declare a variable, it is created from the stack, therefore a) the size is known at compile-time and b) the compiler will generate the code to properly destroy it (remove it from the stack) when the variable goes out of scope. On the other hand, with dynamic allocation, the memory does *not* come from the stack, but from the heap (the pointer to the memory *does* come drom the stack, of course, and will follow normal scoping rules, but what we're talking about here is the memory it *points* to). The heap is really just another area of ram outside of your program's memory space. When you call malloc, or new, the OS's memory manager finds a block from the heap and returns a pointer to it (or NULL if unavailable). Thus, you have to manage the memory yourself, ie: free it when you're done with it.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  4. #4
    Android
    Join Date
    Mar 2004
    Location
    Kurunegala Colony, Sri Lanka, Sri Lanka
    Posts
    386
    It's much clear now. Thanks.

  5. #5
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398

    The example in your book is confusing!

    Actually, the compiler does "know" how much memory to allocate with char[100]. But, wiith dynamic allocation you can say:

    new unsigned char[x]; // Now, the compiler might not know what x is.

    You cannot do that without using dynamic allocation (unless x is a constant/macro defined at compile-time.)

    Also, that statement could be in a loop, and the compiler might not know how many times you are going to execute the loop!!!

    For example, if you need to add a new name to a database... you might not know how many names the database will to hold when you write the program.

    Perhaps the book should have said:
    ...the compiler doesn’t need to know how much storage...

    Or,
    ...the compiler doesn’t care how much storage...

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21