Hello, I am declearing the Following Variable: Code: char *x = "C-PROGRAMMING"; char *Prog = (char *)malloc(strlen(x)); Afterwards, I only insert several letters(chars) into Prog, Leaveing The most part blank. Except that instead of declaring the spaces as NULL, it starts to present me with hearts and lines and what not, everything except 0(NULL). This is how I am assigning the letters to Prog: Code: for(int i = 0;i<strlen(y);i++){ for(int i1 = 0;i1<strlen(x);i1++){ if(x[i1] == toupper(y[i]) && x[i] != x[R_N]) {Prog[i1] = x[i1];} // Count1++; //else{Prog[i1] = 0; //} } } Why is this caused? Thank you for your attention, Thanks Daniel
Code: char *Prog = (char *)malloc(strlen(x)); That part is wrong. It gives you enough characters to hold the string, but not the terminating nul byte ('\0'). You may not be planning to use them all, but your code indicates otherwise by its reference to x. Be careful with your terminology. NULL generally refers to the value representing a NULL pointer, which may or may not actually be zero (though a zero has to act as a NULL in a pointer context). The value, 0, for a char is referred to as a nul, or as '\0', to reduce confusion. Your example code uses a lot of stuff you haven't bothered to define in our sight. Since we only read minds on every other Wednesday, that's not too effective.
NO, It's not a good practise at all. You don't need to cast result of malloc in C. Sometimes people do it to maintain compatibility between C and C++ code but then again there are very less situations when you will use malloc in C++.
Why you think its not a good practice. If you dont type cast it then compiler will do that for you and its always better to be doing it yourself rather than leave it upto the compilers.
I dont totally agree on the issues. I am not challenging the issue but out of interest I would like to know. Still not sure of what bug its talking about. If I maintain the consistency of type casting everytime this should never come in the scenario.
Try Code: int main(){ int *ptr=malloc(10); return 0; } as well as this Code: int main(){ int *ptr=(int*)malloc(10); return 0; } In the first case you should get an error which is hidden in second case. That's what the FAQ is talking about. Most of the time the maintainer of the code is not the original coder. Even if the person is same it's very difficult to keep track of such things in a projects of any appreciable size.
Basically I work on MFC and Win Platform where the code is compile time error Code: int main(){ int *ptr=malloc(10); return 0; } and so never came across such hidden error. Thanks for clearing the doubts.
Personal outlook: don't cast the return of malloc, in C (don't use malloc in C++). In any project that isn't trivial, the probability of correctness is greater if one relies on the compiler.
I just made one more try for the followin. When you have the file as .c in MS compiler for the code Code: int main(){ int *ptr=malloc(10); return 0; } but it gives when the file name is a .cpp file but with the .c file it does not run into any error for the code when executed. That is could not run into the hidden error.
it could be alot simpler a problem... try: char *x = "C-PROGRAMMING"; char *Prog = (char *)malloc(strlen(14));