Hot answers tagged

4

This is an excellent question. Understanding this behaviour means knowing a bit about the history of the C language, and the design and development of CPUs. The short answer is that the size difference stems from the fact that older computers were much simpler than modern computers. In the early days of microcomputers before PCs (circa 1970), the CPUs at ...


2

The walkthrough was filmed on the CS50 Appliance (which is a 32-bit machine). I assume you are trying to run the program using this year's CS50 IDE, which is a 64-bit machine. As such, the sizeof() function will be returning unsigned long values rather than int values. You will need to change your print format from %i or %d to %lu. I think the compile ...


2

An int is 32 bits (4 bytes) on the appliance. The size of a 5-int array is 20 bytes (5 ints * 4 bytes each). The sizeof() operator is used to calculate the size of any datatype in bytes. For more information, you may read this wikipedia article!


1

It's always the simple ones that hide in plain sight. ;-) Look very carefully at this: Node* temp = malloc(sizeof(Node*)); The code allocates memory space that is sizeof what? A node or a pointer? As a side note, why does the code increment i inside the for loop and as the index of the for loop? If this answers your question, please click on the check ...


1

You're getting 12 because of something called alignment, which adds internal padding to structures. You can read more about it here: http://c-faq.com/struct/align.html https://en.wikipedia.org/wiki/Data_structure_alignment A quick workaround for this would be to use __attribute__((__packed__)) like you've used in pset4 (see bmp.h there). Like this: #...


1

You're overthinking this. A struct is not necessary here. Why not try storing your identifiers in an array instead?


1

quoting the C99 standard (6.5.3.4 - 20): C89, like K&R, defined the result of the sizeof operator to be a constant of an unsigned integer type. Common implementations, and common usage, have often assumed that the resulting type is int. Old code that depends on this behavior has never been portable to implementations that define the result to ...


1

Don't confuse hexadecimal with a type of number. It is simply a way to write the binary values in a different way. Each byte in the JPG is 8 bits long. Those 8 bits can be represented in binary, in hexadecimal, in decimal, etc, although those will all essentially mean the same thing. So the first byte of a JPG is the value 0xff (in hex), or 255 (in ...


1

What are you hoping to get from using sizeof()? I'm unsure why you have such an array for greedy at all. Might it be easier to use an integer counter that increments every time you calculate a coin?


Only top voted, non community-wiki answers of a minimum length are eligible