Skip to main content
Share Your Experience: Take the 2024 Developer Survey
170 votes
Accepted

Why are floats still part of the Java language when doubles are mostly recommended instead?

LibGDX is a framework mostly used for game development. In game development you usually have to do a whole lot of number crunching in real-time and any performance you can get matters. That's why ...
Philipp's user avatar
  • 23.4k
146 votes
Accepted

When to optimize for memory vs performance speed for a method?

Instead of speculating about what may or may not happen, let's just look, shall we? I'll have to use C++ since I don't have a C# compiler handy (though see the C# example from VisualMelon), but I'm ...
Phil Frost's user avatar
  • 1,569
93 votes

Why do we still grow the stack backwards?

Does this mean the base pointer or the stack pointer are actually moving down the memory addresses instead of going up? Why is that? Yes, the push instructions decrement the stack pointer and write ...
Erik Eidt's user avatar
  • 34.1k
81 votes
Accepted

Why does a long int take 12 bytes on some machines?

It didn't take 12 bytes, it only took 8. However, the default alignment for an 8 byte long int on this platform is 8 bytes. As such, the compiler needed to move the long int to an address that's ...
Alex's user avatar
  • 3,912
69 votes
Accepted

What's the difference between a variable and a memory location?

A variable is a logical construct that goes to the intent of an algorithm, whereas a memory location is a physical construct that describes the operation of a computer.  Generally speaking, in order ...
Erik Eidt's user avatar
  • 34.1k
66 votes

When to optimize for memory vs performance speed for a method?

To answer the stated question: When to optimize for memory vs performance speed for a method? There are two things you have to establish: What is limiting your application? Where can I reclaim the ...
Berin Loritsch's user avatar
60 votes
Accepted

Does Garbage Collection Scan The Entire Memory?

I was reading a bit about garbage collectors and I am wondering if the garbage collector of a program scans the entire heap memory or what is allocated to it? That depends on the garbage collector. ...
Jörg W Mittag's user avatar
58 votes

Why are floats still part of the Java language when doubles are mostly recommended instead?

Floats use half as much memory as doubles. They may have less precision than doubles, but many applications don't require precision. They have a larger range than any similarly-sized fixed point ...
Jules's user avatar
  • 17.8k
58 votes
Accepted

Professional way to produce a large problem without filling up huge arrays: C++, free memory from part of an array

What you describe, "smoothing by fives", is a finite impulse response (FIR) digital filter. Such filters are implemented with circular buffers. You keep only the last N values, you keep an index ...
John R. Strohm's user avatar
48 votes

Why are floats still part of the Java language when doubles are mostly recommended instead?

