3

Possible Duplicate:
Any better suggestions for this c functions copyString,concatString

This is a question form a job interview , I need to implement it with a specific signature this is the code I need to work:

int main(int argc, char *argv[])
{

    char *str = NULL;
    new_strcpy(&str , "string one");
    new_strcpy(&str , str +7);
    new_strcat(&str , " two");
    new_printf(&str , "%str !", s);
    puts(str ); 
    new_free(&str);
    return 0;
}

this is my implementation to new_strcpy:

char* new_strcpy(char **dst,const char *source)
{

  char *ans=*dst;

  while(**dst++=*source++);

  return ans;

}

But this solution crash, can someone help me ?

Community
  • 1
  • 1
user1120007
  • 268
  • 3
  • 13
  • `**dst++` use parentheses for everyone sake. – UmNyobe Dec 05 '12 at 09:17
  • 3
    "This is a question form a job interview" -- sorry, but you should not have told them you know C. – Steve Jessop Dec 05 '12 at 09:43
  • maybe the interviewee mentions the benefits of code re-use and how one should aim to allocate and free heap memory in the same scope rather than offsetting them by reinventing existing functions in a less elegant form (in other words its not a very good interview question) – bph Dec 05 '12 at 10:00

1 Answers1

6

The problem with your solution is that you fail to allocate memory for *dst.

Consider the first three lines of the code that needs to work:

char *str = NULL;
new_strcpy(&str , "string one");
new_strcpy(&str , str +7);         // ***

From this, it is clear that:

  1. new_strcpy() needs to allocate memory for the result.
  2. When allocating str anew, new_strcpy() needs to deallocate the previous str to avoid leaking memory.
  3. To make line *** above work, the deallocation has to happen after the allocation.

Here is a skeleton implementation to give you the idea. I implement the functions in terms of strcpy() et al, but if calling the library function is not permissible, you can write your own loops (you already know how to do that).

#include <stdlib.h>
#include <string.h>

void new_strcpy(char** dst, const char* src) {
    char* orig_dst = *dst;
    *dst = malloc(strlen(src) + 1);
    strcpy(*dst, src); /* replace with a loop if calling strcpy() is not permissible */
    free(orig_dst);
}

void new_strcat(char** dst, const char* src) {
    char* orig_dst = *dst;
    *dst = malloc(strlen(*dst) + strlen(src) + 1);
    strcpy(*dst, orig_dst); /* replace with a loop if calling strcpy() is not permissible */
    strcat(*dst, src);      /* ditto for strcat() */
    free(orig_dst);
}

void new_free(char** dst) {
    free(*dst);
    *dst = NULL;
}

int main(int argc, char *argv[])
{
    char *str = NULL;
    new_strcpy(&str , "string one");
    new_strcpy(&str , str +7);
    new_strcat(&str , " two");
/*    new_printf(&str , "%str !", s); */
    puts(str );
    new_free(&str);
    return 0;
}

I leave implementing new_printf() as an exercise for the reader. :-)

NPE
  • 486,780
  • 108
  • 951
  • 1,012
  • free(orig_dst); is wrong. with that you will lose the memory of *dst. you should not free memory in that stage. – MOHAMED Dec 05 '12 at 09:40
  • @MohamedKALLEL: You are completely missing the point. See how the functions are used in the OP's `main()`. – NPE Dec 05 '12 at 09:41
  • the question is to develop an alternate function of `strcpy` and not use strcp in the `new_strcpy` function – MOHAMED Dec 05 '12 at 09:41
  • @MohamedKALLEL: And for `new_printf()` the task is what, to write your own `printf()` from scratch? – NPE Dec 05 '12 at 09:42
  • @Lundin: Have you looked at the code in any detail? – NPE Dec 05 '12 at 09:42
  • @NPE for the `new_printf()`. the exercice is not to print on the screen but is to print on a destination string. so it could be done from scratch ;-) – MOHAMED Dec 05 '12 at 09:53
  • 2
    I don't think this is returning a pointer to garbage land - orig_dst is free'd, dst is returned, after the call to malloc these point to different things - looks ok to me – bph Dec 05 '12 at 09:55
  • Looks ok to me too now. So I removed my answer. – David J Dec 05 '12 at 10:02
  • @NPE: when you use this instruction "free(orig_dst)", you lose the returned value (because you have this instruction "char* orig_dst = *dst") – developer Dec 05 '12 at 10:17
  • @AhmedZRIBI: `*dst` gets re-assigned between the two lines that you quote. – NPE Dec 05 '12 at 10:18
  • 1
    Is it the official troll day or something? – NPE Dec 05 '12 at 10:19
  • 1
    Ok so it doesn't return a garbage pointer, but it assumes that it gets a pointer to dynamic memory, and then returns an allocated chunk of memory which isn't going to be freed unless you call a custom free function. The code is so obscure I don't see how it would be of any use in a real world application. A sane implementation would be similar to strdup. – Lundin Dec 05 '12 at 10:25
  • 1
    I guess the interviewer want to see if the candidate can handle heap memory and can use pointer the right way. Its not the question if this is a real world problem. For example I would let pass the candidate if he/she is able to give the right ideas. No need to write the code untile the end. – David J Dec 05 '12 at 10:29
  • 1
    @NPE. You are right, the free does not cause loose of the new malloc because it free the old buffer. – MOHAMED Dec 05 '12 at 10:31
  • @Lundin yes its the question not the solution which is at fault here – bph Dec 05 '12 at 11:51