C++ problem (regarding char*)

CTho9305

Elite Member
Jul 26, 2000
9,214
1
81
the program prints "read first letter", and then stops. it doesn't print "wrote first letter". it doesn't give me any useful info :(
threadNum is a char* that reads "1&threadID=555". any idea whats wrong?

some code:
{
char* fname;
cout << "generating name" << endl;
char temp = threadNum[0];
cout << "read first letter" << endl;
fname[0] = temp;
cout << "wrote first letter" << endl;
}
 

CTho9305

Elite Member
Jul 26, 2000
9,214
1
81


<< That should say fname = temp. >>


no, that wont (doesn't) compile. temp is a char, fname is a char*. changing the line to

*fname = temp;

has the same effect - the program seems to stop right there.
 

CSoup

Senior member
Jan 9, 2002
565
0
0
don't have a c compiler on me, so did not run your code, but one problem I see is you delcare char * fname and then later do fname[0] = temp, but nowhere between those lines did you allocate memory (ie use new or malloc). basically your code currently is assigning into random locations in memory which could be your problem.
 

CTho9305

Elite Member
Jul 26, 2000
9,214
1
81


<< don't have a c compiler on me, so did not run your code, but one problem I see is you delcare char * fname and then later do fname[0] = temp, but nowhere between those lines did you allocate memory (ie use new or malloc). basically your code currently is assigning into random locations in memory which could be your problem. >>



yeah, i figured that was bad. how do I allocate it properly? i've never used malloc... if I do a "char* fname = new char[LEN]" that will give me up to LEN bytes to use, right? of course, then I have to "delete fname" at the end of the function. evidently using java resulted in me forgetting memory management :(
 

CSoup

Senior member
Jan 9, 2002
565
0
0
yes you can use new like that which is how you would do it in C++ and then do a "delete [] fname" to free the memory. To use malloc you would do something like "char * fname = malloc(LEN * sizeof(char));" where LEN is how many chars of memory you want to allocate. Then to free the memory you would do "free(fname)".
 

CTho9305

Elite Member
Jul 26, 2000
9,214
1
81


<< yes you can use new like that which is how you would do it in C++ and then do a "delete [] fname" to free the memory. To use malloc you would do something like "char * fname = malloc(LEN * sizeof(char));" where LEN is how many chars of memory you want to allocate. Then to free the memory you would do "free(fname)". >>



thanks a lot for your help :)
 

Brian23

Banned
Dec 28, 1999
1,655
1
0
What's the difference between:
char* fname;
char fname;
and useing those malloc and free functions?

I've only written a few C++ programs in the past 2 months since I started learning it. In the book I bought it says to declare a sting like this:

char fname[5]="Text";
 

CSoup

Senior member
Jan 9, 2002
565
0
0


<< What's the difference between:
char* fname;
char fname;
and useing those malloc and free functions?

I've only written a few C++ programs in the past 2 months since I started learning it. In the book I bought it says to declare a sting like this:

char fname[5]="Text";
>>



char * fname; declares fname to be a pointer to a char type. You then point it to some piece of memory (hopefully allocated for use using new or malloc).

char fname; declares either a global or stack variable of type char depending on whether it is inside or outside a function declaration.

char fname[5]="Text"; declares an array of 5 chars. This piece of memory is either in the data segment or the stack depending on if it is declared outside or inside a function.

Global declarations are destroyed when the program exits. Stack variables are destroyed when the function exits. Memory allocated through malloc or new should to be freed by the user with a call to free or delete respectively.