Just to note I am a noob at C and am very new at the concept of using malloc, calloc and realloc. My situation is this, so I am trying create a lexer in C. I have simplified all the code to the minimum where it still produces the same error.
typedef struct TOKEN_STRUCT {
char* value;
} Token;
typedef struct LEXER_STRUCT {
char* src;
int src_size;
char curr;
int index;
Token** tokens;
int num_tokens;
} Lexer;
These two structs represent the two structs that I use which are each token and the lexer.
void lexer_tokenize_file(Lexer* lexer) {
int size = 30;
Token** tokens = malloc(size * sizeof(struct TOKEN_STRUCT));
int index = 0;
while (lexer->index < lexer->src_size) {
tokens[index] = lexer_advance_once(lexer);
size = lexer_resize_token_arr(tokens, index, size);
index++;
}
lexer->num_tokens = index;
lexer->tokens = tokens;
}
I use this function to go through each character for in the lexer->src
using the function lexer_advance()
and if the Token** token
has reached the max size I am supposed to resize. and return the new size of the tokens.
Token* lexer_advance_once(Lexer* lexer) {
char* token_buffer = calloc(2, sizeof(char));
token_buffer[0] = lexer->curr;
token_buffer[1] = '\0';
lexer->index++;
lexer->curr = lexer->src[lexer->index];
return init_token(token_buffer);
}
int lexer_resize_token_arr(Token** tokens, int index, int size) {
if (index == size) {
size += 2;
tokens = realloc(tokens, size * sizeof(struct TOKEN_STRUCT));
return size;
}
return size;
}
The Error is get is
malloc: *** error for object 0x14ee067d0: pointer being realloc'd was not allocated
I'm assuming the error is in the resize function but I don't understand why I am getting the error in the first place. I hope someone can explain why I am getting the error in the first place and help me debug this issue. This is my first question here so sorry if the question is not well formatted I did my best.