char* pname = "Harry"

E

erktek

Statement given below;

char * p ;

p = (char*) malloc(20);

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

free(p);
 
I

Ian Collins

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.
Bad, you are attempting to free "harry".
 
J

John Bode

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 said:

This will attempt to free the string literal "harry", not the memory
you allocated earlier.
 
P

peter.koch.larsen

John said:
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
 
F

Flash Gordon

John said:
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.
 
M

Martin Ambuhl

John said:
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.
 
S

Simon Biber

John said:
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.
 
K

Keith Thompson

Martin Ambuhl said:
(e-mail address removed) 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++."
 
M

Martin Ambuhl

Keith said:
Martin Ambuhl said:
(e-mail address removed) 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, < Why in
the world you thought I dropped a "not" is between you and your God.
 
K

Keith Thompson

Martin Ambuhl said:
Keith said:
Martin Ambuhl said:
(e-mail address removed) 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, < 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.)
 
J

John Bode

John said:
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.
 
M

Mark McIntyre

I did not drop a "not".
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
 
J

Jack Klein

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.
 
M

Martin Ambuhl

Mark said:
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.
 
V

Vladimir S. Oka

Martin Ambuhl opined:
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 (e-mail address removed)-berlin.de, Felix von Leitner)

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

John Bode

Jack said:
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 *.
 
K

Keith Thompson

Martin Ambuhl said:
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:}
 
M

Mark McIntyre

Martin Ambuhl opined:


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
 
R

Richard Heathfield

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. :)
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,480
Members
44,900
Latest member
Nell636132

Latest Threads

Top