Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Problem with strcat, strcpy,sprintf

Reply
Thread Tools

Problem with strcat, strcpy,sprintf

 
 
Arctic Fidelity
Guest
Posts: n/a
 
      10-29-2005
On Fri, 28 Oct 2005 19:55:25 -0400, nelu <(E-Mail Removed)> wrote:

> I've seen this in a lot of places and I've been wandering if it's smart
> to write code like this:
>log_file=malloc(STRGSIZE)
>instead of:
>log_file=(char *)malloc(STRGSIZE*sizeof(char))
>even if it's faster to write
>
> because it is both a portability problem and a lot of students don't
> get the idea and do the exact same thing for types other than char?


From my understanding there should be no cast since void pointers are
implicitly converted, and that there is really no difference or preference
either way with regards to the sizeof(char) addition, because sizeof(char)
is always supposed to be 1? Of course, I can see where you're coming from
on the student side, and that's why I usually put the sizeof operator in
there.

- Arctic

--
Using Opera's revolutionary e-mail client: http://www.opera.com/mail/
 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      10-29-2005
"nelu" <(E-Mail Removed)> writes:
>> > log_file = malloc(STRGSIZE);

>>
>> Always check the result of malloc(); if it fails, it will return a
>> null pointer. Often the only thing you can do in response is to abort
>> the program, but it's better than continuing blindly.

>
> I've seen this in a lot of places and I've been wandering if it's smart
> to write code like this:
>
> log_file=malloc(STRGSIZE)
>
> instead of:
>
> log_file=(char *)malloc(STRGSIZE*sizeof(char))
>
> even if it's faster to write
> because it is both a portability problem and a lot of students don't
> get the idea and do the exact same thing for types other than char?


