• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

C++ problem (regarding char*)

CTho9305

Elite Member
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;
}
 


<< 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.
 
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.
 


<< 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 🙁
 
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)".
 


<< 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 🙂
 
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";
 


<< 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.

 
Back
Top