Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   When NULL When Not, In C.... (http://www.go4expert.com/forums/null-c-t2338/)

danielakkerman 25Dec2006 22:05

When NULL When Not, In C....
 
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

DaWei 26Dec2006 08:14

Re: When NULL When Not, In C....
 
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.

Aztec 26Dec2006 08:47

Re: When NULL When Not, In C....
 
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.

shabbir 26Dec2006 09:41

Re: When NULL When Not, In C....
 
Quote:

Originally Posted by Aztec
You don't even need to cast return value of malloc in C.

Its always a good practice though.

Aztec 26Dec2006 11:42

Re: When NULL When Not, In C....
 
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++.

shabbir 26Dec2006 12:28

Re: When NULL When Not, In C....
 
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.

Aztec 26Dec2006 13:59

Re: When NULL When Not, In C....
 
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

shabbir 26Dec2006 15:26

Re: When NULL When Not, In C....
 
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.

Aztec 26Dec2006 17:49

Re: When NULL When Not, In C....
 
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.

shabbir 26Dec2006 18:02

Re: When NULL When Not, In C....
 
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.


All times are GMT +5.5. The time now is 04:09.