Saturday, July 7, 2007

Write a C program to implement your own strdup() function.

Here is a C program to implement the strdup() function.


char *mystrdup(char *s)
{
char *result = (char*)malloc(strlen(s) + 1);
if (result == (char*)0){return (char*)0;}
strcpy(result, s);
return result;
}

42 comments:

Anonymous said...

Could someone explain this line, please:
if(result==(char*)0){return (char*)0;}
Is it returning null if memory on the heap cannot be allocated?

jagannath said...

char *result = (char*)malloc(strlen(s) + 1);
should be
char *result = (char*)malloc(strlen(s)*sizeof(char) + 1);

It should be generic. I was asked this question in ADOBE interview, interviewer asked a lot string libary functions implementations. He stressed on writing generic and fast code.

Anonymous said...

Hey there would you mind sharing which blog platform
you're using? I'm looking to start my own blog in the near future but I'm having a tough time choosing between BlogEngine/Wordpress/B2evolution and Drupal. The reason I ask is because your design seems different then most blogs and I'm looking
for something completely unique. P.S My apologies for getting off-topic but I had to ask!


Feel free to surf to my web site carrier furnace troubleshooting

Anonymous said...

Hey there would you mind letting me know which webhost you're using? I've loaded your blog in
3 completely different browsers and I must say this blog loads a lot faster then most.
Can you suggest a good web hosting provider at a honest price?
Many thanks, I appreciate it!

Feel free to visit my website :: search short term housing
My web site :: short term rental agreement form

Anonymous said...

Yesterday, while I was at work, my sister stole my iPad and tested to see if it can survive a
25 foot drop, just so she can be a youtube sensation.
My apple ipad is now destroyed and she has 83 views.
I know this is entirely off topic but I had to share it with
someone!

Here is my weblog gas furnace ratings

Anonymous said...

Greetings from Ohio! I'm bored to tears at work so I decided to browse your blog on my iphone during lunch break. I love the knowledge you present here and can't wait to
take a look when I get home. I'm amazed at how quick your blog loaded on my mobile .. I'm not even using WIFI,
just 3G .. Anyhow, amazing blog!

My website; interiors.net

Anonymous said...

Write more, thats all I have to say. Literally, it seems as though you relied on the
video to make your point. You obviously know what youre talking about, why waste
your intelligence on just posting videos to your blog when you
could be giving us something informative to
read?

Visit my blog ladies fashion boots

Anonymous said...

My partner and I stumbled over here from a
different web page and thought I might as well check things out.
I like what I see so i am just following
you. Look forward to exploring your web page repeatedly.


My website :: 80's workout clothes ideas

Anonymous said...

Hmm it appears like your website ate my first
comment (it was super long) so I guess I'll just sum it up what I submitted and say, I'm thoroughly enjoying your blog.
I as well am an aspiring blog writer but I'm still new to the whole thing. Do you have any helpful hints for rookie blog writers? I'd certainly appreciate it.



my web blog; cute gym clothes

Anonymous said...

I am really enjoying the theme/design of
your web site. Do you ever run into any internet browser compatibility problems?

A small number of my blog audience have complained about my
site not working correctly in Explorer but looks great in Safari.
Do you have any tips to help fix this problem?

Here is my site :: cold weather running clothes for men

Anonymous said...

Hi there! I hope you don't mind but I decided to submit your website: http://www.blogger.com/comment.g?blogID=7662544687317968079&postID=2411526366936388504 to my online directory website. I used, "Blogger: Interview Questions" as your site headline. I hope this is ok with you. In the event you'd like me to change the title or perhaps remove it
completely, email me at tammarastephens@gmail.com.
Appreciate it.

Have a look at my web-site: computer component review sites

Anonymous said...

With havin so much content and articles do you ever run into any issues of plagorism
or copyright violation? My website has a lot of completely unique content I've either created myself or outsourced but it appears a lot of it is popping it up all over the web without my authorization. Do you know any solutions to help stop content from being stolen? I'd really appreciate it.



my blog; average electrical engineering salaries

Anonymous said...

Greetings from California! I'm bored to death at work so I decided to check out your website on my iphone during lunch break. I love the knowledge you present here and can't wait to take
a look when I get home. I'm surprised at how quick your blog loaded on my cell phone .. I'm not
even using WIFI, just 3G .. Anyways, great blog!

