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. How C++ manage memory in event driven system ?

How C++ manage memory in event driven system ?

Scheduled Pinned Locked Moved C / C++ / MFC
performancequestionc++code-review
7 Posts 2 Posters 4 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.
  • L Offline
    L Offline
    Lost User
    wrote on last edited by
    #1

    I am trying to improve my C++ skills and could use some advice. I am using event driven tool - I am not allowed to say the name here - and my basic question is if I create an instance of a class which uses a library should the library by instantiated in class constructor as a common to all class methods variable or be instantiated in each method ( as a local method variable ) as needed ? Process speed is immaterial, my question is about how is memory allocated / de-allocated ( managed by C++) during the events.

    L K 2 Replies Last reply
    0
    • L Lost User

      I am trying to improve my C++ skills and could use some advice. I am using event driven tool - I am not allowed to say the name here - and my basic question is if I create an instance of a class which uses a library should the library by instantiated in class constructor as a common to all class methods variable or be instantiated in each method ( as a local method variable ) as needed ? Process speed is immaterial, my question is about how is memory allocated / de-allocated ( managed by C++) during the events.

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      You do not normally instantiate a library. If the library contains classes then you would instantiate objects as you need them. If it only contains functions then there is nothing to instantiate. But without details of the actual library and how you want to use it, it is not possible to be clearer.

      1 Reply Last reply
      0
      • L Lost User

        I am trying to improve my C++ skills and could use some advice. I am using event driven tool - I am not allowed to say the name here - and my basic question is if I create an instance of a class which uses a library should the library by instantiated in class constructor as a common to all class methods variable or be instantiated in each method ( as a local method variable ) as needed ? Process speed is immaterial, my question is about how is memory allocated / de-allocated ( managed by C++) during the events.

        K Offline
        K Offline
        k5054
        wrote on last edited by
        #3

        This is probably best answered by the provider of the library. Is the library smart enough to know it's been initialized, so repeated initializations don't leak resources? Does the library need to initialized per thread, per object created, or on some other scheme? Is finalization needed? If so, is that per thread/object/other? As Richard points out, without more detail, we'd just be guessing.

        "A little song, a little dance, a little seltzer down your pants" Chuckles the clown

        L 1 Reply Last reply
        0
        • K k5054

          This is probably best answered by the provider of the library. Is the library smart enough to know it's been initialized, so repeated initializations don't leak resources? Does the library need to initialized per thread, per object created, or on some other scheme? Is finalization needed? If so, is that per thread/object/other? As Richard points out, without more detail, we'd just be guessing.

          "A little song, a little dance, a little seltzer down your pants" Chuckles the clown

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Maybe the answer is in this approach create an instance of the object create primary method and create local instance of the library create secondary method and again create local instance of the library that way there are two instances of the library "in use " the compiler /linker may flag it or not I really think the issue is - these methods are events and "go away" when done - which is really confusing as far as actual C++ "deleting" unused stuff.

          L K 2 Replies Last reply
          0
          • L Lost User

            Maybe the answer is in this approach create an instance of the object create primary method and create local instance of the library create secondary method and again create local instance of the library that way there are two instances of the library "in use " the compiler /linker may flag it or not I really think the issue is - these methods are events and "go away" when done - which is really confusing as far as actual C++ "deleting" unused stuff.

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            Salvatore Terress wrote:

            create primary method and create local instance of the library create secondary method and again create local instance of the library

            As I said above, there is no "ceate an instance of a library". A library is linked into your application and appears (at the excution level) as if it is just part of your code. So please read my answer once again, or get yourself a good book on C++.

            1 Reply Last reply
            0
            • L Lost User

              Maybe the answer is in this approach create an instance of the object create primary method and create local instance of the library create secondary method and again create local instance of the library that way there are two instances of the library "in use " the compiler /linker may flag it or not I really think the issue is - these methods are events and "go away" when done - which is really confusing as far as actual C++ "deleting" unused stuff.

              K Offline
              K Offline
              k5054
              wrote on last edited by
              #6

              When we talk about a library we generally mean a DLL (.so in linux world) or static library (.a in linux) that is linked in with your code. That being the case, there is only one instance of any given library during the execution of a single process1, even if its multi threaded. Either you don't understand this, or you're using the term library to refer to something else. If the latter, you need to explain your concerns differently. The difference between a DLL and a static library, in case you don't know, is that the functions in a static library are linked in at compile time, while dynamic libraries are linked in at run time. That means that when a change is introduced in a library (bug fix, for example), a program that is linked against a static library needs to be relinked after the static library has been compiled and installed. A program that is linked against a DLL gets the benefit of the change the next time it is run, without the need to relink it. 1There is a way to get multiple copies of a shared library attached to a single process, but that involves using dlopen() and friends, which I assume you're not doing here. Update: Just checked, multiple calls to dlopen() does not attach multiple instances of a library to a process, so ignore that bit.

              "A little song, a little dance, a little seltzer down your pants" Chuckles the clown

              L 1 Reply Last reply
              0
              • K k5054

                When we talk about a library we generally mean a DLL (.so in linux world) or static library (.a in linux) that is linked in with your code. That being the case, there is only one instance of any given library during the execution of a single process1, even if its multi threaded. Either you don't understand this, or you're using the term library to refer to something else. If the latter, you need to explain your concerns differently. The difference between a DLL and a static library, in case you don't know, is that the functions in a static library are linked in at compile time, while dynamic libraries are linked in at run time. That means that when a change is introduced in a library (bug fix, for example), a program that is linked against a static library needs to be relinked after the static library has been compiled and installed. A program that is linked against a DLL gets the benefit of the change the next time it is run, without the need to relink it. 1There is a way to get multiple copies of a shared library attached to a single process, but that involves using dlopen() and friends, which I assume you're not doing here. Update: Just checked, multiple calls to dlopen() does not attach multiple instances of a library to a process, so ignore that bit.

                "A little song, a little dance, a little seltzer down your pants" Chuckles the clown

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                Thank you for your constructive contribution to this discussion. Appreciate your input and now I have a better understanding of the process.

                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