The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
Str_cat,str_cpy question
Discuss Str_cat,str_cpy question in the C Programming forum on Dev Shed. Str_cat,str_cpy question C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

November 1st, 2012, 07:24 AM
|
|
Registered User
|
|
Join Date: Nov 2012
Posts: 3
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.
|

November 1st, 2012, 02:27 PM
|
 |
Contributing User
|
|
Join Date: Jan 2010
Location: Katy, Texas
|
|
What is this doing the first time str_cpy() is called?
__________________
Do you agree? Disagree? And remember, it's all about the reputation power...
|

November 1st, 2012, 02:44 PM
|
|
Registered User
|
|
Join Date: Nov 2012
Posts: 3
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.
|

November 1st, 2012, 03:31 PM
|
 |
Banned ;)
|
|
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
|
|
^^^
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
|

November 1st, 2012, 05:49 PM
|
|
Registered User
|
|
Join Date: Nov 2012
Posts: 3
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.
|

January 25th, 2013, 06:39 AM
|
|
Registered User
|
|
Join Date: Jan 2013
Posts: 1
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..
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|