0

I have C code that includes header and c files. In header which is string.h I defined the functions and implement in them in string.c and in the main.c I am testing my program. I want to allocate memory for char and check if it is okay then I want to free. But I got error from visual studio code that "The breakpoint directive (__debugbreak() statement or similar call) was executed in StringC.exe." or "Acces violation to read error"

Here is my string.h :

#include<stdio.h>
#include<stdlib.h>
typedef enum
{
    string_return_success,
    string_return_allocation_error,
    string_return_source_overflow,
    string_return_destination_overflow,
    string_return_not_found

} StringReturnType;

typedef unsigned int StringSizeType;

StringReturnType stringCreate(char *str, StringSizeType size);

void stringDestroy(char *str);

Here is my string.c :

StringReturnType stringCreate(char *str ,StringSizeType size)
{
    char temp = *str;
    str = (char*)malloc(sizeof(char) * (size));
    *str = temp;
   
    if (*str == NULL) {
        printf("burdayim");
        return string_return_allocation_error;
    }
    printf("%c", *str);
   
   
}

void stringDestroy(char *str)
{
    
    free(str);
}

And here is the main:


int main()
{

    char str = 'h';
    char* ptr = &str;

    stringCreate(ptr, sizeof(str));
    stringDestroy(ptr);


    return 0;
}
Aconcagua
  • 24,880
  • 4
  • 34
  • 59
  • Of course you get a segmentation fault – you try to free the locally allocated `char` variable `str`. Note that you assign its address to `ptr` but never change the latter. – Aconcagua Oct 27 '22 at 08:05
  • You might have intended to exchange the content of `ptr` within `createString`, but that doesn't work that way. Whenever you pass a pointer to a function you pass a *copy* of. Then modifying the pointer in the function means modifying the copy while leaving the original untouched. Maybe a good time to peak into a good [C book](https://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list) to get a better understanding of how pointers work. You might have intended to change the content of ` – Aconcagua Oct 27 '22 at 08:06
  • 1
    Changing the a pointer passed to a function requires it being passed by its address, i.e. creating a pointer to pointer: `create(char** ptr) { *ptr = ...; } int main(void) { char* ptr = ...; create(&ptr); return 0; }`! – Aconcagua Oct 27 '22 at 08:13
  • @Aconcagua Thank you for your good explaining. First I did try the double poıinter but that time I could not manage it. Now with your explanation I manage thank you so much!! And If you have any suggestions that I should look up please share thank you! – Havva Fatma Özbay Oct 27 '22 at 08:22
  • Oh, just noticing: As is `*str == NULL` is wrong, too, it compares a character against a pointer, but worse, if allocation failed, it would try to dereference the null pointer. So when fixing your code to pointer to pointer, just leave this line as is, don't change it to `**str == NULL`, that would re-introduce this error! – Aconcagua Oct 27 '22 at 08:43
  • Aside: `sizeof (char)` is **guaranteed** to be `1`. When `size` is `1`, as it is here, `malloc(sizeof (char) * size)` is the same as `malloc(1)`. The only actual [string](https://en.cppreference.com/w/c/string/byte) this can hold is the empty string (just the null terminating byte). A single nonzero byte does not constitute a string. – Oka Oct 27 '22 at 08:54

0 Answers0