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 ...
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 ...
113
votes
Accepted
Can the "level 256 bug" in the game of Pacman be considered an unhandled segfault?
Definitely not.
Accessing a memory address you didn't allocate is always a programming error. And acting on the information you get out of it produces undefined behavior, that much is accurate. I ...
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 ...
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 ...
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 ...
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 ...
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. ...
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 ...
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 ...
48
votes
If a number is too big does it spill over to the next memory location?
No, it does not. In C, variables have a fixed set of memory addresses to work with. If you are working on a system with 4-byte ints, and you set an int variable to 2,147,483,647 and then add 1, the ...
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 ...
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 ...
43
votes
Accepted
Why are C string literals read-only?
Historically (perhaps by rewriting parts of it), it was the contrary. On the very first computers of the early 1970s (perhaps PDP-11) running a prototypical embryonic C (perhaps BCPL) there was no MMU ...
38
votes
Can the "level 256 bug" in the game of Pacman be considered an unhandled segfault?
It looks like you're confusing "undefined behaviour" and "segmentation fault".
There is no such thing as an unhandled segfault. A segmentation fault is error handling, by definition.
If you don't ...
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 ...
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 ...
31
votes
Accepted
Redux memory consumption
This is a valid concern. While I have not measured the memory usage of Redux applications, I think that before committing to use Redux (or any other framework for that matter) you should create stress ...
28
votes
Is it possible to statically predict when to deallocate memory---from source code only?
RAII is not automatically the same thing, but it has the same effect. It provides an easy answer to the question "how do you know when this cannot be accessed any more?" by using scope to cover the ...
24
votes
If a number is too big does it spill over to the next memory location?
Signed integer overflow is undefined behavior. If this happens your program is invalid. The compiler is not required to check this for you, so it may generate an executable that appears to do ...
24
votes
Can the "level 256 bug" in the game of Pacman be considered an unhandled segfault?
Neither of these terms is appropriate for a bug in an arcade game that was programmed in assembly language and runs without benefit of memory-protection hardware or operating system.
"Undefined ...
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 ...
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 ...
22
votes
Accepted
Is it possible to statically predict when to deallocate memory---from source code only?
Take this (contrived) example:
void* resource1;
void* resource2;
while(true){
int input = getInputFromUser();
switch(input){
case 1: resource1 = malloc(500); break;
case 2: ...
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 ...
17
votes
Accepted
Does Time Complexity analysis factor for cache performance of an algorithm?
Time Complexity (Big-O notation) does not measure performance of an algorithm. Instead, it categorizes how an algorithm's resource use scales with input size. This allows us to compare two algorithms ...
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 ...
16
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 ...
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 ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
memory × 313c++ × 53
c × 46
memory-usage × 37
cpu × 24
java × 23
performance × 21
operating-systems × 19
architecture × 14
allocation × 12
pointers × 11
garbage-collection × 11
computer-architecture × 11
c# × 10
stack × 10
array × 9
heap × 9
data × 8
caching × 8
design × 7
algorithms × 7
database × 7
optimization × 7
malloc × 7
object-oriented × 6