473,395 Members | 1,556 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

char* pname = "Harry"

Statement given below;

char * p ;

p = (char*) malloc(20);

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

free(p);

Apr 18 '06 #1
18 1760
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

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

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

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

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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: harry | last post by:
Hi, During compilation, a C# project in my solution triggers the following warning: "warning CS0168: The variable 'ex' is declared but never used" To trigger this warning, it appears the C#...
4
by: Harry Whitehouse | last post by:
For some string values, I see an @ character pre-pended to the string data itself. IOW, mystring=@"12345Test" whereas othertimes I don't see the @. What is the significance of that...
10
by: MLH | last post by:
I would like to call a function that "returned" several values - all of which are relevant to the needs of a procedure on a form. I do understand that FN's return a single value. I'm wondering,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.