If you assign the result of malloc() to a pointer object, it's never
necessary to cast the result. malloc() returns a result of type
void*, which can be implicitly converted to any pointer-to-object type.
Using the cast can mask the error of failing to "#include <stdlib.h>".
It can also mask the error of trying to compile C code with a C++
compiler (the implicit conversion isn't done in C++).

This has been discussed many many times in this newsgroup.

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <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.
 
Reply With Quote
 
 
 
 
nelu
Guest
Posts: n/a
 
      10-29-2005


> > log_file=(char *)malloc(STRGSIZE*sizeof(char))

> If you assign the result of malloc() to a pointer object, it's never
> necessary to cast the result. malloc() returns a result of type
> void*, which can be implicitly converted to any pointer-to-object type.
> Using the cast can mask the error of failing to "#include <stdlib.h>".
> It can also mask the error of trying to compile C code with a C++
> compiler (the implicit conversion isn't done in C++).


Thanks a lot!

 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      10-29-2005
"nelu" <(E-Mail Removed)> writes:
>> > log_file=(char *)malloc(STRGSIZE*sizeof(char))

>> If you assign the result of malloc() to a pointer object, it's never
>> necessary to cast the result. malloc() returns a result of type
>> void*, which can be implicitly converted to any pointer-to-object type.
>> Using the cast can mask the error of failing to "#include <stdlib.h>".
>> It can also mask the error of trying to compile C code with a C++
>> compiler (the implicit conversion isn't done in C++).

>
> Thanks a lot!


Thank you for quoting the previous article, but please don't snip the
attribution line (the one that indicates who wrote the quoted
material).

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <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.
 
Reply With Quote
 
Jordan Abel
Guest
Posts: n/a
 
      10-29-2005
On 2005-10-28, nelu <(E-Mail Removed)> wrote:
>
>> > log_file = malloc(STRGSIZE);

>>
>> Always check the result of malloc(); if it fails, it will return a
>> null pointer. Often the only thing you can do in response is to abort
>> the program, but it's better than continuing blindly.

>
> I've seen this in a lot of places and I've been wandering if it's smart
> to write code like this:
>
> log_file=malloc(STRGSIZE)
>
> instead of:
>
> log_file=(char *)malloc(STRGSIZE*sizeof(char))
>
> even if it's faster to write
> because it is both a portability problem and a lot of students don't
> get the idea and do the exact same thing for types other than char?


The statement in itself is not a portability problem. I'd say the
proper solution to the issue of students possibly applying this
wrongly to other types would be to make sure they learn it properly,
but you may be on to something.

(The above was a response to the addition of the sizeof(char) -
casting malloc is never necessary in C - no matter to what type)
 
Reply With Quote
 
pete
Guest
Posts: n/a
 
      10-29-2005
Arctic Fidelity wrote:
>
> On Fri, 28 Oct 2005 18:34:47 -0400,
> Keith Thompson <(E-Mail Removed)> wrote:


> >> void main(void)

> >
> > No, no, no, no, no.
> >
> > main() returns int, not void.

>
> :-O I never knew...Wah?? Gah! Ouch.
> I'll keep that in definite mind next time.


The rules are that main returns int,
but implementations may accept alternate forms of main.

Since the alternate forms are not standard,
and this is a newsgroup about C and not about *your* compiler,
the alternate forms are off topic here.

--
pete
 
Reply With Quote
 
Jordan Abel
Guest
Posts: n/a
 
      10-29-2005
On 2005-10-29, pete <(E-Mail Removed)> wrote:
> Arctic Fidelity wrote:
>>
>> On Fri, 28 Oct 2005 18:34:47 -0400,
>> Keith Thompson <(E-Mail Removed)> wrote:

>
>> >> void main(void)
>> >
>> > No, no, no, no, no.
>> >
>> > main() returns int, not void.

>>
>> :-O I never knew...Wah?? Gah! Ouch.
>> I'll keep that in definite mind next time.

>
> The rules are that main returns int, but implementations may
> accept alternate forms of main.
>
> Since the alternate forms are not standard, and this is a
> newsgroup about C and not about *your* compiler, the alternate
> forms are off topic here.


Furthermore, very few implementations _actually_ accept void main.
It's just that a few of the most common ones happen to not do
anything worse than having some arbitrary number as the exit status
 
Reply With Quote
 
Mark McIntyre
Guest
Posts: n/a
 
      10-29-2005
On Fri, 28 Oct 2005 23:38:31 GMT, in comp.lang.c , Keith Thompson
<(E-Mail Removed)> wrote:

>Mark McIntyre <(E-Mail Removed)> writes:
>> On 28 Oct 2005 14:09:43 -0700, in comp.lang.c ,
>> (E-Mail Removed) wrote:

>[...]
>>>void copy(char *filename)
>>>{
>>> char *log_file;
>>> log_file=filename;

>>
>> This points log_file to the same place as filename.
>>
>> Remember that in C, = is not the copy operator, its the assignment
>> operator. For pointer types, this sets the pointers to point to the
>> same place. It does /not/ copy the contents.

>[...]
>
>I don't think I'd phrase it that way.


Yeah, yours is perhaps more correct phrasing, but IMO that would have
totally confused anyone who thought of "string" as an actual type.
Since this is a very common newby view, I deliberately chose different
wording.


>Another way to put it is that "=" does a shallow copy, not a deep
>copy; it copies only the value itself,


As a newby I'd expect this to copy the string...

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>

----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
 
Reply With Quote
 
Mark McIntyre
Guest
Posts: n/a
 
      10-29-2005
On 28 Oct 2005 16:55:25 -0700, in comp.lang.c , "nelu"
<(E-Mail Removed)> wrote:

>I've seen this in a lot of places and I've been wandering if it's smart
>to write code like this:
>
>log_file=malloc(STRGSIZE)


This is the right way.

>instead of:
>
>log_file=(char *)malloc(STRGSIZE*sizeof(char))


In this version,
a) the cast is not needed and can conceal a serious error
b) sizeof(char) is by definition 1, so its not needed.

>because it is both a portability problem


only between C and C++

>and a lot of students don't
>get the idea and do the exact same thing for types other than char?


in that case use the form
log_file = malloc (STRGSIZE * sizeof (*log_file));
and no matter what log_file is defined as, you're clear.
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>

----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      10-29-2005
Mark McIntyre <(E-Mail Removed)> writes:
> On Fri, 28 Oct 2005 23:38:31 GMT, in comp.lang.c , Keith Thompson
> <(E-Mail Removed)> wrote:
>>Mark McIntyre <(E-Mail Removed)> writes:
>>> On 28 Oct 2005 14:09:43 -0700, in comp.lang.c ,
>>> (E-Mail Removed) wrote:

>>[...]
>>>>void copy(char *filename)
>>>>{
>>>> char *log_file;
>>>> log_file=filename;
>>>
>>> This points log_file to the same place as filename.
>>>
>>> Remember that in C, = is not the copy operator, its the assignment
>>> operator. For pointer types, this sets the pointers to point to the
>>> same place. It does /not/ copy the contents.

>>[...]
>>
>>I don't think I'd phrase it that way.

>
> Yeah, yours is perhaps more correct phrasing, but IMO that would have
> totally confused anyone who thought of "string" as an actual type.
> Since this is a very common newby view, I deliberately chose different
> wording.


I think that just reinforces any confusion. Anyone who thinks that
"string" is an actual type should be told that it isn't.

>>Another way to put it is that "=" does a shallow copy, not a deep
>>copy; it copies only the value itself,

>
> As a newby I'd expect this to copy the string...


Not if you understand what a "string" is. (You're not restricted to
explaining just one thing at a time.)

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem problem problem :( Need Help Mike ASP General 2 05-11-2004 08:36 AM



Advertisments