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 Offline
    P Offline
    piul
    wrote on last edited by
    #1

    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 C D M _ 6 Replies 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
      #2

      First up, the "customer" has no business wrt such low level programming details. I smell something else here. Anyway, to the point, the allocation itself is flawed. You are allocating just one character and copying several characters into it. A lot of corrupted memory. The delete will cry. The proper way to delete is to loop through the array and delete every allocated sub array.

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

      P 1 Reply Last reply
      0
      • L Lost User

        First up, the "customer" has no business wrt such low level programming details. I smell something else here. Anyway, to the point, the allocation itself is flawed. You are allocating just one character and copying several characters into it. A lot of corrupted memory. The delete will cry. The proper way to delete is to loop through the array and delete every allocated sub array.

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

        P Offline
        P Offline
        piul
        wrote on last edited by
        #3

        Something like this??

        const int num = 3;
        char \* ptr\_p\_c\[num\];
        for (int i = 0; i<num; i++)
        	ptr\_p\_c\[i\] = new char\[20\];
        
        strcpy\_s (ptr\_p\_c\[0\], 9, "hi there");
        strcpy\_s (ptr\_p\_c\[1\], 7, "change");
        
        for (int i = 0; i<num; i++)
        	delete ptr\_p\_c\[i\];
        
        L 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.

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

          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]

          T A 2 Replies Last reply
          0
          • P piul

            Something like this??

            const int num = 3;
            char \* ptr\_p\_c\[num\];
            for (int i = 0; i<num; i++)
            	ptr\_p\_c\[i\] = new char\[20\];
            
            strcpy\_s (ptr\_p\_c\[0\], 9, "hi there");
            strcpy\_s (ptr\_p\_c\[1\], 7, "change");
            
            for (int i = 0; i<num; i++)
            	delete ptr\_p\_c\[i\];
            
            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            better

            const int N = ??
            const char* names[N] = {"...", "...", ...}
            char *arr[N];
            for(n=0;n
            ...byte till it megahertz...

            my donation to web rubbish

            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]

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

              Agreed. Use std::string or you can use one of many already existing string classes. You also could create an array of char[] if you have a series of strings whose length the compiler can computebv in advance.

              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.

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

                Does the memory for those strings necessarily need to come from the heap? If not, what about:

                char *ptr_p_c[] = { "hi there", "whatever" };

                "One man's wage rise is another man's price increase." - Harold Wilson

                "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

                "Man who follows car will be exhausted." - Confucius

                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.

                  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