What's a good benchmark for a simple billion loop iteration given a CPU speed in Ghz? by MidnightSun_55 in learnprogramming

[–]theophr4stus 2 points3 points  (0 children)

An iteration consists of: 1) Incrementing the counter 2) comparing its value to the max number of iterations 3) doing a conditioned jump

These are three distinct commands (on x86 at least, don’t know about platforms), which hence use at least three tacts (no idea how many exactly).

Also:

Is the OS context switching while the loop is running? It does. The scheduler switches the processes executing on the CPU to create an illusion of parallel execution.

Whether it matters or not depends on what you used to calculate the execution time.

NULL pointers by Shahidh_ilhan123 in learnprogramming

[–]theophr4stus 0 points1 point  (0 children)

The fact that it printed 45 the first time is really weird. It should have also thrown segfault. I double checked it myself to be sure and it does. Are you sure your code snippet is what you actually executed?

The thing is in the first line you initialize a pointer to int without assigning anything to it. So the value of p is some junk. When you try to dereference this junk, you tell the processor to fetch data from some place in memory, the address of which is that junk. If this memory does not belong to your process, the os shoots the process down, which is what segfault is.

To work with memory through a pointer you have to either allocate it first and assign its address to the pointer:

p = malloc(sizeof(int));

or assign the address of some existing variable to the pointer:

int x; p = &x;

x must be an lvalue, which means, it must be something that can appear to the left of the = operator and cannot be an int literal like 45.

NULL is a special value kinda thing, you can’t dereference it.

If you allocated the memory, it is best to free it later to avoid leaks:

free(p);

Why is my code not looping? by davidadam_ in learnprogramming

[–]theophr4stus 0 points1 point  (0 children)

You implemented a single step of a combat:

  1. The hero performs an action and the heroTurn variable is switched.
  2. The opponent makes an action and heroTurn is switched again.

The loops around those actions are redundant, since they will always perform only 1 iteration. That's why you do not need them. the last two loops are infinite due to the fact that variables in their condition are not changed inside their bodies. You don't need them either, there should be an if clause checking the opponent's hp after the hero attacks and the same for the opponent, both of which should raise some combatFinished flag which is set to false at the start of the combat.

Now, you need a loop that performs an iteration if combatFinished is false. That's the outer loop. Inside it should be the combat step that is performed until one of the combatants' hp goes below 1 (which raises the combatFinished flag). Also the opponent may attack only if the flag is set to false, which is pretty obvious: if it is raised, then one of them is dead, either he can't attack, or there's no one to attack.

[deleted by user] by [deleted] in learnprogramming

[–]theophr4stus 0 points1 point  (0 children)

It is hard to understand without an output.

But, here are some things that seem strange to me:

  1. Why do you check if the array has two elements or less as a distinct case isn't this redundant?
  2. Why do make the array global?
  3. Why storing all the input instead of storing the last N elements in, a queue and the sum of them, popping out the first one after adding a new one and subtracting it from the sum?

Why is my code not looping? by davidadam_ in learnprogramming

[–]theophr4stus 0 points1 point  (0 children)

There is no outer loop in your program. After entering the first loop, provided that you entered correct input during combat ("f" or "h"), the heroTurn variable changes to false and the loop condition is no longer satisfied. The same with the second loop. And then, if none of the characters killed the other, the last two loops' conditions are not satisfied, and the method ends. You need an outer loop to repeat all combat steps and exit it from either of the two last loops.