Either I'm an idiot, or there is something seriously wrong with how free() works on Linux.
Blake Bailey
bump
pls help
Carson Butler
string is defined in string.h, and you're using it as a variable name in both dString_init and dString_delete. I don't think it's causing the problem, but even if it somehow compiles and runs, it's exceedingly bad practice to use 'string' for a variable name.
John Baker
Works fine on cpp.sh. Want me to reboot into Linux and try it?
Carter Hernandez
Nevermind, apparently C's string.h does not directly define a string type. Still a bad idea to name variables 'string'.
Your pointer is the problem. Try to understand what variables and their addresses in C mean, especially when pointers are involved. When you write dString* string; you create a new pointer variable. You didn't initialize it with anything so it points to something undefined. You then pass the address of that variable into the init function. There you initialize a new pointer. But that is a new pointer variable. And you allocate memory from that pointer which is not the same as in your main function. So the pointer in your main function is still undefined. And passing a null pointer to free results in a segfault.
Anthony Perry
Why even pass in dString** instead of just a dString*?
Jack Rogers
This is my new code taking your suggestions into account. It compiles but the string doesn't get printed out
#include #include #include
typedef struct dString_struct{ int size; char* data; }dString;
These: Because otherwise the pointer in main will not be changed to whats allocated in dString_init.
Adrian Howard
In the main function there's a dString* variable. If you want to write a separate function that modifies this variable you need to pass that variable as a pointer, hence dString**.
Isaiah Diaz
You don't write properly. The resize is triggered but you don't strcpy after the resize. Try
Also, it wasn't that the the print somehow failed like you implied. A debug tip: If you had tried writing the numeric value of the char (yes, you wrote the char and not the string, not sure if intended) you would have seen that the write must have failed.
Jaxson Russell
I know, I was just doing some testing.
Dominic Lee
Why not just something like this? IMO you should avoid memory management where possible.
#include
typedef struct { int length; char* data; } string;