char* pname = "Harry"

Discussion in 'C Programming' started by erktek@gmail.com, Apr 18, 2006.

  1. Guest

    Statement given below;

    char * p ;

    p = (char*) malloc(20);

    p = "harry" ; // <------------- Is it perfectly valid ?

    free(p);
     
    , Apr 18, 2006
    #1
    1. Advertising

  2. Ian Collins Guest

    wrote:
    > Statement given below;
    >
    > char * p ;
    >
    > p = (char*) malloc(20);


    You don't have to cast.
    >
    > p = "harry" ; // <------------- Is it perfectly valid ?
    >

    Valid, but it doesn't do what you expect, it changes p to point to the
    string literal "harry" rather than the memory returned by malloc.

    > free(p);
    >

    Bad, you are attempting to free "harry".

    --
    Ian Collins.
     
    Ian Collins, Apr 18, 2006
    #2
    1. Advertising

  3. John Bode Guest

    wrote:
    > Statement given below;
    >
    > char * p ;
    >
    > p = (char*) malloc(20);


    Lose the cast, unless you're working with a *very* old implementation
    (pre-C89).

    >
    > p = "harry" ; // <------------- Is it perfectly valid ?


    Perfectly valid, but not correct, given the context. Instead of
    copying the contents of the string "harry" to the memory pointed to by
    p, you've assigned the address of the string literal "harry" to p,
    causing you to lose track of the memory you just allocated, which is a
    memory leak.

    Try

    strcpy(p, "harry");

    instead. Don't forget to #include <string.h>.

    >
    > free(p);


    This will attempt to free the string literal "harry", not the memory
    you allocated earlier.
     
    John Bode, Apr 18, 2006
    #3
  4. Guest

    John Bode wrote:
    > wrote:
    > > Statement given below;
    > >
    > > char * p ;
    > >
    > > p = (char*) malloc(20);

    >
    > Lose the cast, unless you're working with a *very* old implementation
    > (pre-C89).

    [snip]
    Or the code should be usable with both C and C++.

    /Peter
     
    , Apr 18, 2006
    #4
  5. Flash Gordon Guest

    wrote:
    > John Bode wrote:
    >> wrote:
    >>> Statement given below;
    >>>
    >>> char * p ;
    >>>
    >>> p = (char*) malloc(20);

    >> Lose the cast, unless you're working with a *very* old implementation
    >> (pre-C89).

    > [snip]
    > Or the code should be usable with both C and C++.


    Very few people have a good reason for doing that. C++ defines a method
    of calling C from C++ and in general you would be better off using that
    or selecting just one language and using it.
    --
    Flash Gordon, living in interesting times.
    Web site - http://home.flash-gordon.me.uk/
    comp.lang.c posting guidelines and intro:
    http://clc-wiki.net/wiki/Intro_to_clc
     
    Flash Gordon, Apr 18, 2006
    #5
  6. wrote:
    > John Bode wrote:
    >
    >> wrote:
    >>
    >>>Statement given below;
    >>>
    >>> char * p ;
    >>>
    >>> p = (char*) malloc(20);

    >>
    >>Lose the cast, unless you're working with a *very* old implementation
    >>(pre-C89).

    >
    > [snip]
    > Or the code should be usable with both C and C++.


    Continuing with your off-topic statement, for which there is no excuse.
    Idiomatic C Code is "usable with both C and C++." C++ has defined
    methods for linking C code written without regard to C++ arcane
    requirements, so there is never a reason to pervert the normal C idiom
    to "be usable with both C and C++." I thought we lost the trolls that
    kept beating this horse.
     
    Martin Ambuhl, Apr 18, 2006
    #6
  7. Simon Biber Guest

    John Bode wrote:
    > wrote:
    >
    >>Statement given below;
    >>
    >> char * p ;
    >>
    >> p = (char*) malloc(20);

    >
    >
    > Lose the cast, unless you're working with a *very* old implementation
    > (pre-C89).


    A pre-C89 implementation of malloc would return char* anyway, as void
    had not yet been introduced. So, the cast is still unnecessary.

    Simon.
     
    Simon Biber, Apr 18, 2006
    #7
  8. Martin Ambuhl <> writes:
    > wrote:

    [...]
    >> Or the code should be usable with both C and C++.

    >
    > Continuing with your off-topic statement, for which there is no excuse.
    > Idiomatic C Code is "usable with both C and C++." C++ has defined
    > methods for linking C code written without regard to C++ arcane
    > requirements, so there is never a reason to pervert the normal C idiom
    > to "be usable with both C and C++." I thought we lost the trolls that
    > kept beating this horse.


    I think you dropped a "not", i.e.,

    Idiomatic C Code is *not* "usable with both C and C++."

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Apr 18, 2006
    #8
  9. Keith Thompson wrote:
    > Martin Ambuhl <> writes:
    >
    >> wrote:

    >
    > [...]
    >
    >>>Or the code should be usable with both C and C++.

    >>
    >>Continuing with your off-topic statement, for which there is no excuse.
    >>Idiomatic C Code is "usable with both C and C++." C++ has defined
    >>methods for linking C code written without regard to C++ arcane
    >>requirements, so there is never a reason to pervert the normal C idiom
    >>to "be usable with both C and C++." I thought we lost the trolls that
    >>kept beating this horse.

    >
    >
    > I think you dropped a "not", i.e.,
    >
    > Idiomatic C Code is *not* "usable with both C and C++."


    I did not drop a "not". For the C++ mechanism for linking to pure
    idiomatic C, go to the correct newsgroup, <news:comp.lang.c++>. Why in
    the world you thought I dropped a "not" is between you and your God.
     
    Martin Ambuhl, Apr 18, 2006
    #9
  10. Martin Ambuhl <> writes:
    > Keith Thompson wrote:
    >> Martin Ambuhl <> writes:
    >>
    >>> wrote:

    >> [...]
    >>
    >>>>Or the code should be usable with both C and C++.
    >>>
    >>>Continuing with your off-topic statement, for which there is no excuse.
    >>>Idiomatic C Code is "usable with both C and C++." C++ has defined
    >>>methods for linking C code written without regard to C++ arcane
    >>>requirements, so there is never a reason to pervert the normal C idiom
    >>>to "be usable with both C and C++." I thought we lost the trolls that
    >>>kept beating this horse.

    >> I think you dropped a "not", i.e., Idiomatic C Code is *not*
    >> "usable with both C and C++."

    >
    > I did not drop a "not". For the C++ mechanism for linking to pure
    > idiomatic C, go to the correct newsgroup, <news:comp.lang.c++>. Why
    > in the world you thought I dropped a "not" is between you and your God.


    Sorry, I misunderstood. I thought that you meant to say that
    idiomatic C code is not valid C++ code. It should have been clear to
    me from the context that C code is *usable* by C++ code via C++'s
    interfacing mechanism.

    (And perhaps you could have pointed out my error without getting quite
    so personal about it.)

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Apr 18, 2006
    #10
  11. John Bode Guest

    wrote:
    > John Bode wrote:
    > > wrote:
    > > > Statement given below;
    > > >
    > > > char * p ;
    > > >
    > > > p = (char*) malloc(20);

    > >
    > > Lose the cast, unless you're working with a *very* old implementation
    > > (pre-C89).

    > [snip]
    > Or the code should be usable with both C and C++.
    >
    > /Peter


    IME, there are damned few cases where it makes sense to have code
    compile as both C and C++; memory management is *not* one of those
    cases. Of course, resource management should be abstracted out in the
    first place, but the OP doesn't appear to be that far along yet.
     
    John Bode, Apr 18, 2006
    #11
  12. On Tue, 18 Apr 2006 19:55:34 GMT, in comp.lang.c , Martin Ambuhl
    <> wrote:

    >Keith Thompson wrote:
    >> Martin Ambuhl <> writes:
    >>
    >>>Idiomatic C Code is "usable with both C and C++."

    >>
    >> I think you dropped a "not", i.e.,
    >>
    >> Idiomatic C Code is *not* "usable with both C and C++."

    >
    >I did not drop a "not".

    <snip>
    > Why in the world you thought I dropped a "not" is between you and your God.


    Well, frankly I also thought you dropped a "not" and I'm not convinced
    there was a call to be quite so rude. Idiomatic C can't be used in C++
    unless you take great care to avoid all sorts of trivial C-isms and
    add in extra C++ specific stuff.

    Mark McIntyre
    --
    "Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are,
    by definition, not smart enough to debug it."
    --Brian Kernighan
     
    Mark McIntyre, Apr 18, 2006
    #12
  13. Jack Klein Guest

    On 18 Apr 2006 04:55:11 -0700, "John Bode" <>
    wrote in comp.lang.c:

    >
    > wrote:
    > > Statement given below;
    > >
    > > char * p ;
    > >
    > > p = (char*) malloc(20);

    >
    > Lose the cast, unless you're working with a *very* old implementation
    > (pre-C89).


    No, in pre-C89 there was no problem at all, as long as the destination
    type was, as in this case, a pointer to char. Prior to C89, malloc()
    returned a pointer to char.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://c-faq.com/
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
     
    Jack Klein, Apr 19, 2006
    #13
  14. Mark McIntyre wrote:
    > On Tue, 18 Apr 2006 19:55:34 GMT, in comp.lang.c , Martin Ambuhl
    > <> wrote:
    >
    >
    >>Keith Thompson wrote:
    >>
    >>>Martin Ambuhl <> writes:
    >>>
    >>>
    >>>>Idiomatic C Code is "usable with both C and C++."
    >>>
    >>>I think you dropped a "not", i.e.,
    >>>
    >>> Idiomatic C Code is *not* "usable with both C and C++."

    >>
    >>I did not drop a "not".

    >
    > <snip>
    >
    >>Why in the world you thought I dropped a "not" is between you and your God.

    >
    >
    > Well, frankly I also thought you dropped a "not" and I'm not convinced
    > there was a call to be quite so rude.


    I clearly have a problem communicating with both Mark McIntyre and Keith
    Thompson. I have no idea why Mark thinks that was "so rude" or Keith
    thinks it was "getting quite so personal about it". What I wrote is a
    simple idiom expressing a lack of understanding of why someone had
    written something. If Mark thinks it "so rude" amd Keith thinks it "so
    personal", I'm sorry. I'm sure the problem is with my writing; I've
    otherwise seen little sign of chips on the shoulders of these gentlemen.

    > Idiomatic C can't be used in C++
    > unless you take great care to avoid all sorts of trivial C-isms and
    > add in extra C++ specific stuff.


    Mark has misunderstood what I responded to and what I wrote. Mark is
    quite right that "idiomatic C can't be used in C++". That is completely
    beside the point. I wrote, quoting the OP,
    >Idiomatic C Code is "usable with both C and C++."

    The word "with" and and the word "in" are not the same, and the OP's
    assertion that C++ism were necessary for code to be "usable with both C
    and C++" is blatantly false.
     
    Martin Ambuhl, Apr 19, 2006
    #14
  15. Martin Ambuhl opined:

    > Mark McIntyre wrote:
    >> On Tue, 18 Apr 2006 19:55:34 GMT, in comp.lang.c , Martin Ambuhl
    >> <> wrote:
    >>
    >>
    >>>Keith Thompson wrote:
    >>>
    >>>>Martin Ambuhl <> writes:
    >>>>
    >>>>
    >>>>>Idiomatic C Code is "usable with both C and C++."
    >>>>
    >>>>I think you dropped a "not", i.e.,
    >>>>
    >>>> Idiomatic C Code is *not* "usable with both C and C++."
    >>>
    >>>I did not drop a "not".

    >>
    >> <snip>
    >>
    >>>Why in the world you thought I dropped a "not" is between you and
    >>>your God.

    >>
    >>
    >> Well, frankly I also thought you dropped a "not" and I'm not
    >> convinced there was a call to be quite so rude.

    >
    > I clearly have a problem communicating with both Mark McIntyre and
    > Keith Thompson. I have no idea why Mark thinks that was "so rude" or
    > Keith thinks it was "getting quite so personal about it". What I
    > wrote is a simple idiom expressing a lack of understanding of why
    > someone had written something. If Mark thinks it "so rude" amd Keith
    > thinks it "so personal", I'm sorry. I'm sure the problem is with my
    > writing; I've otherwise seen little sign of chips on the shoulders of
    > these gentlemen.


    I think it's the "between you and *your* God" that's the problem. Not
    being of a religious disposition (to say the least) it took me a few
    re-reads to catch it, but I think I can see how someone can feel it as
    both "personal" and "rude" (and I'd defend their right to do so, to
    poorly paraphrase). However, I'm also sure that it wasn't your
    intention for it to be taken that way. Hopefully, both Keith and Mark
    will see it that way as well.

    --
    "Who is General Failure and why is he reading my hard disk ?"
    Microsoft spel chekar vor sail, worgs grate !!
    (By -berlin.de, Felix von Leitner)

    <http://clc-wiki.net/wiki/Introduction_to_comp.lang.c>
     
    Vladimir S. Oka, Apr 19, 2006
    #15
  16. John Bode Guest

    Jack Klein wrote:
    > On 18 Apr 2006 04:55:11 -0700, "John Bode" <>
    > wrote in comp.lang.c:
    >
    > >
    > > wrote:
    > > > Statement given below;
    > > >
    > > > char * p ;
    > > >
    > > > p = (char*) malloc(20);

    > >
    > > Lose the cast, unless you're working with a *very* old implementation
    > > (pre-C89).

    >
    > No, in pre-C89 there was no problem at all, as long as the destination
    > type was, as in this case, a pointer to char. Prior to C89, malloc()
    > returned a pointer to char.
    >


    I was without 'net access for a week, so I was behind on my "being
    stupid in public" quota. Hopefully I'm all caught up now.

    Obviously, for this particular example, the cast can be dropped whether
    you're dealing with a pre-C89 implementation or not. I was thinking of
    the general case, for types other than char *.
     
    John Bode, Apr 19, 2006
    #16
  17. Martin Ambuhl <> writes:
    [...]
    > I clearly have a problem communicating with both Mark McIntyre and
    > Keith Thompson. I have no idea why Mark thinks that was "so rude" or
    > Keith thinks it was "getting quite so personal about it". What I
    > wrote is a simple idiom expressing a lack of understanding of why
    > someone had written something. If Mark thinks it "so rude" amd Keith
    > thinks it "so personal", I'm sorry. I'm sure the problem is with my
    > writing; I've otherwise seen little sign of chips on the shoulders of
    > these gentlemen.


    No offense intended, none taken.

    Smilies for everyone! :cool:}

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Apr 19, 2006
    #17
  18. On Wed, 19 Apr 2006 19:05:01 +0100, in comp.lang.c , "Vladimir S. Oka"
    <> wrote:

    >Martin Ambuhl opined:
    >
    >> Mark McIntyre wrote:
    >>> On Tue, 18 Apr 2006 19:55:34 GMT, in comp.lang.c , Martin Ambuhl
    >>> <> wrote:


    >>>>Why in the world you thought I dropped a "not" is between you and
    >>>>your God.
    >>>
    >>>
    >>> Well, frankly I also thought you dropped a "not" and I'm not
    >>> convinced there was a call to be quite so rude.

    >>
    >>I have no idea why Mark thinks that was "so rude"

    >
    >I think it's the "between you and *your* God" that's the problem.


    Indeed. In my variant of english, this is a not-so-subtle way of
    saying either "I don't give a fsck why you think that" or "only
    weirdos think that way".

    >Hopefully, both Keith and Mark will see it that way as well.


    One or two of Martin's posts have been getting more and more Poppish,
    and sometimes I get riled. I'll try not to.
    Mark McIntyre
    --
    "Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are,
    by definition, not smart enough to debug it."
    --Brian Kernighan
     
    Mark McIntyre, Apr 19, 2006
    #18
  19. Martin Ambuhl said:

    > What I wrote is a
    > simple idiom expressing a lack of understanding of why someone had
    > written something. If Mark thinks it "so rude" amd Keith thinks it "so
    > personal", I'm sorry. I'm sure the problem is with my writing; I've
    > otherwise seen little sign of chips on the shoulders of these gentlemen.


    I'm afraid you're right, Martin. It is indeed a problem with your writing.
    At least, having read through the whole subthread chronologically, I found
    the wording of your "you and your god" article to be unnecessarily
    provocative.

    On the bright side, though, I found nothing wrong with its technical
    content. :)

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at above domain (but drop the www, obviously)
     
    Richard Heathfield, Apr 19, 2006
    #19
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. wwj
    Replies:
    7
    Views:
    594
  2. lovecreatesbeauty
    Replies:
    1
    Views:
    1,149
    Ian Collins
    May 9, 2006
  3. Stefan Ram

    Brian Harry

    Stefan Ram, Apr 20, 2007, in forum: Java
    Replies:
    1
    Views:
    357
    Sanjay
    Apr 20, 2007
  4. Helmut Jarausch

    Python and Harry Potter?

    Helmut Jarausch, Jun 5, 2008, in forum: Python
    Replies:
    5
    Views:
    370
    Eduardo O. Padoan
    Jun 5, 2008
  5. GHULAM MUSTAFA

    Start business in home Harry up

    GHULAM MUSTAFA, Aug 27, 2009, in forum: Python
    Replies:
    1
    Views:
    283
    Noraizi
    Sep 15, 2009
Loading...

Share This Page