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. array of char*

array of char*

Scheduled Pinned Locked Moved C / C++ / MFC
data-structuressalesperformancetutorial
17 Posts 8 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.
  • P piul

    Hi there! I need an array of strings of char * type and I am not clear about how to free up memory after use. The code below does not work.

    char \* ptr\_p\_c\[3\];
    ptr\_p\_c\[0\] = new char;
    strcpy (ptr\_p\_c\[0\], "hi there");
    ptr\_p\_c\[1\] = new char;
    strcpy (ptr\_p\_c\[1\], "whatever");
    
    delete ptr\_p\_c;
    

    I know I shouldn't be using this type for strings and all this. Believe me, I've tried repeatedly to convince my customer this is not the right way to do things, but unfortunately it is not my decission... Thanks a lot.

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

    piul wrote:

    Believe me, I've tried repeatedly to convince my customer this is not the right way to do things, but unfortunately it is not my decission...

    If you are a professional, then MAKE IT your decision. if it takes less time to develop, less time to debug, make the code more secure and readable, than you need to do it the right way. anyway,you should delete each item in the ptr_p_c array. good luck.

    Watched code never compiles.

    1 Reply Last reply
    0
    • P piul

      Hi there! I need an array of strings of char * type and I am not clear about how to free up memory after use. The code below does not work.

      char \* ptr\_p\_c\[3\];
      ptr\_p\_c\[0\] = new char;
      strcpy (ptr\_p\_c\[0\], "hi there");
      ptr\_p\_c\[1\] = new char;
      strcpy (ptr\_p\_c\[1\], "whatever");
      
      delete ptr\_p\_c;
      

      I know I shouldn't be using this type for strings and all this. Believe me, I've tried repeatedly to convince my customer this is not the right way to do things, but unfortunately it is not my decission... Thanks a lot.

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

      piul wrote:

      Believe me, I've tried repeatedly to convince my customer this is not the right way to do things

      Why does the customer need to know how you are implementing a small piece of code? If they understand that level of detail then why do they need you?

      Just say 'NO' to evaluated arguments for diadic functions! Ash

      C 1 Reply Last reply
      0
      • P piul

        Hi there! I need an array of strings of char * type and I am not clear about how to free up memory after use. The code below does not work.

        char \* ptr\_p\_c\[3\];
        ptr\_p\_c\[0\] = new char;
        strcpy (ptr\_p\_c\[0\], "hi there");
        ptr\_p\_c\[1\] = new char;
        strcpy (ptr\_p\_c\[1\], "whatever");
        
        delete ptr\_p\_c;
        

        I know I shouldn't be using this type for strings and all this. Believe me, I've tried repeatedly to convince my customer this is not the right way to do things, but unfortunately it is not my decission... Thanks a lot.

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

        I know dumb customers. I also know dumber customers. But this is the dumbest customer.

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

        _Microsoft MVP (Visual C++)

        Polymorphism in C

        C L 2 Replies Last reply
        0
        • L Lost User

          piul wrote:

          Believe me, I've tried repeatedly to convince my customer this is not the right way to do things

          Why does the customer need to know how you are implementing a small piece of code? If they understand that level of detail then why do they need you?

          Just say 'NO' to evaluated arguments for diadic functions! Ash

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

          Because he's afraid of using new for instancing strings. :rolleyes:

          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]

          1 Reply Last reply
          0
          • _ _Superman_

            I know dumb customers. I also know dumber customers. But this is the dumbest customer.

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

            _Microsoft MVP (Visual C++)

            Polymorphism in C

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

            You know dumb customers. I know dumb customers. He knows dumb customers. Hey Superman, your customers. Hey Superman, YOUR customers are just not that dumb. :-D

            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]

            1 Reply Last reply
            0
            • _ _Superman_

              I know dumb customers. I also know dumber customers. But this is the dumbest customer.

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

              _Microsoft MVP (Visual C++)

              Polymorphism in C

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

              So dumb customers are your kryptonite, eh?

              ...byte till it megahertz... my donation to web rubbish

              _ 1 Reply Last reply
              0
              • L Lost User

                So dumb customers are your kryptonite, eh?

                ...byte till it megahertz... my donation to web rubbish

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

                A good reason to dislike them. :)

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

                _Microsoft MVP (Visual C++)

                Polymorphism in C

                1 Reply Last reply
                0
                • C CPallini

                  Well, you already know that avoiding std::string is just plain silly so I don't bother you more:

                  char * ptr_p_c[2];
                  ptr_p_c[0] = new char[sizeof("hi there")];
                  strcpy (ptr_p_c[0], "hi there");
                  ptr_p_c[1] = new char[sizeof("whatever")];
                  strcpy (ptr_p_c[1], "whatever");
                  delete ptr_p_c[0];
                  delete ptr_p_c[1];

                  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]

                  A Offline
                  A Offline
                  Alan Balkany
                  wrote on last edited by
                  #15

                  It looks like your sample is an example of why std:::string should be used: The null terminator will overflow the char[] buffers you allocated by one byte.

                  C 1 Reply Last reply
                  0
                  • A Alan Balkany

                    It looks like your sample is an example of why std:::string should be used: The null terminator will overflow the char[] buffers you allocated by one byte.

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

                    You're wrong. Hint: I didn't use strlen. :)

                    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]

                    A 1 Reply Last reply
                    0
                    • C CPallini

                      You're wrong. Hint: I didn't use strlen. :)

                      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]

                      A Offline
                      A Offline
                      Alan Balkany
                      wrote on last edited by
                      #17

                      Oops, I stand corrected.

                      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