New answers tagged

2

Generally speaking, including extra header files shouldn't increase the size or impact the performance of your compiled code, but it's still a very bad practice. When you're working on a project that has hundreds or thousands of source files, you want to rebuild as few files as possible when making a change to a header file. If every source file includes ...


1

If you're talking of C or C++, then no - all that happens is that your compile time is slowed as the compiler has to read and parse all the extra header files. This can be significant, especially if headers include other headers that include.. you get the idea. Compilation time can increase dramatically (so much so that some compilers like visual studios ...


1

As said before no it's not a seg fault. I'll add why the problem occurs: it's an overflow. Level number are store on a byte so the range is 0-255. Each time you complete a level the counter is incremented. At level 256 the counter is in fact 0 due to the overflow. However the game try to display some fruits on the bottom of the level. The fruit number/type ...


1

I have done quite a bit of work on this and described some of it here. I benchmarked the Boehm GC in C++, allocating using malloc but not freeing, allocating and freeing using free and a custom-built mark-region GC written in C++ all vs OCaml's stock GC running a list-based n-queens solver. OCaml's GC was faster in all cases. The C++ and OCaml programs were ...


8

The level-256 bug in Pac Man results in the program reading data which is beyond the end of the intended table, but is still readable storage, and writing to portions of the screen which are beyond those which the program intends to write, but are still well within the areas of the screen that the program is allowed to write. No other areas of memory are ...


22

"Undefined behaviour" is a term-of-art in C and related languages, coined by the C standards committee back in 1989. "Segmentation fault" is a term-of-art in POSIX, derived from PDP system programming jargon. Neither is appropriately applied to a bug in an arcade game that was programmed in assembly language and runs without benefit of memory-protection ...


35

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 have an OS that detected the bad memory access and terminated the process for safety, then you don't have a segmentation fault. If anything, then, this is a ...


107

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 have no idea what platform the original Pac-man was written for, but I'm pretty sure it exhibited this behavior just like any other von Neumann machine. However, ...


1

Your questions are very abstract and therefore mostly quite difficult to answer precisely, but here are some thoughts you may not have considered. Where is the list stored in memory? Is it literally stored in a way where first element is at memory index k, next element k+1, etc. That depends on what you mean by "list"! A C++ vector is stored ...


0

Look at your hands. With the five fingers on one hand you can represent 32 different numbers using a binary encoding. With the ten fingers on both hands you can represent 32 x 32 = 1,024 different numbers. Double the memory means the number of possible states is squared. And I'd love to see you trying to represent a million numbers using fingers and ...


4

First (assuming C99 standard), you may want to include <stdint.h> standard header and use some of the types defined there, notably int32_t which is exactly a 32 bits signed integer, or uint64_t which is exactly a 64 bits unsigned integer, and so on. You might want to use types like int_fast16_t for performance reasons. Read others answers explaining ...


14

So, there are two things here: the language level: what are the semantics of C the machine level: what are the semantics of the assembly/CPU you use At the language level: In C: overflow and underflow are defined as modulo arithmetic for unsigned integers, thus their value "loops" overflow and underflow are Undefined Behavior for signed integers, ...


4

To further @StevenBurnap's answer, the reason this happens is because of how computers work at machine-level. Your array is stored in memory (e.g. in RAM). When an arithmetic operation is performed, the value in memory is copied into the input registers of the circuit that performs the arithmetic (the ALU: Arithmetic Logic Unit), the operation is then ...


24

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 something reasonable, but there is no guarantee that it will. However, unsigned integer overflow is well-defined. It will wrap modulo UINT_MAX+1. The memory not ...


47

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 variable will usually contain -2147483648. (On most systems. The behavior is actually undefined.) No other memory locations will be modified. In essence, ...


0

A class is not a monolithic template. It is an abstract concept. It has methods, which are executable code. That code will be in the "code segment". The template you refer to would be a structure that contains initial values for data members and possibly a virtual method table. Those would be in the "data segment". That is, once everything is native code ...


2

Class definitions are stored in a separate area (neither stack nor heap) called the method area. In .net the corresponding area is called the Loader Heap. Data in the method area is written by the class loader, and it is never garbage collected and cannot be deleted.


0

Stack memory is used to store local variables and function call. While heap memory is used to store objects in Java. No matter, where object is created in code. Where will the primitive a in class A be stored? In this case primitive a is associated with class A object. So it creates in Heap Memory. Why does heap memory exist at all? Why ...



Top 50 recent answers are included