1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

When NULL When Not, In C....

Discussion in 'C' started by danielakkerman, Dec 25, 2006.

  1. danielakkerman

    danielakkerman New Member

    Joined:
    Dec 25, 2006
    Messages:
    9
    Likes Received:
    0
    Trophy Points:
    0
    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
     
  2. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    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.
     
  3. Aztec

    Aztec New Member

    Joined:
    May 9, 2006
    Messages:
    90
    Likes Received:
    0
    Trophy Points:
    0
    You don't even need to cast return value of malloc in C.
     
  4. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,283
    Likes Received:
    364
    Trophy Points:
    83
    Its always a good practice though.
     
  5. Aztec

    Aztec New Member

    Joined:
    May 9, 2006
    Messages:
    90
    Likes Received:
    0
    Trophy Points:
    0
    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++.
     
  6. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,283
    Likes Received:
    364
    Trophy Points:
    83
    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.
     
  7. Aztec

    Aztec New Member

    Joined:
    May 9, 2006
    Messages:
    90
    Likes Received:
    0
    Trophy Points:
    0
    Read this
     
  8. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,283
    Likes Received:
    364
    Trophy Points:
    83
    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.
     
  9. Aztec

    Aztec New Member

    Joined:
    May 9, 2006
    Messages:
    90
    Likes Received:
    0
    Trophy Points:
    0
    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.
     
  10. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,283
    Likes Received:
    364
    Trophy Points:
    83
    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.
     
  11. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    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.
     
  12. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,283
    Likes Received:
    364
    Trophy Points:
    83
    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.
     
  13. Aztec

    Aztec New Member

    Joined:
    May 9, 2006
    Messages:
    90
    Likes Received:
    0
    Trophy Points:
    0
  14. Jamkirk

    Jamkirk New Member

    Joined:
    Dec 27, 2006
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    it could be alot simpler a problem...
    try:
    char *x = "C-PROGRAMMING";
    char *Prog = (char *)malloc(strlen(14));
     
  15. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,283
    Likes Received:
    364
    Trophy Points:
    83
    The online compiler gives error when not type casted.
     

Share This Page