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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. About Memory initialization

About Memory initialization

Scheduled Pinned Locked Moved C / C++ / MFC
questionperformance
12 Posts 7 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
    manish patel
    wrote on last edited by
    #1

    I want to do something like this: char *str = new char[n]; Here n can be vary at runtime. So anybody tell me how can i do this without new operator? Manish Patel

    R J M T J 5 Replies Last reply
    0
    • M manish patel

      I want to do something like this: char *str = new char[n]; Here n can be vary at runtime. So anybody tell me how can i do this without new operator? Manish Patel

      R Offline
      R Offline
      Raj Prathap
      wrote on last edited by
      #2

      Use char *str = malloc(n*sizeof(char)); if str is local, then foo(int n) { char str[n]; } is also valid in vc6.0 Regards, Pratap

      1 Reply Last reply
      0
      • M manish patel

        I want to do something like this: char *str = new char[n]; Here n can be vary at runtime. So anybody tell me how can i do this without new operator? Manish Patel

        J Offline
        J Offline
        jhwurmbach
        wrote on last edited by
        #3

        std::string text(n);

        const char* str = &text[0];

        Remember the const, though. Almost the same is possible with the MFC CString.


        Let's think the unthinkable, let's do the undoable, let's prepare to grapple with the ineffable itself, and see if we may not eff it after all.
        Douglas Adams, "Dirk Gently's Holistic Detective Agency"

        T 1 Reply Last reply
        0
        • M manish patel

          I want to do something like this: char *str = new char[n]; Here n can be vary at runtime. So anybody tell me how can i do this without new operator? Manish Patel

          M Offline
          M Offline
          Matthew Faithfull
          wrote on last edited by
          #4

          The short answer is you can't. The long answer is:- You can use a pre-written class like std::string or CString instead of a plain character array but somewhere underneath the neat interface new will be in use if you change the memory size you require at runtime. One alternative is to fix n but to make it large enough that there won't be a problem. e.g. char str[1024]; This is always risky and can be wasteful but may be the right solution in some cases. Another approach is to use BSTRs otherwise known as Bee Stings because they can be very painful. SysAllocString and realted APIs use some special Win32 magic, probably involving reserved memory or non-paged pool to do memory operations faster than ordinary new and delete. If you dig around CP and elsewhere you might even find a string class that uses BSTRs internally but has a nice easy to use CString like interface. It will still likely be less efficient than using plain character arrays though if raw speed is all you're after.

          Nothing is exactly what it seems but everything with seems can be unpicked.

          T 1 Reply Last reply
          0
          • M manish patel

            I want to do something like this: char *str = new char[n]; Here n can be vary at runtime. So anybody tell me how can i do this without new operator? Manish Patel

            T Offline
            T Offline
            toxcct
            wrote on last edited by
            #5

            hey, you didn't gave me time to finalize the answer ! :( here it is[^] enjoy :rose:


            [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

            1 Reply Last reply
            0
            • J jhwurmbach

              std::string text(n);

              const char* str = &text[0];

              Remember the const, though. Almost the same is possible with the MFC CString.


              Let's think the unthinkable, let's do the undoable, let's prepare to grapple with the ineffable itself, and see if we may not eff it after all.
              Douglas Adams, "Dirk Gently's Holistic Detective Agency"

              T Offline
              T Offline
              toxcct
              wrote on last edited by
              #6

              to get the char* string of a std::string, you can use the std::string::**c_str**() function for that


              [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

              J 1 Reply Last reply
              0
              • M Matthew Faithfull

                The short answer is you can't. The long answer is:- You can use a pre-written class like std::string or CString instead of a plain character array but somewhere underneath the neat interface new will be in use if you change the memory size you require at runtime. One alternative is to fix n but to make it large enough that there won't be a problem. e.g. char str[1024]; This is always risky and can be wasteful but may be the right solution in some cases. Another approach is to use BSTRs otherwise known as Bee Stings because they can be very painful. SysAllocString and realted APIs use some special Win32 magic, probably involving reserved memory or non-paged pool to do memory operations faster than ordinary new and delete. If you dig around CP and elsewhere you might even find a string class that uses BSTRs internally but has a nice easy to use CString like interface. It will still likely be less efficient than using plain character arrays though if raw speed is all you're after.

                Nothing is exactly what it seems but everything with seems can be unpicked.

                T Offline
                T Offline
                toxcct
                wrote on last edited by
                #7

                i think he was just confused about all the complaining answers he got there[^] ;)


                [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

                1 Reply Last reply
                0
                • T toxcct

                  to get the char* string of a std::string, you can use the std::string::**c_str**() function for that


                  [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

                  J Offline
                  J Offline
                  jhwurmbach
                  wrote on last edited by
                  #8

                  You are of course right. I had changed my post from using std::vector<char> to std::string. I obviously did it without thinking too much


                  Let's think the unthinkable, let's do the undoable, let's prepare to grapple with the ineffable itself, and see if we may not eff it after all.
                  Douglas Adams, "Dirk Gently's Holistic Detective Agency"

                  N 1 Reply Last reply
                  0
                  • M manish patel

                    I want to do something like this: char *str = new char[n]; Here n can be vary at runtime. So anybody tell me how can i do this without new operator? Manish Patel

                    J Offline
                    J Offline
                    John R Shaw
                    wrote on last edited by
                    #9

                    Ahaaa! Use STL instead! Seriously use the STL, unless you have a good reason not too. The little code line you showed is equivalent to “char* str = (char*)malloc(n)” in C code. Use std::string instead of that and ignore the internal details (life gets complicated there). I love C, but you have to learn to separate the concepts that apply to C from those that apply to C++. If you use the STL liberally, then you will rarely need to allocate memory directly.

                    INTP "Program testing can be used to show the presence of bugs, but never to show their absence."Edsger Dijkstra

                    1 Reply Last reply
                    0
                    • J jhwurmbach

                      You are of course right. I had changed my post from using std::vector<char> to std::string. I obviously did it without thinking too much


                      Let's think the unthinkable, let's do the undoable, let's prepare to grapple with the ineffable itself, and see if we may not eff it after all.
                      Douglas Adams, "Dirk Gently's Holistic Detective Agency"

                      N Offline
                      N Offline
                      Nemanja Trifunovic
                      wrote on last edited by
                      #10

                      Actually, it will work even for the string the way you wrote it. It is not standardized yet, but it will be soon, and all the implementation already enable this.


                      Programming Blog utf8-cpp

                      J 1 Reply Last reply
                      0
                      • N Nemanja Trifunovic

                        Actually, it will work even for the string the way you wrote it. It is not standardized yet, but it will be soon, and all the implementation already enable this.


                        Programming Blog utf8-cpp

                        J Offline
                        J Offline
                        jhwurmbach
                        wrote on last edited by
                        #11

                        There had be a 'technical addendum' or something, requiring the std::vector to store its contained items in one continuous memory block. Also, the complexity-requirements almost demand an implementation like this. But for std::string, you are really at the mercy of the implementors., I guess (But it works with VC2003 and VC2005).


                        Let's think the unthinkable, let's do the undoable, let's prepare to grapple with the ineffable itself, and see if we may not eff it after all.
                        Douglas Adams, "Dirk Gently's Holistic Detective Agency"

                        N 1 Reply Last reply
                        0
                        • J jhwurmbach

                          There had be a 'technical addendum' or something, requiring the std::vector to store its contained items in one continuous memory block. Also, the complexity-requirements almost demand an implementation like this. But for std::string, you are really at the mercy of the implementors., I guess (But it works with VC2003 and VC2005).


                          Let's think the unthinkable, let's do the undoable, let's prepare to grapple with the ineffable itself, and see if we may not eff it after all.
                          Douglas Adams, "Dirk Gently's Holistic Detective Agency"

                          N Offline
                          N Offline
                          Nemanja Trifunovic
                          wrote on last edited by
                          #12

                          jhwurmbach wrote:

                          But for std::string, you are really at the mercy of the implementors., I guess

                          As I said, in theory yes - but all the implementations allow this, and it will be standardized in C++ 0x.


                          Programming Blog utf8-cpp

                          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