0

As written in title, the exception "project has triggered a breakpoint" is thrown. I have searched for an answer in the forum, but I have seen that this exception is thrown in a variety of cases, and I couldn't indicate a solution to this exception in my program. The interesting thing is that sometimes the program runs as it should, without any exceptions, and sometimes it just does not. I think it relates to a memory issue, but I can't find something wrong. Thus, I will be glad to get your help.

The code:

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

int main()
{
    char* str;
    char character;
    int i = 0;

    str = (char*)malloc(sizeof(char)*1 + 1 ); //+1 for the '\0'

    if (str == NULL) return 1;

    printf("Please enter a character: ");
    scanf_s(" %c", &character);

    while (character != 'X')
    {
        str[i] = character;
        i++;
        str[i] = '\0';
        realloc(str, sizeof(char)*1 + strlen(str) + 1);//+1 for the '\0'
        printf("Please enter a character: ");
        scanf_s(" %c", &character);
    }
    str[i] = '\0';
    
    printf("%d\n", strlen(str)); //Debug.
    puts(str);                   //Pring string.

    free(str);
    return 0;
}

If my question is not clear enough, write it in comments and I will be glad to clarify the question.

Thank you very much beforehand!

Modern
  • 75
  • 2
  • 9
  • 1
    You are not using `realloc` correctly. It does not guarantee that the resulting address will be the same as original, yet you are discarding the result. – Eugene Sh. Nov 23 '21 at 17:46
  • @EugeneSh. Thanks for replying. What will be the right way to do it then? – Modern Nov 23 '21 at 17:48
  • You assign the result value to a *different* pointer, check it for NULL, and if it is not, reassign it back to original pointer. https://stackoverflow.com/questions/13748338/how-to-use-realloc-in-a-function-in-c - check the answer of `user8036942` there – Eugene Sh. Nov 23 '21 at 17:50
  • [proper use of realloc](https://stackoverflow.com/questions/21006707/proper-usage-of-realloc) – yano Nov 23 '21 at 17:50
  • Just something to consider, allocating a string one character at a time isn't efficient. Maybe `realloc` 10 at a time, or double the string length each time you need more space. – yano Nov 23 '21 at 17:53
  • 1
    also adding `\0` in each iteration does not seem necessary. – Eugene Sh. Nov 23 '21 at 17:54
  • Thank both of you for replying and helping. I read what it is written in both of the links you published. I deleted the '\0' in the loop, you are right, it is unnecessary. Do I have to create another pointer to save the result of the realloc function? I mean I created another variable, char * reallocResult and changed what was written to: reallocResult = (char*)realloc(str, sizeof(char)*1 + strlen(str) + 1); if (reallocResult == NULL) { free(str); exit(1); } else str = reallocResult; – Modern Nov 23 '21 at 18:11
  • 1
    yes, that's the `realloc` paradigm (you don't have to `free` and `exit`, you can handle the error in other ways). Make sure you read the [manpage for `realloc`](https://linux.die.net/man/3/realloc) so you understand why it should be done that way: "The `realloc()` function returns a pointer to the newly allocated memory, ... and **may be different** from (the original) `ptr`, or **NULL if the request fails**." – yano Nov 23 '21 at 20:39
  • @yano I truely thank you for replying and helping. If I use the same pointer twice, which means, as paramter and as result, is that fine? I mean: `str = (char*)realloc(str, sizeof(char)*1 + strlen(str) + 1);` Is that legal (of course that after that I check if str is not NULL) ? – Modern Nov 23 '21 at 20:50
  • 1
    that's precisely why you need a temporary pointer. If you do `ptr = malloc(..)` then some time later do `ptr = realloc(..)`, what if `realloc` returns NULL? If so, now `ptr == NULL`, and you've lost your pointer to your original data, creating a memory leak. Instead, you do `tempPtr = realloc(..)`, and if `tempPtr != NULL`, you're safe to assign `ptr = tempPtr`. If `tempPtr == NULL`, then `ptr` still points to your original memory, nothing is lost. If `realloc` returns NULL, the original memory block is untouched. – yano Nov 23 '21 at 20:58
  • 1
    If `realloc` succeeds, then it may 1) simply add space on to the current block, or 2) it may allocate an entirely new block somewhere else, move the contents from the old to the new block, and `free` the old block. As the user of a successful call to `realloc`, you don't particularly care whether 1 or 2 happened, but you do need to handle failures (eg, `realloc` returns NULL). – yano Nov 23 '21 at 21:06
  • 1
    @yano Thank you for the great explanation! – Modern Nov 26 '21 at 10:52

0 Answers0