Thread: when do you think dynamic memory is useful instead of static memory ?

  1. #1
    Registered User
    Join Date
    May 2017
    Posts
    129

    when do you think dynamic memory is useful instead of static memory ?

    I am looking for example that prove dynamic memory will be useful instead of static memory. Dynamic memory is allocated at run time where as static is allocated at compile time.

    I found example for both type of memory but that doesn't explain difference and doesn't give idea how dynamic memory is useful instead of static memory.?


  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Do you understand that at run-time it is possible more information is known about how much memory is needed?

    If no, then you need to learn why it is yes before any answer will make sense to you.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User
    Join Date
    May 2017
    Posts
    129
    Quote Originally Posted by stahta01 View Post
    Do you understand that at run-time it is possible more information is known about how much memory is needed?

    If no, then you need to learn why it is yes before any answer will make sense to you.

    Tim S.
    compile time means that we have written the program and we have to do compile it to check the error. Run time means that the program has been compiled and now it will run on the device

    Whenever we write a program, we know how much size we need to store information. When we know the size then we can allocate the static memory.

    By searching Google, it is known that when we do not know how much memory we need, then we use dynamic memory. I don't know when we don't know the size , so any body can give one example to understand difference between static and dynamic memory allocation ?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by abhi143
    Whenever we write a program, we know how much size we need to store information.
    "Oh, my sweet summer child."

    Quote Originally Posted by abhi143
    By searching Google, it is known that when we do not know how much memory we need, then we use dynamic memory. I don't know when we don't know the size , so any body can give one example to understand difference between static and dynamic memory allocation ?
    I am going to provide to you a text file containing English words, all in lowercase, one word per line. Your task is to write a C program to count the frequency with which each word appears in the file, and print to standard output the distinct words and their corresponding frequencies, sorted according to the words in lexicographical order.

    One approach to doing this is to read all the words into a big array, sort the array, and then make a single pass over the sorted array to count the distinct words. Now, tell me what is the size of this array that you are going to allocate at compile time?

    This is, of course, a trick question: I control the content of the file yet I didn't tell you the maximum number of words, so whatever number you come up with I can gleefully tell you that you're wrong because the actual number of words in the file is +1 of the number that you came up with. So, in order to accommodate my power to control the number of words, you'll have to use dynamic memory allocation.

    (Having said that, if you were permitted to use other approaches, you might be able to get away with something fixed size like a perfect hash table or a trie of a definitive list of words in English, but besides that being overkill if it turns out that the file only has a few words, it's also possible to change the requirements such that the language is not necessarily English, but possibly even a constructed regular language where "word" means something like "one or more alphabetic characters", in which case pre-constructing even a trie from that would be impossible.)
    Last edited by laserlight; 04-14-2020 at 10:25 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C static variables + dynamic memory allocation
    By dralexpe in forum C Programming
    Replies: 3
    Last Post: 06-25-2012, 06:58 PM
  2. Replies: 7
    Last Post: 11-18-2011, 07:49 PM
  3. Dynamic memory and realloc(), freeing memory
    By C_Sparky in forum C Programming
    Replies: 6
    Last Post: 10-06-2010, 07:55 PM
  4. Memory (Static vs Dynamic)
    By tempster09 in forum C Programming
    Replies: 2
    Last Post: 12-11-2009, 06:39 PM
  5. static memory and dynamic memory
    By nextus in forum C++ Programming
    Replies: 1
    Last Post: 03-01-2003, 08:46 PM

Tags for this Thread