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. Sharing a structure between a console application and a static library

Sharing a structure between a console application and a static library

Scheduled Pinned Locked Moved C / C++ / MFC
performancequestion
12 Posts 4 Posters 2 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.
  • M Offline
    M Offline
    manoharbalu
    wrote on last edited by
    #1

    We have a console application to represent a process plant model. This application has several functions for doing plant calculations. We have defined a structure and a global object for maintaining the data and doing calculations, which is initialized in one of the function. We assign and calculate values for the members of the object by using it in all the corresponding functions. For Eg: struct plantDB { short IMISC_I [300]; char LL1_I [300]; char LL2_I [300]; char MALF_I [300]; char REMP_I [300]; float REM_I [300]; float NXS [30][600]; float VAR_I [1500]; float VART_I [300]; float VRAMP_I [300]; . . . }; plantDB *ETest = new PlantDB; Sample of the function: float EULER(int N,int K,int TYPE,float DER,float MAX,float MIN,float FLAG) { float FACTOR = 0.0 ; float EULER; if ( FLAG ) ETest->VAR_I = 51.0 ; } We have defined a number of functions as shown above which changes the value of each of the members of ETest. Now our requirement is to identify and separate the functions to create a library to abstract it from users of the application. Now, when I create a static library application and add the files with all the functions, and add the lib in my original application to use the functions, the functions still need to use the same object ETest, which is used in the main application as well. Is there any way to share the structure between the main application and the static library. Can I use the createfilemapping for creating a shared memory to assign values in the main app as well as the static library?

    L C 2 Replies Last reply
    0
    • M manoharbalu

      We have a console application to represent a process plant model. This application has several functions for doing plant calculations. We have defined a structure and a global object for maintaining the data and doing calculations, which is initialized in one of the function. We assign and calculate values for the members of the object by using it in all the corresponding functions. For Eg: struct plantDB { short IMISC_I [300]; char LL1_I [300]; char LL2_I [300]; char MALF_I [300]; char REMP_I [300]; float REM_I [300]; float NXS [30][600]; float VAR_I [1500]; float VART_I [300]; float VRAMP_I [300]; . . . }; plantDB *ETest = new PlantDB; Sample of the function: float EULER(int N,int K,int TYPE,float DER,float MAX,float MIN,float FLAG) { float FACTOR = 0.0 ; float EULER; if ( FLAG ) ETest->VAR_I = 51.0 ; } We have defined a number of functions as shown above which changes the value of each of the members of ETest. Now our requirement is to identify and separate the functions to create a library to abstract it from users of the application. Now, when I create a static library application and add the files with all the functions, and add the lib in my original application to use the functions, the functions still need to use the same object ETest, which is used in the main application as well. Is there any way to share the structure between the main application and the static library. Can I use the createfilemapping for creating a shared memory to assign values in the main app as well as the static library?

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

      You should just pass the structure by reference to all functions that need to access its content.

      1 Reply Last reply
      0
      • M manoharbalu

        We have a console application to represent a process plant model. This application has several functions for doing plant calculations. We have defined a structure and a global object for maintaining the data and doing calculations, which is initialized in one of the function. We assign and calculate values for the members of the object by using it in all the corresponding functions. For Eg: struct plantDB { short IMISC_I [300]; char LL1_I [300]; char LL2_I [300]; char MALF_I [300]; char REMP_I [300]; float REM_I [300]; float NXS [30][600]; float VAR_I [1500]; float VART_I [300]; float VRAMP_I [300]; . . . }; plantDB *ETest = new PlantDB; Sample of the function: float EULER(int N,int K,int TYPE,float DER,float MAX,float MIN,float FLAG) { float FACTOR = 0.0 ; float EULER; if ( FLAG ) ETest->VAR_I = 51.0 ; } We have defined a number of functions as shown above which changes the value of each of the members of ETest. Now our requirement is to identify and separate the functions to create a library to abstract it from users of the application. Now, when I create a static library application and add the files with all the functions, and add the lib in my original application to use the functions, the functions still need to use the same object ETest, which is used in the main application as well. Is there any way to share the structure between the main application and the static library. Can I use the createfilemapping for creating a shared memory to assign values in the main app as well as the static library?

        C Offline
        C Offline
        Chris Losinger
        wrote on last edited by
        #3

        you don't need to do anything special for this. static libs (as opposed to DLLs) get linked directly into your app so they'll use the same memory your main app does.

        image processing toolkits | batch image processing

        L M 2 Replies Last reply
        0
        • C Chris Losinger

          you don't need to do anything special for this. static libs (as opposed to DLLs) get linked directly into your app so they'll use the same memory your main app does.

          image processing toolkits | batch image processing

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

          A DLL gets mapped into the main application's address space, so it effectively has full access also.

          C 1 Reply Last reply
          0
          • L Lost User

            A DLL gets mapped into the main application's address space, so it effectively has full access also.

            C Offline
            C Offline
            Chris Losinger
            wrote on last edited by
            #5

            yeah, you're right. i should've phrased that better; i was thinking more along the lines of dynamic CRT, which complicates new/delete.

            image processing toolkits | batch image processing

            L 1 Reply Last reply
            0
            • C Chris Losinger

              you don't need to do anything special for this. static libs (as opposed to DLLs) get linked directly into your app so they'll use the same memory your main app does.

              image processing toolkits | batch image processing

              M Offline
              M Offline
              Munchies_Matt
              wrote on last edited by
              #6

              Chris Losinger wrote:

              static libs (as opposed to DLLs) get linked directly into your app so they'll use the same memory your main app does.

              dlls also get loaded into the process address space and therefore share app memory too. :)

              C 1 Reply Last reply
              0
              • M Munchies_Matt

                Chris Losinger wrote:

                static libs (as opposed to DLLs) get linked directly into your app so they'll use the same memory your main app does.

                dlls also get loaded into the process address space and therefore share app memory too. :)

                C Offline
                C Offline
                Chris Losinger
                wrote on last edited by
                #7

                they won't share CRT memory management, however (which is really what i was trying to say).

                image processing toolkits | batch image processing

                M 1 Reply Last reply
                0
                • C Chris Losinger

                  they won't share CRT memory management, however (which is really what i was trying to say).

                  image processing toolkits | batch image processing

                  M Offline
                  M Offline
                  Munchies_Matt
                  wrote on last edited by
                  #8

                  Doesnt this depend on the version of CRT you used to build them?

                  C 1 Reply Last reply
                  0
                  • C Chris Losinger

                    yeah, you're right. i should've phrased that better; i was thinking more along the lines of dynamic CRT, which complicates new/delete.

                    image processing toolkits | batch image processing

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

                    I don't see how, it is just allocating space on the heap which is part of the data sections in the address space. If this were an issue then hardly any DLL code would work properly.

                    C 1 Reply Last reply
                    0
                    • L Lost User

                      I don't see how, it is just allocating space on the heap which is part of the data sections in the address space. If this were an issue then hardly any DLL code would work properly.

                      C Offline
                      C Offline
                      Chris Losinger
                      wrote on last edited by
                      #10

                      a DLL with it's own copy of the CRT could be using a different version of the CRT than the main app uses. and that will lead to problems if you try to delete/free memory in one place that was allocated in the other. Allocating and freeing memory across module boundaries – The Old New Thing[^]

                      image processing toolkits | batch image processing

                      L 1 Reply Last reply
                      0
                      • M Munchies_Matt

                        Doesnt this depend on the version of CRT you used to build them?

                        C Offline
                        C Offline
                        Chris Losinger
                        wrote on last edited by
                        #11

                        yes

                        image processing toolkits | batch image processing

                        1 Reply Last reply
                        0
                        • C Chris Losinger

                          a DLL with it's own copy of the CRT could be using a different version of the CRT than the main app uses. and that will lead to problems if you try to delete/free memory in one place that was allocated in the other. Allocating and freeing memory across module boundaries – The Old New Thing[^]

                          image processing toolkits | batch image processing

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

                          Chris Losinger wrote:

                          if you try to delete/free memory in one place that was allocated in the other.

                          You should be punished severely. In fact you probably will be when your application starts misbehaving.

                          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