المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : Dynamic char Array



C++ Programming
07-02-2009, 08:11 PM
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 al******** 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.