C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 1st, 2012, 07:24 AM
Mike.R Mike.R is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 3 Mike.R User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 34 m 43 sec
Reputation Power: 0
Str_cat,str_cpy question

Hi guys,
I got a little challenge to implement:
Discription:
* Look at main(): it calls various functions.
* You are asked to implement two functions: str_cpy() and str_cat(). No need to implement str_printf() and str_free()
* Reading main() carefully will allow to understand str_cpy() and str_cat() signature and usage.
* The code you write needs to be "library quality"; as good as you would expect a good libc to implement such functions.
* At the top of the page, you see 4 includes - indicating the functions that can be used to implement str_cpy() and str_cat().
* FYI: it is possible to implement str_cpy() and str_cat() efficiently in no more than 7 lines of code per function.

So I implement it and i think it works fine ( I debugged it and checked several times)

my implementation:
Code:
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <stdarg.h>

    void str_cpy(char **dst,char *src){
	int i=0;
	char **dst1;
	dst1=(char**)malloc(sizeof(char*));
	*dst1=(char*)malloc(strlen(src)+1);      // asume that allocation will always work
	//strcpy(*dst,src);
	while(*(src+i)!='\0'){
		*(*dst1+i)=*(src+i);
		i++;
	}
	*(*dst1+i)='\0';
	free(*dst);
	*dst=*dst1;
	free(dst1);
    }

    void str_cat(char **dst,char *src){
	//strcat(*dst,src);
	int i=0;
	int n=strlen(*dst);
	*dst=(char*)realloc(*dst,n+strlen(src)+1);      
	while(*(src+i)!='\0'){
		*(*dst+n+i)=*(src+i);
		i++;
	}
    *(*dst+n+i)='\0';
    }

    int main(int argc, char *argv[])
    {
    char *s = NULL;
    str_cpy(&s, "Hola Hola");
    str_cpy(&s, s+5);
    str_cat(&s, " Mundo");
    //str_printf(&s, "%s!", s);
    puts(s); /* result: "Hola Mundo" */
    //str_free(&s);
    system("pause");
    return 0;
}
       

I sent my solution and first response that i got that my solution simply not good enough,
So i asked a little more information why it is not good and that what i got:

Your solution failed our challenge because it crashes ("Segmentation
Fault" error) on the challenge's main() example

and they won't tell me any more.

I debugged my code. I use Visual studio 2008.It works good!!!
I will really appreciate if some body will give me a clue at which direction to think and why it fails or a new way to debug it ?
Tnx a lot.

Reply With Quote
  #2  
Old November 1st, 2012, 02:27 PM
TheOtherDino's Avatar
TheOtherDino TheOtherDino is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2010
Location: Katy, Texas
Posts: 489 TheOtherDino User rank is First Lieutenant (10000 - 20000 Reputation Level)TheOtherDino User rank is First Lieutenant (10000 - 20000 Reputation Level)TheOtherDino User rank is First Lieutenant (10000 - 20000 Reputation Level)TheOtherDino User rank is First Lieutenant (10000 - 20000 Reputation Level)TheOtherDino User rank is First Lieutenant (10000 - 20000 Reputation Level)TheOtherDino User rank is First Lieutenant (10000 - 20000 Reputation Level)TheOtherDino User rank is First Lieutenant (10000 - 20000 Reputation Level)TheOtherDino User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 4 Days 18 h 25 m 29 sec
Reputation Power: 198
What is this doing the first time str_cpy() is called?

Code:
free(*dst);
__________________
Do you agree? Disagree? And remember, it's all about the reputation power...

Reply With Quote
  #3  
Old November 1st, 2012, 02:44 PM
Mike.R Mike.R is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 3 Mike.R User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 34 m 43 sec
Reputation Power: 0
Tnx for reply
For the first time it do nothing but i think that freeing the NULL ptr won't cause an error.

Reply With Quote
  #4  
Old November 1st, 2012, 03:31 PM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 9th Plane (9000 - 9499 posts)
 
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
Posts: 9,381 Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 4 Weeks 1 Day 20 h 24 m 14 sec
Reputation Power: 4080
^^^
That is correct. free() is guaranteed to do nothing if the argument passed to it is NULL. Your bug is right below it though:
Code:
	*dst=*dst1;
	free(dst1);

The first line assigns dst to whatever dst1 is pointing to. It DOES NOT make a copy of that memory (Note emphasis on DOES NOT). The next line frees the chunk of memory that dst1 is pointing to. Now guess what dst is pointing to. That's right, it is still pointing to the chunk that you just freed when you freed dst1.

Now, if you try to access that memory after you freed it, random things could happen, depending on environment, compiler settings, debug mode, phase of the moon etc. It may work or it may seg-fault. In any case, it is a very bad thing to do.
__________________
Up the Irons
What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
"Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
Down with Sharon Osbourne

Reply With Quote
  #5  
Old November 1st, 2012, 05:49 PM
Mike.R Mike.R is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 3 Mike.R User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 34 m 43 sec
Reputation Power: 0
Tnx for help Scorpions4ever,
I may not agree with you coz : my first line assigns *dst=*dst1 (not dst=dst1 as you claims). After that it i really freeing dst1 which points to *dst1 (but i don't free the memory which was allocated for *dst1). So *dst still points to allocated memory.

Reply With Quote
  #6  
Old January 25th, 2013, 06:39 AM
jonson1 jonson1 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 1 jonson1 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 m 27 sec
Reputation Power: 0
hey mike, did you solve it in the end?

did you solve it in the end?
I was wondering what was the solution for this problem was..

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Str_cat,str_cpy question

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap