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. Dynamic char Array

Dynamic char Array

Scheduled Pinned Locked Moved C / C++ / MFC
helpdata-structures
7 Posts 3 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.
  • J Offline
    J Offline
    jlgeris
    wrote on last edited by
    #1

    Essentially, I'm reading some names from a text file, and I need to allocate the array at runtime. I'm missing something regarding the allocation of each individual name. After I copy the first name into the array slot, it fills up every spot in the array with the first value. After the loop finishes I have an array with [count] copies of the initial value I read from the file. I'm using the StreamReader class in other places in the program so that's why I'm marshaling the string to a char*. Maybe not the ideal solution, but I believe the problem is separate from that.

    char **temp=NULL;
    my_malloc((void**)&temp,count*sizeof(char*));

    StreamReader^ din = File::OpenText(fileName);
    String^ str;

    while ((str = din->ReadLine()) != nullptr)
    {
    char* x = (char*)System::Runtime::InteropServices::Marshal::StringToHGlobalAnsi(str).ToPointer();
    my_malloc((void**)&temp[count],strlen(x)*sizeof(char)+1); //i imagine the problem is here
    strcpy(temp[count],x); //or here
    System::Runtime::InteropServices::Marshal::FreeHGlobal((IntPtr)x);
    count++;
    }

    The my_malloc function is just a wrapper around free() and malloc(). Thanks for the help.

    J _ S 3 Replies Last reply
    0
    • J jlgeris

      Essentially, I'm reading some names from a text file, and I need to allocate the array at runtime. I'm missing something regarding the allocation of each individual name. After I copy the first name into the array slot, it fills up every spot in the array with the first value. After the loop finishes I have an array with [count] copies of the initial value I read from the file. I'm using the StreamReader class in other places in the program so that's why I'm marshaling the string to a char*. Maybe not the ideal solution, but I believe the problem is separate from that.

      char **temp=NULL;
      my_malloc((void**)&temp,count*sizeof(char*));

      StreamReader^ din = File::OpenText(fileName);
      String^ str;

      while ((str = din->ReadLine()) != nullptr)
      {
      char* x = (char*)System::Runtime::InteropServices::Marshal::StringToHGlobalAnsi(str).ToPointer();
      my_malloc((void**)&temp[count],strlen(x)*sizeof(char)+1); //i imagine the problem is here
      strcpy(temp[count],x); //or here
      System::Runtime::InteropServices::Marshal::FreeHGlobal((IntPtr)x);
      count++;
      }

      The my_malloc function is just a wrapper around free() and malloc(). Thanks for the help.

      J Offline
      J Offline
      jlgeris
      wrote on last edited by
      #2

      FYI

      	int my\_malloc(void \*\*param\_adr,	int param\_size)
      	{
      		my\_free(param\_adr);
      		if ((\*param\_adr = malloc(param\_size)) == NULL)
      		{				
      			return -1;
      		}
      		memset(\*param\_adr,0,param\_size);			
      		
      		return 0;
      	}
      
      	void my\_free(void \*\*param\_adr)
      	{
      		if (\*param\_adr != NULL)
      		{
      			free(\*param\_adr);
      			\*param\_adr = NULL;
      		}
      		return;
      	}
      
      1 Reply Last reply
      0
      • J jlgeris

        Essentially, I'm reading some names from a text file, and I need to allocate the array at runtime. I'm missing something regarding the allocation of each individual name. After I copy the first name into the array slot, it fills up every spot in the array with the first value. After the loop finishes I have an array with [count] copies of the initial value I read from the file. I'm using the StreamReader class in other places in the program so that's why I'm marshaling the string to a char*. Maybe not the ideal solution, but I believe the problem is separate from that.

        char **temp=NULL;
        my_malloc((void**)&temp,count*sizeof(char*));

        StreamReader^ din = File::OpenText(fileName);
        String^ str;

        while ((str = din->ReadLine()) != nullptr)
        {
        char* x = (char*)System::Runtime::InteropServices::Marshal::StringToHGlobalAnsi(str).ToPointer();
        my_malloc((void**)&temp[count],strlen(x)*sizeof(char)+1); //i imagine the problem is here
        strcpy(temp[count],x); //or here
        System::Runtime::InteropServices::Marshal::FreeHGlobal((IntPtr)x);
        count++;
        }

        The my_malloc function is just a wrapper around free() and malloc(). Thanks for the help.

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

        You're mixing managed and unmanaged code in a very strange way.

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

        J 1 Reply Last reply
        0
        • _ _Superman_

          You're mixing managed and unmanaged code in a very strange way.

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

          J Offline
          J Offline
          jlgeris
          wrote on last edited by
          #4

          I know. This was the quick and dirty solution, but i'm open to alternatives.

          S 1 Reply Last reply
          0
          • J jlgeris

            I know. This was the quick and dirty solution, but i'm open to alternatives.

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

            How about using an [array](http://msdn.microsoft.com/en-us/library/ts4c4dw6\(VS.80\).aspx)<[String](http://msdn.microsoft.com/en-us/library/system.string\(VS.80\).aspx)^>^ (i.e. a C++/CLI array of C++/CLI strings) instead of standard C types?

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

            J 1 Reply Last reply
            0
            • S Stuart Dootson

              How about using an [array](http://msdn.microsoft.com/en-us/library/ts4c4dw6\(VS.80\).aspx)<[String](http://msdn.microsoft.com/en-us/library/system.string\(VS.80\).aspx)^>^ (i.e. a C++/CLI array of C++/CLI strings) instead of standard C types?

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

              J Offline
              J Offline
              jlgeris
              wrote on last edited by
              #6

              Well, I need the array in that format because I'm working with another proprietary API that works only with standard C types.

              1 Reply Last reply
              0
              • J jlgeris

                Essentially, I'm reading some names from a text file, and I need to allocate the array at runtime. I'm missing something regarding the allocation of each individual name. After I copy the first name into the array slot, it fills up every spot in the array with the first value. After the loop finishes I have an array with [count] copies of the initial value I read from the file. I'm using the StreamReader class in other places in the program so that's why I'm marshaling the string to a char*. Maybe not the ideal solution, but I believe the problem is separate from that.

                char **temp=NULL;
                my_malloc((void**)&temp,count*sizeof(char*));

                StreamReader^ din = File::OpenText(fileName);
                String^ str;

                while ((str = din->ReadLine()) != nullptr)
                {
                char* x = (char*)System::Runtime::InteropServices::Marshal::StringToHGlobalAnsi(str).ToPointer();
                my_malloc((void**)&temp[count],strlen(x)*sizeof(char)+1); //i imagine the problem is here
                strcpy(temp[count],x); //or here
                System::Runtime::InteropServices::Marshal::FreeHGlobal((IntPtr)x);
                count++;
                }

                The my_malloc function is just a wrapper around free() and malloc(). Thanks for the help.

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

                jlgeris wrote:

                temp[count]

                When you allocated temp, you allocated temp[0] to temp[count-1] - temp[count] is undefined. Either use a different index variable or reset count to 0

                jlgeris wrote:

                strlen(x)*sizeof(char)+1

                As sizeof(char)==1, it makes no difference, but you really wanted (strlen(x)+1)*sizeof(char)

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

                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