-5

//////

char* duplicate(char* string, int count)
{
    char* duped = (char*) malloc( sizeof(char)*(count*strlength(string)+1) );
    int i=0,j=0,t=0;
    for( i=0; i<count*strlength(string); i++,j++ )
    {   
        if (j==(strlength(string))) 
        {
            j=0; 
            t++;
        }

        if (t==count) 
        {
            duped[i] = '\0'; 
            break;
        }

        duped[i] = string[j];
    }

    return duped;
}

The above code works perfectly, but the one below gives a segmentation fault. What is the difference between them? Shouldn't a function stop when it see return?

char* duplicate(char* string, int count)
{
    char* duped = (char*) malloc( sizeof(char)*(count*strlength(string)+1) );
    int i=0,j=0,t=0;
    for( i=0; i<count*strlength(string); i++,j++ )
    {   
        if (j==(strlength(string))) 
        {
            j=0; 
            t++;
        }

        if (t==count) 
        {
            duped[i] = '\0'; 
            return duped;
        }

        duped[i] = string[j];
    }
}
Michael Dorgan
  • 12,453
  • 3
  • 31
  • 61
onur
  • 132
  • 1
  • 1
  • 8
  • [Please see this discussion on why not to cast the return value of `malloc()` and family in `C`.](http://stackoverflow.com/q/605845/2173917). – Sourav Ghosh May 12 '16 at 18:44
  • 3
    Please fix the indentation, hope half of your problem will get sorted, if not more.\ – Sourav Ghosh May 12 '16 at 18:45
  • 1
    What is `strlength()`? Why are you using it rather than the standard `strlen()`? – Keith Thompson May 12 '16 at 18:48
  • it is the same, just i write the strlength. for no reason. – onur May 12 '16 at 18:49
  • There is information missing. See [ask]. – too honest for this site May 12 '16 at 18:51
  • 1
    "just i write the strlength. for no reason." - This is a bad reason. – too honest for this site May 12 '16 at 18:52
  • The answer to your question is simple: Yes, a function stops when it reaches the return statement. The problem is that you asked the wrong question and didn't show your complete code. – indiv May 12 '16 at 19:10
  • `t==count` never become true. – BLUEPIXY May 12 '16 at 19:12
  • That is the complete code, the other part is just calling it with these lines: char* result = duplicate("zoom", 3); printf("'%s'", result); The one works good prints "zoomzoomzoom", other one gives error. – onur May 12 '16 at 19:19
  • BLUEPIXY, then how the one with break statement works? – onur May 12 '16 at 19:21
  • 1
    It can't be the complete code because even you admitted that `strlength()` should be `strlen()`. Who knows how many other errors are embedded there. See [mcve] – KevinDTimm May 12 '16 at 19:42
  • There is a library included, which just includes the declarations and strlength(). I can mail it to you if you want to see it that much, yet there is no error or any other code related to the question I ask. – onur May 12 '16 at 19:44

1 Answers1

1

What if string is 0 length or count is 0? In your second case, you don't return a value.

This should be giving a compiler warning and will probably cause a crash as it returns whatever it wants.


Edit

Ok, the problem is deeper - you are never getting to the part of the code that sets the '\0' in the for loop with those initial conditions ("zoom" and 3). Therefore, your string could also not have that terminator set depending on how nice malloc is feeling. You need to set the NUL value if there is a string even if you exit the for loop. This is why the second was failing and not the first - it was indeed missing the return statement.

Michael Dorgan
  • 12,453
  • 3
  • 31
  • 61
  • That is not the case. String is "zoom" and count is 3. – onur May 12 '16 at 19:06
  • And it is throwing compiler warnings and has a definite fail case which invokes UB that you are ignoring. That said, you still haven't said which line or where the seg-fault occurs. Having that would help to narrow things down. – Michael Dorgan May 12 '16 at 19:09
  • Sorry for the inconvenience. I am just too newbie, all I see in terminal is "Segmentation fault (core dumped)". I'd be more than happy if you tell me how can I see which line causes the error. – onur May 12 '16 at 19:16
  • I took your code and ran it on an online compiler and it is indeed getting to the bottom of your function and failing - my guess is because the compiler is allowed to do whatever it wants when it returns from a function that doesn't have a proper return path. Place a return at the bottom of the function to handle this and I believe it will work for you. – Michael Dorgan May 12 '16 at 19:21
  • ..... a return at the bottom of the function `that returns a value` – KevinDTimm May 12 '16 at 19:43