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 function variable problem

Static function variable problem

Scheduled Pinned Locked Moved C / C++ / MFC
helptutorialquestion
6 Posts 5 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.
  • A Offline
    A Offline
    alikalik
    wrote on last edited by
    #1

    Hello, I have a function that is called from a dll and contains static variable:

    void myFn(double data)
    {
    static double x;
    ...
    }

    At each iteration x increases on some value. The problem occurs when pass diffent data at the same time (i.e. call the function more than once at the same time), x variable mixes all values.

    data1[] = 1,2,3
    data2[] = 4,5,6

    The result for the first call must be 1+2+3 = 6 The result for the second call must be 4+5+6 = 15 But now the result for the first call will be 1+4+2+5+... and so on. How to avoid this problem?

    D C L CPalliniC 4 Replies Last reply
    0
    • A alikalik

      Hello, I have a function that is called from a dll and contains static variable:

      void myFn(double data)
      {
      static double x;
      ...
      }

      At each iteration x increases on some value. The problem occurs when pass diffent data at the same time (i.e. call the function more than once at the same time), x variable mixes all values.

      data1[] = 1,2,3
      data2[] = 4,5,6

      The result for the first call must be 1+2+3 = 6 The result for the second call must be 4+5+6 = 15 But now the result for the first call will be 1+4+2+5+... and so on. How to avoid this problem?

      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #2

      At first glance, your problem sounds like one of synchronization. But then I could not figure out how you were calling myFn(). It is expecting a single argument, yet you've shown two arrays. Are you passing one member of the array with each call to myFn()?

      alikalik wrote:

      The result for the first call must be 1+2+3 = 6 The result for the second call must be 4+5+6 = 15 But now the result for the first call will be 1+4+2+5+... and so on.

      Nothing you've shown supports this. :confused:

      "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

      "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

      1 Reply Last reply
      0
      • A alikalik

        Hello, I have a function that is called from a dll and contains static variable:

        void myFn(double data)
        {
        static double x;
        ...
        }

        At each iteration x increases on some value. The problem occurs when pass diffent data at the same time (i.e. call the function more than once at the same time), x variable mixes all values.

        data1[] = 1,2,3
        data2[] = 4,5,6

        The result for the first call must be 1+2+3 = 6 The result for the second call must be 4+5+6 = 15 But now the result for the first call will be 1+4+2+5+... and so on. How to avoid this problem?

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

        alikalik wrote:

        call the function more than once at the same time

        are you calling the DLL from multiple threads ?

        image processing toolkits | batch image processing

        A 1 Reply Last reply
        0
        • A alikalik

          Hello, I have a function that is called from a dll and contains static variable:

          void myFn(double data)
          {
          static double x;
          ...
          }

          At each iteration x increases on some value. The problem occurs when pass diffent data at the same time (i.e. call the function more than once at the same time), x variable mixes all values.

          data1[] = 1,2,3
          data2[] = 4,5,6

          The result for the first call must be 1+2+3 = 6 The result for the second call must be 4+5+6 = 15 But now the result for the first call will be 1+4+2+5+... and so on. How to avoid this problem?

          L Offline
          L Offline
          led mike
          wrote on last edited by
          #4

          Try putting that code in your main() function.

          1 Reply Last reply
          0
          • A alikalik

            Hello, I have a function that is called from a dll and contains static variable:

            void myFn(double data)
            {
            static double x;
            ...
            }

            At each iteration x increases on some value. The problem occurs when pass diffent data at the same time (i.e. call the function more than once at the same time), x variable mixes all values.

            data1[] = 1,2,3
            data2[] = 4,5,6

            The result for the first call must be 1+2+3 = 6 The result for the second call must be 4+5+6 = 15 But now the result for the first call will be 1+4+2+5+... and so on. How to avoid this problem?

            CPalliniC Offline
            CPalliniC Offline
            CPallini
            wrote on last edited by
            #5

            Remove the static local variable making it an argument passed by reference. :)

            If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
            This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
            [My articles]

            In testa che avete, signor di Ceprano?

            1 Reply Last reply
            0
            • C Chris Losinger

              alikalik wrote:

              call the function more than once at the same time

              are you calling the DLL from multiple threads ?

              image processing toolkits | batch image processing

              A Offline
              A Offline
              alikalik
              wrote on last edited by
              #6

              I'm creating add-in for MS Excel. The argument is a value in a cell. So for instance, if I call MyFn(B11) and MyFn(B12), where B11 and B12 are cells, the result will be wrong.

              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