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. Using static for a Large Data Structure

Using static for a Large Data Structure

Scheduled Pinned Locked Moved C / C++ / MFC
data-structureshelpquestion
9 Posts 6 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
    Andy202
    wrote on last edited by
    #1

    I have a large data array and was told to make it static to ensure that I have enough storage allocated DATA_DETAILS signal_details[NO_FIELDS] = { "1.000000", // Scaling "Integer", // Display Format "0", // Raw Data String "0", // Engineering Data String etc Note NO_FIELDS could be 25,000 and some of the elements are char arrays of 4096 bytes. i.e. lots of data. Now if I make it static; e.g. static DATA_DETAILS signal_details[NO_FIELDS] = { "1.000000", // Scaling "Integer", // Display Format "0", // Raw Data String "0", // Engineering Data String etc the project fails to build as I have extern references; e.g including the keyword static causes a problem. extern static DATA_DETAILS signal_details[NO_FIELDS]; How can I overcome this problem please.

    V C W 3 Replies Last reply
    0
    • A Andy202

      I have a large data array and was told to make it static to ensure that I have enough storage allocated DATA_DETAILS signal_details[NO_FIELDS] = { "1.000000", // Scaling "Integer", // Display Format "0", // Raw Data String "0", // Engineering Data String etc Note NO_FIELDS could be 25,000 and some of the elements are char arrays of 4096 bytes. i.e. lots of data. Now if I make it static; e.g. static DATA_DETAILS signal_details[NO_FIELDS] = { "1.000000", // Scaling "Integer", // Display Format "0", // Raw Data String "0", // Engineering Data String etc the project fails to build as I have extern references; e.g including the keyword static causes a problem. extern static DATA_DETAILS signal_details[NO_FIELDS]; How can I overcome this problem please.

      V Offline
      V Offline
      Viorel
      wrote on last edited by
      #2

      I think the static is for the case your array is declared locally in a function. If it is a global variable and is accessed from other modules, then do not add static (otherwise it becomes available from the current file only).

      1 Reply Last reply
      0
      • A Andy202

        I have a large data array and was told to make it static to ensure that I have enough storage allocated DATA_DETAILS signal_details[NO_FIELDS] = { "1.000000", // Scaling "Integer", // Display Format "0", // Raw Data String "0", // Engineering Data String etc Note NO_FIELDS could be 25,000 and some of the elements are char arrays of 4096 bytes. i.e. lots of data. Now if I make it static; e.g. static DATA_DETAILS signal_details[NO_FIELDS] = { "1.000000", // Scaling "Integer", // Display Format "0", // Raw Data String "0", // Engineering Data String etc the project fails to build as I have extern references; e.g including the keyword static causes a problem. extern static DATA_DETAILS signal_details[NO_FIELDS]; How can I overcome this problem please.

        C Offline
        C Offline
        CPallini
        wrote on last edited by
        #3

        Why somebody did tell that? You have not to make it static, since static means only file scope in such context. You can choose to allocate storage on the stack or on the heap. You don't need to allocate static as you did.

        1 Reply Last reply
        0
        • A Andy202

          I have a large data array and was told to make it static to ensure that I have enough storage allocated DATA_DETAILS signal_details[NO_FIELDS] = { "1.000000", // Scaling "Integer", // Display Format "0", // Raw Data String "0", // Engineering Data String etc Note NO_FIELDS could be 25,000 and some of the elements are char arrays of 4096 bytes. i.e. lots of data. Now if I make it static; e.g. static DATA_DETAILS signal_details[NO_FIELDS] = { "1.000000", // Scaling "Integer", // Display Format "0", // Raw Data String "0", // Engineering Data String etc the project fails to build as I have extern references; e.g including the keyword static causes a problem. extern static DATA_DETAILS signal_details[NO_FIELDS]; How can I overcome this problem please.

          W Offline
          W Offline
          Waldermort
          wrote on last edited by
          #4

          When you were told to make it static, they probably meant it in the context of where you allocate it ( stack or heap ). If you use operator new with such a large struct there is a good change you will run out of memory, but declaring it on the stack, as it appears in your code, you are guaranteed to have the memory before you run your exe.

          C 1 Reply Last reply
          0
          • W Waldermort

            When you were told to make it static, they probably meant it in the context of where you allocate it ( stack or heap ). If you use operator new with such a large struct there is a good change you will run out of memory, but declaring it on the stack, as it appears in your code, you are guaranteed to have the memory before you run your exe.

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

            WalderMort wrote:

            you are guaranteed to have the memory before you run your exe

            It depends upon how big is your stack...:):-D:) Anyway, as far as I know, you don't need to declare static a local variable to have guaranteed memory for it.... -- modified at 9:40 Thursday 23rd November, 2006

            P 1 Reply Last reply
            0
            • C CPallini

              WalderMort wrote:

              you are guaranteed to have the memory before you run your exe

              It depends upon how big is your stack...:):-D:) Anyway, as far as I know, you don't need to declare static a local variable to have guaranteed memory for it.... -- modified at 9:40 Thursday 23rd November, 2006

              P Offline
              P Offline
              Prakash Nadar
              wrote on last edited by
              #6

              static vars are not stored in the stack so in a way if you declare a var static, the memory is guaranteed for that var.


              -Prakash

              C T 2 Replies Last reply
              0
              • P Prakash Nadar

                static vars are not stored in the stack so in a way if you declare a var static, the memory is guaranteed for that var.


                -Prakash

                C Offline
                C Offline
                CPallini
                wrote on last edited by
                #7

                Mr.Prakash wrote:

                the memory is guaranteed for that var.

                I think it holds for automatic vars too.

                1 Reply Last reply
                0
                • P Prakash Nadar

                  static vars are not stored in the stack so in a way if you declare a var static, the memory is guaranteed for that var.


                  -Prakash

                  T Offline
                  T Offline
                  tom groezer
                  wrote on last edited by
                  #8

                  well static and extern do not o hand in hand. but what abt the case when we have to make this structure visible to other files(declare extern) and also have guaranteed memory allocation assumin the structure is not local to any function. Is it that the allocatin shall be made on the stack for a function using it.

                  P 1 Reply Last reply
                  0
                  • T tom groezer

                    well static and extern do not o hand in hand. but what abt the case when we have to make this structure visible to other files(declare extern) and also have guaranteed memory allocation assumin the structure is not local to any function. Is it that the allocatin shall be made on the stack for a function using it.

                    P Offline
                    P Offline
                    Prakash Nadar
                    wrote on last edited by
                    #9

                    tom groezer wrote:

                    Is it that the allocatin shall be made on the stack for a function using it.

                    Since the structure is declared in someother file, and used as extern in other files or function, the memory is still allocated to the structure, if the function is making a copy of the structure into its stack then its a different thing, but the original structure has been memory preallocated.


                    -Prakash

                    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