LINUX.IE, website of the Irish Linux Users' Group
Tux rules!

   
Home
New Users
Articles
Download
Projects
Community
Vendors

  Print Version
 
Archives:


planetILUG

Recent News

News Archive


Join the
ILUG
on FaceBook


Join the
ILUG
on LinkedIn


Join the
ILUG SETI
Group



















 
 :: Mailing Lists

[ILUG] C string concat question ??

[ILUG] C string concat question ??

Glen Gray glen at antefacto.com
Wed May 2 18:57:40 IST 2001


On 02 May 2001 18:09:29 +0100, Kenn Humborg wrote:
> > Looking for a simple way to cat two strings into a third.
><SNIP>
> And you are, of course, sure that the memory allocated to strvar1 
> has enough space for strvar2?  And this is consipicously commented
> in the source code so that the next guy doesn't break it (or, worse
> still, use the same code elsewhere without ensuring this)?
> And you'll check that strdup() doesn't return NULL?
> 

Yes, and no 'cause its not there anymore. When it wasn't doing what I
thought I chopped for a
more sensible snprintf. And yes the result of strdup is duely checked.

> See, it's more than just a one-liner in any case...  Now stop
> being lazy and write _good_ code as opposed to _cute_ code 
> (which is very often broken, fragile, unmaintainable or downright
> insecure):
> 
>    msg = malloc(strlen(strvar1) + strlen(strvar2) + 1);
>    if (msg == NULL) {
>        /* argh */
>    } else {
>       strcpy(msg, strvar1);
>       strcat(msg, strvar2);
>    }
> 

[Bows head] "Yes sir"
Missing the question slightly though. What I was asking was there a
function that did this already that I wasn't aware of, something along
the lines of. I guess not :-)

Thanks for the programming tips though !
Glen





More information about the ILUG mailing list
Read this without the formatting.
                                                                                                    

 

Hosted by HEAnet


Maintained by the ILUG website team. The aim of Linux.ie is to support and help commercial and private users of Linux in Ireland. You can display ILUG news in your own webpages, read backend information to find out how. Networking services kindly provided by HEAnet, server kindly donated by Dell. Linux is a trademark of Linus Torvalds, used with permission. No penguins were harmed in the production or maintenance of this highly praised website. Looking for the Indian Linux Users' Group? Try here. If you've read all this and aren't a lawyer: you should be!
RSS Version
Powered by Dell