-1

This is the code I have so far:

char * duplicate(char *str)
{
    int len = strlen(str) + 1;
    char *ptr;
    ptr = (char *)malloc(len);
    strcpy(ptr, str);
    str = (char *)realloc((void *)str, len * 2 * sizeof(char));
    strcat(str, ptr);
    free((void *)ptr);
    present(str);
    return(str);
}

sidenote: I know I should be checking if the memory is actually allocated, I will do so later.

The program asks the user to input a string, proceeds to double it and prints out the new string. present->a function I made so it would print the string out.

Now my thought process was as follows:

1) Allocate memory to a placeholder pointer, copy str string onto ptr

2) Reallocate memory for str, now with double the size

3) Using strcat to copy the string

4) Freeing ptr memory

Output should be as follows:

string input by users for example: "Hi"

output: "HiHi"

If we run the function again output should be: "HiHiHiHi"

The problem I am facing is, after running the function around 2-4 times (depends on the input string size) the program will crash, it will say it had a trigger point and something along the lines of: "wntdll.pdb not loaded".

I believe it has something to do with the memory, because if I am trying to duplicate larger strings, it will simply crash after the first time.

Am I allocating memory properly? I am not sure what to do at this point.

Thanks for the help :)

DeiDei
  • 10,205
  • 6
  • 55
  • 80
  • 1
    [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 Jan 18 '17 at 20:32
  • [`strdup`](http://en.cppreference.com/w/c/experimental/dynamic/strdup) – 101010 Jan 18 '17 at 20:33

2 Answers2

0

You can avoid all the boilerplate by just using strdup:

char *s1 = "String";
char *s2 = strdup(s1);
...
free(s2);

Your function would become:

char* duplicate(char const *str) {
  char *ptr = strdup(str);
  ptr = (char*) realloc(ptr, 2 * strlen(str) + 1);
  strcat(ptr, str);

  return ptr;
}

Live Demo

101010
  • 41,839
  • 11
  • 94
  • 168
  • Thanks, it does clear stuff up! any idea about my other problem? because the string gets duplicated, but crashes if it gets too long. – TricksterJoe Jan 18 '17 at 20:40
  • @Dimakossover how long? – 101010 Jan 18 '17 at 21:08
  • If the input string is : "hi", it will copy it twice, so it becomes hi->hihi->hihihihi->hihihihihihihihi->At this point, it will crash. (after calling the duplicate function again.) – TricksterJoe Jan 18 '17 at 21:10
  • Making an educated guess, I think that returning the input string in your version is not sane. You see, you're actually changing the input buffer. – 101010 Jan 18 '17 at 21:17
  • Hello @101010 i actually forgot to pass over the new value to ptr_s in main from the code, now that I've done it it's all good, no crashes and very smooth, thank you for you attention and help, greatly appreciated ! – TricksterJoe Jan 18 '17 at 21:38
0

You don't need that extra allocation:

char * duplicate(char *str)
{
    size_t len = strlen(str);
    char *ret = realloc(str, 2*len+1); //assuming no overflow
    if(ret){
        memcpy(ret+len, ret, len);
        ret[2*len]=0;
    }
    return ret;
}
Petr Skocik
  • 58,047
  • 6
  • 95
  • 142
  • Thanks, this is very informative! my question is, what does memcpy doe? (obviously it copies memory blocks) but how can i recreate without the use of this function? thanks in advance. – TricksterJoe Jan 18 '17 at 20:54
  • After using your code, same thing happens, i can duplicate a string a few times and after the 3rd-5th time program crashes. – TricksterJoe Jan 18 '17 at 20:56
  • @Dimakossover Show us the entire code. Maybe you do something illegal like `duplicate("foo");` or `char *foo = "foo"; duplicate(foo);`. (You see why that's illegal, right?) – David Schwartz Jan 18 '17 at 20:59
  • hi @DavidSchwartz i copied the relevant code here:http://pastebin.com/VssF3wBe , thanks in advance. – TricksterJoe Jan 18 '17 at 21:02
  • @Dimakossover Since you have so much code that messes with `ptr_s`, the bug could be anywhere. I'd look at `get` first. (Or give us enough code to reproduce the problem.) – David Schwartz Jan 18 '17 at 21:04
  • With 6 duplications you need 64 times as (2^6) as much memory. If your strings are large, the allocations might be failing. Don't forget to check the return codes to makes sure you're not getting NULLs. – Petr Skocik Jan 18 '17 at 21:05
  • this is my get() function : http://pastebin.com/AAfrdXKL , i did notice that the maximum length of my initial string input should not be larger then 250, (LEN is defined 250) , but after getting input from user once, i do not mess with get() no more. – TricksterJoe Jan 18 '17 at 21:08