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. char ** initialization and store values

char ** initialization and store values

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
5 Posts 4 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.
  • T Offline
    T Offline
    tagopi
    wrote on last edited by
    #1

    Hello Everybody, I am trying to pass some values to char ** (to a third party library). But i am getting some issue in that.

        char\*\* ids = new char\*\[m\_Size\];
        for(int i=0; i
    

    using that third party function, its creating an output file and in that file the stored value is showing like this.

    GROUP : 46,0
    GROUP : 44,0
    GROUP : 42,0
    GROUP : ÝÝÝÝÝÝÝ݆«ãe,0
    GROUP : 40,0
    GROUP : 38,0

    But if I give values like this, then in output file, the values are showing fine.

    char* ids[6] = {"38", "40", "42", "44", "46", "48"};

    in the output file,

    GROUP : 38,0
    GROUP : 40,0
    GROUP : 42,0
    GROUP : 44,0
    GROUP : 46,0
    GROUP : 48,0

    (see the sequence also)

    what i am doing wrong? initialization ? or passing values ?

    Thanks,
    A. Gopinath.

    J L _ 3 Replies Last reply
    0
    • T tagopi

      Hello Everybody, I am trying to pass some values to char ** (to a third party library). But i am getting some issue in that.

          char\*\* ids = new char\*\[m\_Size\];
          for(int i=0; i
      

      using that third party function, its creating an output file and in that file the stored value is showing like this.

      GROUP : 46,0
      GROUP : 44,0
      GROUP : 42,0
      GROUP : ÝÝÝÝÝÝÝ݆«ãe,0
      GROUP : 40,0
      GROUP : 38,0

      But if I give values like this, then in output file, the values are showing fine.

      char* ids[6] = {"38", "40", "42", "44", "46", "48"};

      in the output file,

      GROUP : 38,0
      GROUP : 40,0
      GROUP : 42,0
      GROUP : 44,0
      GROUP : 46,0
      GROUP : 48,0

      (see the sequence also)

      what i am doing wrong? initialization ? or passing values ?

      Thanks,
      A. Gopinath.

      J Offline
      J Offline
      Jochen Arndt
      wrote on last edited by
      #2

      The code looks fine. Because only one of the strings from your array returns wrong data, this element may be corrupted somewhere else. Note also that you must call ReleaseBuffer() for each string array element after calling the library function. If not doing so, further accesses may return invalid data. You should also check the documentation of the library if the passed strings are constant or may be changed. If they may be changed, you must pass the max. allowed length to GetBuffer().

      1 Reply Last reply
      0
      • T tagopi

        Hello Everybody, I am trying to pass some values to char ** (to a third party library). But i am getting some issue in that.

            char\*\* ids = new char\*\[m\_Size\];
            for(int i=0; i
        

        using that third party function, its creating an output file and in that file the stored value is showing like this.

        GROUP : 46,0
        GROUP : 44,0
        GROUP : 42,0
        GROUP : ÝÝÝÝÝÝÝ݆«ãe,0
        GROUP : 40,0
        GROUP : 38,0

        But if I give values like this, then in output file, the values are showing fine.

        char* ids[6] = {"38", "40", "42", "44", "46", "48"};

        in the output file,

        GROUP : 38,0
        GROUP : 40,0
        GROUP : 42,0
        GROUP : 44,0
        GROUP : 46,0
        GROUP : 48,0

        (see the sequence also)

        what i am doing wrong? initialization ? or passing values ?

        Thanks,
        A. Gopinath.

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

        char** ids = new char*[m_Size]; Here you create an array of char pointers. Here: ids[i] = you need to create an aray of chars, so it should be ids[i] = new char [] Then copy the data into the char array.

        ============================== Nothing to say.

        1 Reply Last reply
        0
        • T tagopi

          Hello Everybody, I am trying to pass some values to char ** (to a third party library). But i am getting some issue in that.

              char\*\* ids = new char\*\[m\_Size\];
              for(int i=0; i
          

          using that third party function, its creating an output file and in that file the stored value is showing like this.

          GROUP : 46,0
          GROUP : 44,0
          GROUP : 42,0
          GROUP : ÝÝÝÝÝÝÝ݆«ãe,0
          GROUP : 40,0
          GROUP : 38,0

          But if I give values like this, then in output file, the values are showing fine.

          char* ids[6] = {"38", "40", "42", "44", "46", "48"};

          in the output file,

          GROUP : 38,0
          GROUP : 40,0
          GROUP : 42,0
          GROUP : 44,0
          GROUP : 46,0
          GROUP : 48,0

          (see the sequence also)

          what i am doing wrong? initialization ? or passing values ?

          Thanks,
          A. Gopinath.

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

          In the first case, the pointers are pointing to memory maintained by the CStringArray class. It is likely that CStringArray relocated, resized or moved the memory around. So you need to allocate memory for each pointer and copy the contents to the allocated memory. In the second case you have pointers to fixed locations that do not change.

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

          _Microsoft MVP (Visual C++) (October 2009 - September 2013)

          Polymorphism in C

          L 1 Reply Last reply
          0
          • _ _Superman_

            In the first case, the pointers are pointing to memory maintained by the CStringArray class. It is likely that CStringArray relocated, resized or moved the memory around. So you need to allocate memory for each pointer and copy the contents to the allocated memory. In the second case you have pointers to fixed locations that do not change.

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

            _Microsoft MVP (Visual C++) (October 2009 - September 2013)

            Polymorphism in C

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

            I am aurpsised it even runs with out crashing since he is setting his array pointers to the values contained in some string! So if the string was 'this monkley is stupid' the first pointer will have the value of the ASCII codes for 'this', the second pointer ' mon' and so on. Of course these addresses are completely invalid and point to somewhere in his process. If he manipulated them in any way its bye bye process time!

            ============================== Nothing to say.

            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