Hello, Daniel, function: void parser_datatype_adjust_size_for_secondary(struc...

George Chow Answered 4 answers
Question by George Chow 2 years ago

Question

Hello, Daniel, function:

void parser_datatype_adjust_size_for_secondary(struct datatype* datatype, struct token* datatype_secondary_token)
{
if(!datatype_secondary_token)
return;

struct datatype* secondary_data_type = calloc(1, sizeof(struct datatype));
parser_datatype_init_type_and_size_for_primitive(datatype_secondary_token, NULL, secondary_data_type);
datatype->size += secondary_data_type->size;
datatype->datatype_secondary = secondary_data_type;
datatype->flags |= DATATYPE_FLAG_IS_SECONDARY;
}

when I declared a variable like this:

short int a;

the result of datatype->size will be 6( 2 + 4), Is it correct? I am a little bit confused, My be I don't understand secondary data type well, can you give me an example?

Answers

Answered by Daniel McCarthy

Instructor

Hi,
Yes take a look here: https://en.wikipedia.org/wiki/C_data_types so the secondary data type is required as you can stack them as can be seen in the wikipedia article, you can have a long long and so on.

The code you provided will add the size of the secondary type yes
Thanks

Follow-up by George Chow

Question author

Thank you for your help, but one thing I still don't understand, the size of short int is 2, why we set it to 6?

Answered by Daniel McCarthy

Instructor

Hi your right it should be two bytes, if you can file an issue report here: https://github.com/nibblebits/PeachCompiler I would appreciate it.

Follow-up by George Chow

Question author

OK, I already filed a an issue report.