Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Static vs Dynamic

Static vs Dynamic

Scheduled Pinned Locked Moved C / C++ / MFC
c++visual-studioquestion
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    cobyjone
    wrote on last edited by
    #1

    I am new to C++. When would someone choose to use a static over a dynamic or the other way around?What exactly is the difference between to two? Thanks a lot

    A H 2 Replies Last reply
    0
    • C cobyjone

      I am new to C++. When would someone choose to use a static over a dynamic or the other way around?What exactly is the difference between to two? Thanks a lot

      A Offline
      A Offline
      Antti Keskinen
      wrote on last edited by
      #2

      There are multiple things in C++ that can be 'static' or 'dynamic'. If none of the following match your situation, you should consider reposting and clarifying the context a little more. Like, "Where, in what context, were these words mentioned ?" Most commonly, these two refer to the way a variable is stored in memory. When a variable is considered 'static', it is stored in the local stack memory. Each process that executes in a Win32 environment is allocated a piece of memory for it's private purposes. This memory is known as 'stack'. It cannot be directly accessed by any other application, it is private to the process in question. When a stack variable goes out-of-scope (a function where the variable was declared is finished), it is automatically destroyed and freed. The alternative to 'static' variable is a 'dynamic' variable. At this point, the variable is stored in heap memory. This heap memory is considered all the RAM memory on your computer that is not process-specific. It is available for use by different processes. Each process can reserve a slice of this memory, use it to store a variable, and then release the memory when it is no longer needed. A heap variable never goes out-of-scope. You must manually reserve and release the associated memory. Failing to release memory will lead to memory leaks. The actual C++ keyword, static, when used with a variable, creates a scope-independent variable. This means that when you create a static variable inside a function, and then at some point re-call this function, the variable is automatically initialized to the value it was when the last function execution ended. So to speak, the variable is not 'lost' when it goes out-of-scope. All in all, I suggest you grab a good C++ -book and read it through with thought. C++ is quite difficult at start, but when you get a grasp of it, it becomes like a second language :) -Antti Keskinen ---------------------------------------------- The definition of impossible is strictly dependant on what we think is possible.

      1 Reply Last reply
      0
      • C cobyjone

        I am new to C++. When would someone choose to use a static over a dynamic or the other way around?What exactly is the difference between to two? Thanks a lot

        H Offline
        H Offline
        Harold Bamford
        wrote on last edited by
        #3

        If you are referring to the AppWizard's question about the MFC library, then it is asking if you want the MFC library completely embedded into your application or whether you want to access MFC via a DLL (Dynamically Linked Library). From a programming point of view, there is no difference. Static linking makes the application more portable to weirdly configured machines (such as those with ancient MFC libraries or missing them altogether) but seriously increases the size of your application.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups