By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,985 Members | 1,777 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,985 IT Pros & Developers. It's quick & easy.

char* pname = "Harry"

P: n/a
Statement given below;

char * p ;

p = (char*) malloc(20);

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

free(p);

Apr 18 '06 #1
Share this Question
Share on Google+
18 Replies


P: n/a
er****@gmail.com 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.
Apr 18 '06 #2

P: n/a

er****@gmail.com 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.

Apr 18 '06 #3

P: n/a

John Bode wrote:
er****@gmail.com 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 '06 #4

P: n/a
pe***************@gmail.com wrote:
John Bode wrote:
er****@gmail.com 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
Apr 18 '06 #5

P: n/a
pe***************@gmail.com wrote:
John Bode wrote:
er****@gmail.com 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.
Apr 18 '06 #6

P: n/a
John Bode wrote:
er****@gmail.com 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.
Apr 18 '06 #7

P: n/a
Martin Ambuhl <ma*****@earthlink.net> writes:
pe***************@gmail.com 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) ks***@mib.org <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.
Apr 18 '06 #8

P: n/a
Keith Thompson wrote:
Martin Ambuhl <ma*****@earthlink.net> writes:
pe***************@gmail.com 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.

Apr 18 '06 #9

P: n/a
Martin Ambuhl <ma*****@earthlink.net> writes:
Keith Thompson wrote:
Martin Ambuhl <ma*****@earthlink.net> writes:
pe***************@gmail.com 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) ks***@mib.org <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.
Apr 18 '06 #10

P: n/a

pe***************@gmail.com wrote:
John Bode wrote:
er****@gmail.com 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.

Apr 18 '06 #11

P: n/a
On Tue, 18 Apr 2006 19:55:34 GMT, in comp.lang.c , Martin Ambuhl
<ma*****@earthlink.net> wrote:
Keith Thompson wrote:
Martin Ambuhl <ma*****@earthlink.net> 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
Apr 18 '06 #12

P: n/a
On 18 Apr 2006 04:55:11 -0700, "John Bode" <jo*******@my-deja.com>
wrote in comp.lang.c:

er****@gmail.com 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/~a...FAQ-acllc.html
Apr 19 '06 #13

P: n/a
Mark McIntyre wrote:
On Tue, 18 Apr 2006 19:55:34 GMT, in comp.lang.c , Martin Ambuhl
<ma*****@earthlink.net> wrote:

Keith Thompson wrote:
Martin Ambuhl <ma*****@earthlink.net> 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.
Apr 19 '06 #14

P: n/a
Martin Ambuhl opined:
Mark McIntyre wrote:
On Tue, 18 Apr 2006 19:55:34 GMT, in comp.lang.c , Martin Ambuhl
<ma*****@earthlink.net> wrote:

Keith Thompson wrote:

Martin Ambuhl <ma*****@earthlink.net> 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 le*****@inf.fu-berlin.de, Felix von Leitner)

<http://clc-wiki.net/wiki/Introduction_to_comp.lang.c>

Apr 19 '06 #15

P: n/a

Jack Klein wrote:
On 18 Apr 2006 04:55:11 -0700, "John Bode" <jo*******@my-deja.com>
wrote in comp.lang.c:

er****@gmail.com 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 *.

Apr 19 '06 #16

P: n/a
Martin Ambuhl <ma*****@earthlink.net> 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! 8-)}

--
Keith Thompson (The_Other_Keith) ks***@mib.org <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.
Apr 19 '06 #17

P: n/a
On Wed, 19 Apr 2006 19:05:01 +0100, in comp.lang.c , "Vladimir S. Oka"
<no****@btopenworld.com> wrote:
Martin Ambuhl opined:
Mark McIntyre wrote:
On Tue, 18 Apr 2006 19:55:34 GMT, in comp.lang.c , Martin Ambuhl
<ma*****@earthlink.net> 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
Apr 19 '06 #18

P: n/a
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)
Apr 19 '06 #19

This discussion thread is closed

Replies have been disabled for this discussion.