Thread: About dynamic allocation of memory

  1. #1
    Registered User
    Join Date
    Sep 2018
    Posts
    150

    About dynamic allocation of memory

    Suppose I was working for some search engine like Google where every every line of code needs to be at its finest.

    Then if I were writing code for such an engine, would I use pointers to do everything instead of using regular variables? Because variables cannot be de-allocated whereas pointers can be de-allocated right?

    What's a brief estimate for how many variables you can allocate? Just curious.
    Last edited by Nwb; 6 Days Ago at 01:58 AM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,327
    This is C++; you're going to be using the standard containers.

  3. #3
    Registered User
    Join Date
    Sep 2018
    Posts
    150
    Quote Originally Posted by tabstop View Post
    This is C++; you're going to be using the standard containers.
    Then where are pointers used other than to pass by value in functions (and even in that pass by reference is more often used)?
    Last edited by Nwb; 6 Days Ago at 01:59 AM.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    26,041
    To implement the containers and smart pointers, sometimes you might even implement using them because the standard ones don't meet your requirements, whether directly or as components for your own containers and other (template) classes.

    That said, you might use a pointer for an optional argument, though there are alternatives too.

    EDIT:
    I suggest that you read Stroustrup's answer to the FAQ: How do I deal with memory leaks? It is a bit dated in that it mentions the obsolete auto_ptr that has been superseded by unique_ptr, but it still holds true.
    Last edited by laserlight; 6 Days Ago at 02:18 AM.
    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

  5. #5
    Registered User
    Join Date
    Sep 2018
    Posts
    150
    Okay so pointers can be useful. But is it not worth it trying to use dynamically allocated variables? Like if you were working with a lot of data (you have to get rid of the variables at some point right)? Or can you get rid of variables by using
    Code:
    { }
    (block) so that out of the block the variables lose their scope and get de-allocated? I know this is not necessary but suppose, just suppose that if we were making code for some really small machine or for something really important like a search engine? I'm just curious. So how would programmers deal with having very concise memory?

    Heh the forum saw my braces and thought I was trying to post code but that's cool, more forums should have this.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    36,689
    You use dynamic allocation when either of these is true:
    - You don't know at the point the program is started, just how many things you need.
    - You know that you won't want to keep the data for the lifetime of the program.

    So for example, an image editor would use dynamic allocation on both counts because:
    - it doesn't know how big each image is going to be.
    - you won't want to keep the old image lying around when you edit the new image.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    26,041
    Yes, putting your variables in as small a scope as feasible encourages both readability and the deallocation of memory once it is no longer needed. Besides, you can always write the component to manually control deallocation if you really need to do so. Consider std::vector, which has a shrink_to_fit member function, and if that doesn't satisfy you as it is not guaranteed to actually do the reallocation/deallocation, you can swap the vector with an empty vector that will shortly go out of scope.
    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

  8. #8
    Registered User
    Join Date
    Sep 2018
    Posts
    150
    Okay guys thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamic Memory Allocation
    By tictac232434 in forum C Programming
    Replies: 2
    Last Post: 11-20-2012, 12:48 PM
  2. Dynamic memory allocation
    By Luciferek in forum C++ Programming
    Replies: 118
    Last Post: 10-02-2008, 11:34 AM
  3. dynamic memory allocation w/o new
    By bd43274 in forum C++ Programming
    Replies: 11
    Last Post: 02-15-2006, 02:12 AM
  4. Dynamic memory allocation
    By amdeffen in forum C++ Programming
    Replies: 21
    Last Post: 04-29-2004, 08:09 PM
  5. dynamic memory allocation
    By inquisitive in forum C++ Programming
    Replies: 5
    Last Post: 03-13-2004, 02:07 AM

Tags for this Thread