my weblog; usb cable extension best buy

Anonymous said...

I was wondering if you ever considered changing the
structure of your blog? Its very well written; I love what youve got to say.

But maybe you could a little more in the way of content so people
could connect with it better. Youve got an awful lot of text for only having 1
or 2 images. Maybe you could space it out better?

Here is my webpage ... american handyman services tucson az

Anonymous said...

Great post but I was wanting to know if you
could write a litte more on this subject?
I'd be very thankful if you could elaborate a little bit further. Appreciate it!

Also visit my weblog; iimeeting.com

Anonymous said...

Hi! My wife and I frequently write guest posts for other site
owners to help increase exposure to our work, as well
as provide fantastic content to website owners. It really is
a win win situation! If you are interested feel free to contact
me at: baileythibodeaux@freenet.de so we can communicate further.
Thank you!

My weblog: www.xfire.com

Anonymous said...

I am really enjoying the theme/design of your weblog. Do you ever
run into any internet browser compatibility issues? A handful of my blog readers have
complained about my website not operating correctly in Explorer but looks great in Safari.
Do you have any recommendations to help fix this issue?


Look into my website ... http://www.openbottles.com/

Anonymous said...

Hey there! Would you mind if I share your blog with my facebook group?
There's a lot of people that I think would really appreciate your content. Please let me know. Thanks

my web page - handyman bookshelf

Anonymous said...

Awesome blog! Do you have any helpful hints for aspiring writers?
I'm hoping to start my own website soon but I'm a little lost on
everything. Would you propose starting with a free platform
like Wordpress or go for a paid option? There are so many options out there that I'm totally confused .. Any tips? Bless you!

Also visit my homepage ... digital Camera canon powershot

Anonymous said...

I'm curious which blogging platform you're using? I'm new to operating a blog and have been thinking about using the Ning platform. Do you consider this is a good platform to start with? I would be extremely thankful if I could ask you some questions through e-mail so I can learn a bit more prior to getting started. When you have some free time, please make sure to contact me at: audrea.kuntz@inbox.com. Thankyou

Here is my weblog; discount capezio ballroom dance shoes

Anonymous said...

Wow that was unusual. I just wrote an incredibly long comment but after I clicked submit my comment didn't appear. Grrrr... well I'm not writing all that over again.
Anyway, just wanted to say excellent blog!

My page https://webconfaccess.gallaudet.edu/

Anonymous said...

Ι think this iѕ among the most ѕignifіcаnt infο for
me. And і am glаd reаԁіng yоur artiсle.
But want to remаrk οn ѕome genеral things, The webѕіtе stуlе iѕ wonderful,
the articles iѕ really excеllent :
D. Good job, cheers

Feel fгee tο suгf to my ρage; raspberry ketone uk

Anonymous said...

I like the vаluable information yοu
pгovide in your aгticlеs.
I will bookmark your weblog and check again here frequеntlу.
I am quite сertain I'll learn a lot of new stuff right here! Good luck for the next!

my page: raspberry ketone uk

Anonymous said...

Have you ever thought about creating an ebook or guest authoring
on other websites? I have a blog based upon on the same information you discuss
and would really like to have you share some stories/information.
I know my visitors would value your work. If you're even remotely interested, feel free to shoot me an e-mail.

My web-site - digital images photography

Anonymous said...

Hi there! Would you mind if I share your blog with my
twitter group? There's a lot of people that I think would really enjoy your content. Please let me know. Cheers

Also visit my web-site: silicone iphone 4 case review

Anonymous said...

Woah! I'm really digging the template/theme of this website. It's simple, yet effective.
A lot of times it's hard to get that "perfect balance" between usability and visual appeal. I must say that you've done a very good job with this.
In addition, the blog loads super quick for me on Opera.
Outstanding Blog!

Feel free to visit my webpage ... amana refrigerator repair water dispenser

Anonymous said...

Attractive section of cοntent. I just stumbled upοn youг weblog and in aсcesѕion
саpital tο asseгt that Ι get actuallу еnјοyeԁ account your blog posts.

Anу ωаy Ι ωill be subѕcribing tο уour fееԁs аnd eνen I achіеvement уou accesѕ cοnsiѕtently quiсκly.



