Now I don't understand why malloc isn't working

typedef union
{
int64_t int_d;
double number_d;
} Token_data;

typedef struct
{
Token_type type;
Token_data data;
} Token_t;

Token_t *my_token = (Token*) malloc(sizeof(Token_t))

@matrix is the "(Token*)" cast a typo on the post or is that direct from your code? I would expect that to not compile though so I doubt that's the issue

@Binkle malloc.c:2379: sysmalloc: Assertion `(old_top == initial_top (av) && old_size == 0) || ((unsigned long) (old_size) >= MINSIZE && prev_inuse (old_top) && ((unsigned long) old_end & (pagesize - 1)) == 0)' failed.

@matrix @Binkle I thinnk he’s looking for

printf("%d\n", sizeof(Token_t));

Which is a better question than mine tbh

@matrix @ademan is that after taking out the malloc statement and replacing it with the print statement? Or are you including both? If the latter, take out the malloc statement.

@Binkle @matrix Yeah that error would definitely come from the call to malloc(), so either putting the print before that, or taking out the malloc() call should eliminate that error.

@Binkle @ademan Wtf, now that I commented out a previous few lines, the print works and says 24.
Fuck. I think a previous function might be doing something weird. Malloc seems to work

@matrix @Binkle oh, can we get the whole program verbatim? sounds like you’re clobbering your memory (welcome to C)

@Binkle @ademan @matrix
subtext:

"FOR THE LOVE OF GOD, LET ME FUCKING SEE THAT SHIT. I WANT TO SLEEP TONIGHT!!!!!!!!"

@ademan @Binkle Sorry, no, it's a school project, but I think I found the issue.
I'm getting the wrong sizeof because I'm stupid and typed in the wrong struct.

void symtable_init(symtable_t **table){
(*table) = (symtable_t *)malloc(sizeof (symtable_item_t));
for(int i = 0; i < TABLE_SIZE; i++){
(**table)[i] = NULL;
}
}

@ademan @Binkle Yes it was the issue. God I'm dumb. Thank you for help.

@matrix @ademan ah man a compilers class in C? Jealous. I didn't get the time to take a compilers course, but I wish I had.

If you find yourself banging your head against a wall, tag me :comfy:

@Binkle @ademan Yep, it's compilers :lul: We have it mandatory.
I might ping you then, thanks.

@Binkle @matrix The pointer gets casted to Token_t* when it's actually assigned anyway. You can do something like
int* c = (float*) malloc(sizeof(int));
*c = 1;
printf("%d", *c);
And it will just print a warning and that's it

Sign in to participate in the conversation
Game Liberty Mastodon

Mainly gaming/nerd instance for people who value free speech. Everyone is welcome.