When NULL When Not, In C....

danielakkerman's Avatar, Join Date: Dec 2006
Light Poster
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
0
DaWei's Avatar, Join Date: Dec 2006
Team Leader
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.
0
Aztec's Avatar, Join Date: May 2006
Contributor
Quote:
Originally Posted by danielakkerman
Code:
char *x = "C-PROGRAMMING";
char *Prog = (char *)malloc(strlen(x));
You don't even need to cast return value of malloc in C.
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Quote:
Originally Posted by Aztec
You don't even need to cast return value of malloc in C.
Its always a good practice though.
0
Aztec's Avatar, Join Date: May 2006
Contributor
Quote:
Originally Posted by shabbir
Its always a good practice though.
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++.
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Quote:
Originally Posted by Aztec
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.
0
Aztec's Avatar, Join Date: May 2006
Contributor
Quote:
Originally Posted by shabbir
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.
Read this
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Quote:
Originally Posted by Aztec
Read this
I dont totally agree on the issues. I am not challenging the issue but out of interest I would like to know.

Quote:
If the return of malloc is cast then the error which would be flagged is hidden, resulting in a difficult to find bug.
Still not sure of what bug its talking about.
Quote:
Also, during maintenance, if the type of the pointer changes but the cast is not changed, once again there is a difficult to find bug.
If I maintain the consistency of type casting everytime this should never come in the scenario.
0
Aztec's Avatar, Join Date: May 2006
Contributor
Quote:
Originally Posted by shabbir
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.
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.
Quote:
Originally Posted by shabbir
If I maintain the consistency of type casting everytime this should never come in the scenario.
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.
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
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.