Also vіѕit my wеb ѕіtе .
.. vitamin shoppe coupon code

Anonymous said...

With havin so much written content do you ever run into any issues of plagorism or copyright violation?
My website has a lot of unique content I've either created myself or outsourced but it looks like a lot of it is popping it up all over the internet without my agreement. Do you know any methods to help protect against content from being ripped off? I'd genuinely appreciate it.



Here is my website - freezer temperature monitor

Anonymous said...

Wow that was odd. I just wrote an incredibly long comment but after I clicked submit my comment didn't appear. Grrrr... well I'm not writing all that over again.
Anyhow, just wanted to say wonderful blog!

Feel free to visit my web-site freezer drawers under counter australia

Anonymous said...

Heу very nіce blog!

Reѵіeω my wеb blog :: vistaprint coupons

Anonymous said...

Hello just wanted to give you a quick heads up and let you know a few
of the pictures aren't loading correctly. I'm not sure why but I
think its a linking issue. I've tried it in two different browsers and both show the same outcome. ageless male supplement facts - does ageless male testosterone booster work - enhancement capsules

Anonymous said...

It's really very complex in this busy life to listen news on Television, so I simply use web for that purpose, and take the latest information.

Also visit my web-site; Las Vegas Golf Instruction

Anonymous said...

Really when someone doesn't understand then its up to other visitors that they will help, so here it takes place.

my web-site; Grow XL Male Enhancement

Anonymous said...

You actually make it seem so easy together with your presentation however
I in finding this topic to be really something that I feel I'd by no means understand. It kind of feels too complicated and very large for me. I'm having a
look forward for your next submit, I will try to get the hang of it!


Look into my web page :: sushi recipes pinterest

Anonymous said...

It's remarkable in support of me to have a website, which is useful in favor of my know-how. thanks admin

Feel free to surf to my homepage; largest home remodeling company us

Anonymous said...

Good site you've got here.. It's difficult to find excellent writing like
yours these days. I really appreciate individuals like you!
Take care!!

my site - lowes kitchen appliances package deals

Anonymous said...

Hey there I am so happy I found your webpage, I really found you by mistake,
while I was searching on Askjeeve for something else, Anyways I am here now and would just
like to say cheers for a fantastic post and a all round thrilling blog (I also love the theme/design), I
don't have time to look over it all at the minute but I have bookmarked it and also added your RSS feeds, so when I have time I will be back to read a great deal more, Please do keep up the awesome job.

Also visit my web-site - appliance repair in knoxville tn

Anonymous said...

Incredible! I'm genuinely enjoying the style and design of your web site. Are you using a custom made theme or is this freely available to all individuals? If you really don't want to say the name
of it out in the public, please be sure to e-mail me at:
tiamendoza@gmail.com. I'd absolutely love to get my hands on this template! Thank you.

Also visit my blog :: appliance repair service

Anonymous said...

Hey! I could have sworn I've been to this website before but after reading through some of the post I realized it's new to me.
Anyways, I'm definitely happy I found it and I'll be book-marking and checking back frequently!


my site - 24 hr appliance repairs

John Terry said...

Found your blog. It is full of really good information. Thank you for sharing. If you ever need service on your furnace, air conditioner or water heater please visit us at rheemteamcomfort.com. We would love it if you would have a look at some of our blogs and let us know your thoughts.

Anonymous said...

You already found the lenght of the string when calling strlen() therefor strcpy() will be doing redundant work of checking every char for equality with '\0'.

strlen(s) does something like
size_t len = 0;
while(*s++ != '\0') len++;
return len;

and

strcpy(dst,src) does something like
char c;
while(*src != '\0') *dst++ = *src++;
*dst = '\0';

Of course, they are written in a more optimal way, but the point is they both check every char they go through.

If you already know how many bytes to copy, you don't need to check their value.

char * result;
size_t len = sizeof(char)*(strlen(s)+1);
if(result = (char *) malloc( len )) == (char*)0)
return (char *)0;
memcpy( result, s , len);
return result;

Anonymous said...

Also, you would need your function to take a const char * since you're going for strict type correctness.

You can pass a char* to a function that takes a const char*, the function will simply not modify it.

But if you pass a const char* to a function that takes a char*, then you will get a 'discards const qualifier' warning because you're passing something that should not be modified to someone who might modify it.