How to create an empty array?

You cannot make an empty array and then let it grow dynamically whenever the user enters a number in the command line. You should read the numbers and put them in an ArrayList instead. An ArrayList does not require a initial size and does grow dynamically. Something like this: Ofcourse you won’t use while(true)and you won’t …

Read more

getopt_long() — proper way to use it?

First off, you probably don’t want 0 for the has_arg field – it must be one of no_argument, required_arguemnt, or optional_argument. In your case, all of them are going to be required_argument. Besides that, you’re not using the flag field correctly – it has to be an integer pointer. If the corresponding flag is set, getopt_long() will fill it in with the integer you passed in via …

Read more

Categories C Tags

why use 0xffff over 65535

The hexadecimal notation 0xffff makes it clear that all bits in the number are 1. The 65535 is the same number, but the binary representation isn’t as obvious. It’s only a count number. So this way of writing numbers has different semantics. For example, if you want to declare that the maximum value of some variable must be 65535, then …

Read more

Static array vs. dynamic array in C++

Local arrays are created on the stack, and have automatic storage duration — you don’t need to manually manage memory, but they get destroyed when the function they’re in ends. They necessarily have a fixed size: Arrays created with operator new[] have dynamic storage duration and are stored on the heap (technically the “free store”). They can …

Read more

Division in C++

In the expression 1 / 6, both numbers are integers. This means that this division will perform integer division, which results in 0. To do a double division, one number has to be a double: 1.0 / 6 for example.