Backwards Compatibility This is the number one reason for keeping behavior in an already existing language/library/ISA/etc. Consider what would happen if they took floats out of Java. Libgdx (and ...
8bittree's user avatar
  • 5,656
45 votes

When to optimize for memory vs performance speed for a method?

"this would reduce memory" - em, no. Even if this would be true (which, for any decent compiler is not), the difference would most probably be negligible for any real world situation. However, I ...
Doc Brown's user avatar
  • 209k
37 votes

Why are floats still part of the Java language when doubles are mostly recommended instead?

Atomic operations In addition to what others have already said, a Java-specific disadvantage of double (and long) is that assignments to 64-bit primitive types are not guaranteed to be atomic. From ...
Kevin J. Chase's user avatar
33 votes

When to optimize for memory vs performance speed for a method?

You can do better than both of those with return (abs(a + b) > 1000); Most processors (and hence compilers) can do abs() in a single operation. You not only have fewer sums, but also fewer ...
Graham's user avatar
  • 2,006
24 votes

How Are RAM Memory Addresses Determined

I think the other answer has confused you slightly, and again for low level questions like this I suggest you learn one of the 80s microcomputer or modern microcontroller architectures. saying that ...
pjc50's user avatar
  • 13.6k
23 votes
Accepted

Why is DateTime.Month an int?

int is used for almost all integer variables in .NET although often a smaller type would be enough. Also, unsigned types are almost never used although they could be. Some reasons: Signed and ...
usr's user avatar
  • 2,764
20 votes

What's the difference between a variable and a memory location?

Is it safe to say that a variable is the same thing as a memory location? No. Variable and memory location are two abstractions at two different abstraction levels. Variable and pointers are higher ...
Lie Ryan's user avatar
  • 12.4k
17 votes

What's the difference between a variable and a memory location?

Variables are language constructs. They have a name, reside within a scope, may be referenced by other parts of the code, etc. They are a logical entity. The compiler is free to implement this ...
cmaster - reinstate monica's user avatar
16 votes

When to optimize for memory vs performance speed for a method?

When is it appropriate to use Method A vs. Method B, and vice versa? Hardware is cheap; programmers are expensive. So the cost of the time you two wasted on this question is probably far worse than ...
John Wu's user avatar
  • 26.6k
16 votes
Accepted

Why does the base class need to have a virtual destructor here if the derived class allocates no raw dynamic memory?

When the compiler goes to execute the implicit delete _ptr; inside of the unique_ptr's destructor (where _ptr is the pointer stored in the unique_ptr), it knows precisely two things: The address of ...
Nicol Bolas's user avatar
  • 11.9k
15 votes

Why do we need to specify the type of data a pointer will hold, if all pointers are the same

From the memory-allocation point-of-view, you're right. A pointer variable on a 64-bit architecture occupies 8 bytes, no matter what type of pointer it is. But the C compiler needs to know more about ...
Ralf Kleberhoff's user avatar
14 votes

Can Rust replace the C or C++ programs in the Future?

Actually, the bug in the Generator Control Units that you refer to is not the kind of memory handling bug that Rust (or any language with fixed-size integers) can protect against. An internal counter ...
Bart van Ingen Schenau's user avatar
14 votes

Does Garbage Collection Scan The Entire Memory?

There are a lot of details in garbage collection. Lots lots lots lots lots. Each one of them has different behaviors, so it's hard to cover everything. Garbage collectors almost universally scan the ...
Cort Ammon's user avatar
  • 11.2k
13 votes

Professional way to produce a large problem without filling up huge arrays: C++, free memory from part of an array

Every problem can be solved by adding an additional level of indirection. So do that. You can't delete part of an array in C++. But you can create a new array holding just the data you want to keep, ...
Nicol Bolas's user avatar
  • 11.9k
13 votes
Accepted

Why does the C++ standard still allow uninitialized primitive variables?

The guiding principle there is still "You don't pay for what you don't use". It has not been changed to "Let's try to play it safe, and hope the compiler fixes our performance". ...
Deduplicator's user avatar
  • 9,051
12 votes
Accepted

Is it possible to update exactly 1 byte in RAM?

The internet. When you have multiple computers networked together there is no such thing as "the word size". Every computer has its own idea how big it's bus is. But they all agree on bytes (even if ...
candied_orange's user avatar
11 votes

When to optimize for memory vs performance speed for a method?

I would optimize for readability. Method X: private bool IsSumInRange(int number1, int number2) { return IsValueInRange(number1+number2, -1000, 1000); } private bool IsValueInRange(int Value, ...
Pieter B's user avatar
  • 13.2k
11 votes

How Are RAM Memory Addresses Determined

Reading from the other answers, I think one of misunderstandings you're operating under is that memory addresses are somehow globally unique, like IP addresses, MAC addresses, or phone numbers. That's ...
Alexander's user avatar
  • 4,954
10 votes

How does the Base Address Registers (BARs) in a PCI card work?

This question should be moved to stackoverflow. Maybe someone will do that. Am I correct? Essentially, yes. Each BAR holds the address of a communication area. This address can be set and read ...
Erik Eidt's user avatar
  • 34.1k
10 votes
Accepted

In C++, If a member function can be made static with no change to functionality, are there any performance or memory benefits from doing so?

Practically in 99.9% of cases: no. Theoretically: maybe. You won't be passing the implicit this parameter to every function, and not passing that could save you bytes and the time passing those bytes. ...
Philip Kendall's user avatar
10 votes

Does Garbage Collection Scan The Entire Memory?

No garbage collector scans the entire memory. Because, when reading some random memory content, it is not possible to guess if it's used or not. So garbage collection goes through objects or list of ...
Christophe's user avatar

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