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. Speaking of missing the basics - is there a way to dynamically size an array on the stack ?

Speaking of missing the basics - is there a way to dynamically size an array on the stack ?

Scheduled Pinned Locked Moved C / C++ / MFC
data-structuresperformancehelpquestion
13 Posts 9 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.
  • M Offline
    M Offline
    Mike the Red
    wrote on last edited by
    #1

    I know that it works on the heap...

    char * sz1 = "Text";
    char * sz = new char[strlen(sz1)];

    ...and I know that this will give me an error saying that a constant is expected:

    char * sz1 = "Text";
    char sz[strlen(sz1)];

    ...So is there some other way to create an array on the stack whose size is not hard-coded ? I've seen things like:

    char char * szBuff[iSomeLargeNumber];
    char * sz1 = "Text";
    strcpy(szBuff, sz1);

    But no matter how large iSomeLargeNumber is, there's still a hard-coded limit... and declaring a large array on the stack uses the full amount of memory even if the full array isn't populated...(right?) Is the heap the only answer? As always, thanks for putting up with the nooB.. MZR

    L S S M S 6 Replies Last reply
    0
    • M Mike the Red

      I know that it works on the heap...

      char * sz1 = "Text";
      char * sz = new char[strlen(sz1)];

      ...and I know that this will give me an error saying that a constant is expected:

      char * sz1 = "Text";
      char sz[strlen(sz1)];

      ...So is there some other way to create an array on the stack whose size is not hard-coded ? I've seen things like:

      char char * szBuff[iSomeLargeNumber];
      char * sz1 = "Text";
      strcpy(szBuff, sz1);

      But no matter how large iSomeLargeNumber is, there's still a hard-coded limit... and declaring a large array on the stack uses the full amount of memory even if the full array isn't populated...(right?) Is the heap the only answer? As always, thanks for putting up with the nooB.. MZR

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

      There is alloca

      1 Reply Last reply
      0
      • M Mike the Red

        I know that it works on the heap...

        char * sz1 = "Text";
        char * sz = new char[strlen(sz1)];

        ...and I know that this will give me an error saying that a constant is expected:

        char * sz1 = "Text";
        char sz[strlen(sz1)];

        ...So is there some other way to create an array on the stack whose size is not hard-coded ? I've seen things like:

        char char * szBuff[iSomeLargeNumber];
        char * sz1 = "Text";
        strcpy(szBuff, sz1);

        But no matter how large iSomeLargeNumber is, there's still a hard-coded limit... and declaring a large array on the stack uses the full amount of memory even if the full array isn't populated...(right?) Is the heap the only answer? As always, thanks for putting up with the nooB.. MZR

        S Offline
        S Offline
        Stuart Dootson
        wrote on last edited by
        #3

        You could use

        char * sz1 = "Text";
        char* sz = (char*)_alloca(strlen(sz1));

        but make sure you don't exhaust available stack space (starts at 1MB and decreases for standard Windows threads). These days, Microsoft want you to use _malloca[^] instead - it's more safe apparently.

        Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

        1 Reply Last reply
        0
        • M Mike the Red

          I know that it works on the heap...

          char * sz1 = "Text";
          char * sz = new char[strlen(sz1)];

          ...and I know that this will give me an error saying that a constant is expected:

          char * sz1 = "Text";
          char sz[strlen(sz1)];

          ...So is there some other way to create an array on the stack whose size is not hard-coded ? I've seen things like:

          char char * szBuff[iSomeLargeNumber];
          char * sz1 = "Text";
          strcpy(szBuff, sz1);

          But no matter how large iSomeLargeNumber is, there's still a hard-coded limit... and declaring a large array on the stack uses the full amount of memory even if the full array isn't populated...(right?) Is the heap the only answer? As always, thanks for putting up with the nooB.. MZR

          S Offline
          S Offline
          Sarath C
          wrote on last edited by
          #4

          In addition, you've don't have to free pointer allocated using _alloca or _malloca because stack will cleared when the function is returned.

          -Sarath. "Great hopes make everything great possible" - Benjamin Franklin

          My blog - Sharing My Thoughts

          1 Reply Last reply
          0
          • M Mike the Red

            I know that it works on the heap...

            char * sz1 = "Text";
            char * sz = new char[strlen(sz1)];

            ...and I know that this will give me an error saying that a constant is expected:

            char * sz1 = "Text";
            char sz[strlen(sz1)];

            ...So is there some other way to create an array on the stack whose size is not hard-coded ? I've seen things like:

            char char * szBuff[iSomeLargeNumber];
            char * sz1 = "Text";
            strcpy(szBuff, sz1);

            But no matter how large iSomeLargeNumber is, there's still a hard-coded limit... and declaring a large array on the stack uses the full amount of memory even if the full array isn't populated...(right?) Is the heap the only answer? As always, thanks for putting up with the nooB.. MZR

            M Offline
            M Offline
            Mike the Red
            wrote on last edited by
            #5

            ...though that 1mb limit is rather unfortunate, for my current project. I'll have to use the heap after all, but it's a good thing to know for the future. MZR

            _ L 2 Replies Last reply
            0
            • M Mike the Red

              ...though that 1mb limit is rather unfortunate, for my current project. I'll have to use the heap after all, but it's a good thing to know for the future. MZR

              _ Offline
              _ Offline
              _Superman_
              wrote on last edited by
              #6

              You can change the default stack size from the project properties. Project Properties -> Configuration Properties -> Linker -> System -> Stack Reserve Size / Stack Commit Size. Look at the documentation for the /STACK[^] linker option.

              «_Superman_» I love work. It gives me something to do between weekends.

              1 Reply Last reply
              0
              • M Mike the Red

                I know that it works on the heap...

                char * sz1 = "Text";
                char * sz = new char[strlen(sz1)];

                ...and I know that this will give me an error saying that a constant is expected:

                char * sz1 = "Text";
                char sz[strlen(sz1)];

                ...So is there some other way to create an array on the stack whose size is not hard-coded ? I've seen things like:

                char char * szBuff[iSomeLargeNumber];
                char * sz1 = "Text";
                strcpy(szBuff, sz1);

                But no matter how large iSomeLargeNumber is, there's still a hard-coded limit... and declaring a large array on the stack uses the full amount of memory even if the full array isn't populated...(right?) Is the heap the only answer? As always, thanks for putting up with the nooB.. MZR

                S Offline
                S Offline
                Single Step Debugger
                wrote on last edited by
                #7

                I think this is not going in the stack but rather in the string table. My advice is to avoid this approach.

                The narrow specialist in the broad sense of the word is a complete idiot in the narrow sense of the word. Advertise here – minimum three posts per day are guaranteed.

                1 Reply Last reply
                0
                • M Mike the Red

                  I know that it works on the heap...

                  char * sz1 = "Text";
                  char * sz = new char[strlen(sz1)];

                  ...and I know that this will give me an error saying that a constant is expected:

                  char * sz1 = "Text";
                  char sz[strlen(sz1)];

                  ...So is there some other way to create an array on the stack whose size is not hard-coded ? I've seen things like:

                  char char * szBuff[iSomeLargeNumber];
                  char * sz1 = "Text";
                  strcpy(szBuff, sz1);

                  But no matter how large iSomeLargeNumber is, there's still a hard-coded limit... and declaring a large array on the stack uses the full amount of memory even if the full array isn't populated...(right?) Is the heap the only answer? As always, thanks for putting up with the nooB.. MZR

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

                  Are you really sure you want to allocate large chunks of data on the stack? Off the top of my head I can't think of any really good reason for doing this, and it seems to be more trouble than it's worth compared to heap allocation.

                  There are three kinds of people in the world - those who can count and those who can't...

                  M 1 Reply Last reply
                  0
                  • M molesworth

                    Are you really sure you want to allocate large chunks of data on the stack? Off the top of my head I can't think of any really good reason for doing this, and it seems to be more trouble than it's worth compared to heap allocation.

                    There are three kinds of people in the world - those who can count and those who can't...

                    M Offline
                    M Offline
                    Mike the Red
                    wrote on last edited by
                    #9

                    I'm writing an ISAPI extension to handle form data including one or more file uploads. Most of what I've read on ISAPI extensions has indicated that time and memory are of the essence, and, because of that, using the heap should be avoided, as much as is reasonable, in favor of using the stack. I understand a few parts of the stack/heap concept, but my knowledge (and understanding) is severly lacking in this area. As such, I didn't know whether or not trying to place the entire HTTP-REQUEST (including the uploaded files) on the stack was possible or reasonable until I asked. That said, when I asked about dynamically sizing an array I was more thinking about "standard-length" strings... I have been programming for several years, but, until the past couple years, almost exclusively in higher-level languages that didn't require me to keep track of memory. And, when I started with C++, I was using managed C++ with its garbage-collection crutch. Now that I'm trying (or having) to ditch the __gc, I try to keep things on the stack as much as possible. Tracking down those ommitted deletes is a pain, especially for often-changed member variables where I tend to forget to delete the existing object/value before creating the new one. Thank you for your patience in guiding the nooB! MZR

                    K R S 3 Replies Last reply
                    0
                    • M Mike the Red

                      I'm writing an ISAPI extension to handle form data including one or more file uploads. Most of what I've read on ISAPI extensions has indicated that time and memory are of the essence, and, because of that, using the heap should be avoided, as much as is reasonable, in favor of using the stack. I understand a few parts of the stack/heap concept, but my knowledge (and understanding) is severly lacking in this area. As such, I didn't know whether or not trying to place the entire HTTP-REQUEST (including the uploaded files) on the stack was possible or reasonable until I asked. That said, when I asked about dynamically sizing an array I was more thinking about "standard-length" strings... I have been programming for several years, but, until the past couple years, almost exclusively in higher-level languages that didn't require me to keep track of memory. And, when I started with C++, I was using managed C++ with its garbage-collection crutch. Now that I'm trying (or having) to ditch the __gc, I try to keep things on the stack as much as possible. Tracking down those ommitted deletes is a pain, especially for often-changed member variables where I tend to forget to delete the existing object/value before creating the new one. Thank you for your patience in guiding the nooB! MZR

                      K Offline
                      K Offline
                      Kevin McFarlane
                      wrote on last edited by
                      #10

                      Mike the Red wrote:

                      I try to keep things on the stack as much as possible.

                      That's reasonable. The general maxim in C++ is to use the stack except when you need to use the heap. :)

                      Mike the Red wrote:

                      Tracking down those ommitted deletes is a pain, especially for often-changed member variables where I tend to forget to delete the existing object/value before creating the new one.

                      Unless you have certain constraints within ISAPI (I'm not familiar with it) you should use Standard C++ (STL) and Boost to help you with such matters, i.e., avoid raw new and delete. However, there is a learning curve and I don't know how long you've been doing C++ for. For arrays you could use the STL vector. Then you don't need to worry about new and delete for dynamically sizing the array.

                      Kevin

                      1 Reply Last reply
                      0
                      • M Mike the Red

                        I'm writing an ISAPI extension to handle form data including one or more file uploads. Most of what I've read on ISAPI extensions has indicated that time and memory are of the essence, and, because of that, using the heap should be avoided, as much as is reasonable, in favor of using the stack. I understand a few parts of the stack/heap concept, but my knowledge (and understanding) is severly lacking in this area. As such, I didn't know whether or not trying to place the entire HTTP-REQUEST (including the uploaded files) on the stack was possible or reasonable until I asked. That said, when I asked about dynamically sizing an array I was more thinking about "standard-length" strings... I have been programming for several years, but, until the past couple years, almost exclusively in higher-level languages that didn't require me to keep track of memory. And, when I started with C++, I was using managed C++ with its garbage-collection crutch. Now that I'm trying (or having) to ditch the __gc, I try to keep things on the stack as much as possible. Tracking down those ommitted deletes is a pain, especially for often-changed member variables where I tend to forget to delete the existing object/value before creating the new one. Thank you for your patience in guiding the nooB! MZR

                        R Offline
                        R Offline
                        Rajesh R Subramanian
                        wrote on last edited by
                        #11

                        I do not pretend to be an ISAPI expert, but you're trying to get into some kind of hand-crafted memory management. This can be more dangerous than you imagine. Try using some of those existing and well tested libraries like stl, mfc, etc., Agreed that utilising the stack as much as possible is a good thing, but I don't think that it is going to make *that much* of a difference on any normal application. If you try pushing the stack to its limits, there's also a danger of stack overflow. I'll humbly suggest you to drop this idea of 'manual' memory management adventure. For all the pain that it could cause, it isn't worth. Frequent allocation and deallocation of the heap can fragment it and there are memory damage possibilities, memory leaks, etc., I won't ever choose something of this sort, unless I'm writing a memory manager or something like that myself. Pick an existing library and it would use the heap in the best possible way for you.

                        It is a crappy thing, but it's life -^ Carlo Pallini

                        1 Reply Last reply
                        0
                        • M Mike the Red

                          I'm writing an ISAPI extension to handle form data including one or more file uploads. Most of what I've read on ISAPI extensions has indicated that time and memory are of the essence, and, because of that, using the heap should be avoided, as much as is reasonable, in favor of using the stack. I understand a few parts of the stack/heap concept, but my knowledge (and understanding) is severly lacking in this area. As such, I didn't know whether or not trying to place the entire HTTP-REQUEST (including the uploaded files) on the stack was possible or reasonable until I asked. That said, when I asked about dynamically sizing an array I was more thinking about "standard-length" strings... I have been programming for several years, but, until the past couple years, almost exclusively in higher-level languages that didn't require me to keep track of memory. And, when I started with C++, I was using managed C++ with its garbage-collection crutch. Now that I'm trying (or having) to ditch the __gc, I try to keep things on the stack as much as possible. Tracking down those ommitted deletes is a pain, especially for often-changed member variables where I tend to forget to delete the existing object/value before creating the new one. Thank you for your patience in guiding the nooB! MZR

                          S Offline
                          S Offline
                          Stuart Dootson
                          wrote on last edited by
                          #12

                          Mike the Red wrote:

                          using the heap should be avoided, as much as is reasonable

                          I think > than around 10kB of allocation probably crosses the boundary of reasonable and unreasonable avoidance of heap usage :-)

                          Mike the Red wrote:

                          Tracking down those ommitted deletes is a pain, especially for often-changed member variables where I tend to forget to delete the existing object/value before creating the new one

                          If you're using VC2008, then std::tr1::shared_ptr[^] is handy. If not (or even if you are - see shared_array), then Boost.SmartPointers[^] is handy. The best advice is to wrap resource allocation/deallocation in an object[^] (allocation in constructors, deallocation in the destructor), so you can tie the resource usage to the object's lifetime. That's how the STL containers (vector, list, map, set etc) work. C++ (when written in a nice idiomatic way) relies on deterministic destruction to manage resources - when done right, it makes programming as easy as using garbage collection, IMO.

                          Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

                          1 Reply Last reply
                          0
                          • M Mike the Red

                            ...though that 1mb limit is rather unfortunate, for my current project. I'll have to use the heap after all, but it's a good thing to know for the future. MZR

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

                            You're welcome, but please don't write my name like that..

                            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