#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    3
    Rep 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.
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2010
    Location
    Katy, Texas
    Posts
    495
    Rep Power
    199
    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...
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    3
    Rep 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.
  6. #4
  7. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,594
    Rep Power
    4207
    ^^^
    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

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    3
    Rep 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.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    1
    Rep 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..

IMN logo majestic logo threadwatch logo seochat tools logo