Re: Difference between strcpy() and strcat()?
From: Leor Zolman (leor_at_bdsoft.com)
Date: 03/05/04
- Next message: Dan Pop: "Re: int pointing to char"
- Previous message: Dan Pop: "Re: int pointing to char"
- In reply to: Pascal Damian: "Re: Difference between strcpy() and strcat()?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Fri, 05 Mar 2004 14:03:52 GMT
On 5 Mar 2004 04:18:39 -0800, pascaldamian@icqmail.com (Pascal Damian)
wrote:
>Here's exactly where I read it, from a post at comp.lang.ruby:
>
>======================== start quote ============================
>> p = (char *)getenv("LD_LIBRARY_PATH");
>>> if ((env = (char *)malloc(512+1+(p==NULL ? 0:strlen(p)))) ==
>NULL) {
>>> fprintf(stderr, "malloc() failed: %s\n", strerror(errno));
>>> exit(1);
>>> }
>>> strcat(env, "LD_LIBRARY_PATH=");
>
>Use strcpy instead of strcat here, content of malloc'ed area is
>not guaranteed.
>======================== end quote ============================
>
>URL: http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&safe=off&selm=200403041635.i24GZh4Y030395%40sharui.nakada.niregi.kanuma.tochigi.jp
>URL: http://tinyurl.com/393yz
>
>Now what does the poster mean by "content of malloc'ed area is not
>guaranteed"?
Along with what Jens said, I think this kinda goes along with what I was
saying. There's more "stuff" to think about when using strcat; Personally,
I would never dream of using it "in place of" strcpy; the question just
wouldn't arise because I'm totally acclimated to what each of them does and
would pick the one most suitable to the task in the first place [and this
would probably be the wrong group to mention that, these days, I'd use
std::string in C++ and try to avoid str*() whenever possible, so I won't
say it... ;-) ]. The question isn't "which one is safer"; the lesson is:
research well what /any/ function does before you use it!
-leor
Leor Zolman
BD Software
leor@bdsoft.com
www.bdsoft.com -- On-Site Training in C/C++, Java, Perl & Unix
C++ users: Download BD Software's free STL Error Message
Decryptor at www.bdsoft.com/tools/stlfilt.html
- Next message: Dan Pop: "Re: int pointing to char"
- Previous message: Dan Pop: "Re: int pointing to char"
- In reply to: Pascal Damian: "Re: Difference between strcpy() and strcat()?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|