I am trying to use a struct that has strings of an unknown size, but can't get things to work...
Assume with the text below that you don't know what the lengths of a_word and another_word will be, meaning that you can't define the char array length when specifying the struct. So after declaring the struct, I want to assign these unknown strings to its attributes. Then I want to print the attributes and finally free the memory.
However, as it stands only the number prints out and this is weird (i.e. not 100, changes each time I run the code). After that I get an AddressSanitizer:DEADLYSIGNAL error.
#include <stdio.h>
#include <stdlib.h>
#include <regex.h>
#include <string.h>
typedef struct Object {
unsigned short a_number;
char *a_word;
char *another_word;
} Object;
Object *create_object() {
Object my_object;
Object* p_my_object = &my_object;
return p_my_object;
}
void set_word(Object **my_object, const char *a_word) {
(*my_object)->a_word = malloc(strlen(a_word) + 1);
strncpy((*my_object)->a_word, a_word, sizeof((*my_object)->a_word) + 1);
}
void set_another_word(Object **my_object, const char *another_word) {
(*my_object)->another_word = malloc(strlen(another_word) + 1);
strncpy((*my_object)->another_word, another_word, sizeof((*my_object)->another_word) + 1);
}
void set_number(Object **my_object, unsigned short a_number) {
(*my_object)->a_number = a_number;
}
void clear_memory(Object **my_object){
free((*my_object)->a_word);
free((*my_object)->another_word);
}
int main()
{
unsigned short a_number = 100;
char *a_word = "apple";
char *another_word = "banana";
Object *my_object;
my_object = create_object();
set_word(&my_object, a_word);
set_another_word(&my_object, another_word);
set_number(&my_object, a_number);
printf("a_number: %hu\n", my_object->a_number);
printf("a_word: %s\n", my_object->a_word);
printf("another_word: %s\n", my_object->another_word);
clear_memory(&my_object);
return 0;
}
Can anyone see what I'm doing wrong here?