String literals: Where do they go?

A common technique is for string literals to be put in “read-only-data” section which gets mapped into the process space as read-only (which is why you can’t change it). It does vary by platform. For example, simpler chip architectures may not support read-only memory segments so the data segment will be writable. Rather than try … Read more

In Java, what is the best way to determine the size of an object?

You can use the java.lang.instrument package. Compile and put this class in a JAR: import java.lang.instrument.Instrumentation; public class ObjectSizeFetcher { private static Instrumentation instrumentation; public static void premain(String args, Instrumentation inst) { instrumentation = inst; } public static long getObjectSize(Object o) { return instrumentation.getObjectSize(o); } } Add the following to your MANIFEST.MF: Premain-Class: ObjectSizeFetcher Use … Read more

How do I determine the size of my array in C?

Executive summary: int a[17]; size_t n = sizeof(a)/sizeof(a[0]); Full answer: To determine the size of your array in bytes, you can use the sizeof operator: int a[17]; size_t n = sizeof(a); On my computer, ints are 4 bytes long, so n is 68. To determine the number of elements in the array, we can divide … Read more

How dangerous is it to access an array out of bounds?

As far as the ISO C standard (the official definition of the language) is concerned, accessing an array outside its bounds has “undefined behavior“. The literal meaning of this is: behavior, upon use of a nonportable or erroneous program construct or of erroneous data, for which this International Standard imposes no requirements A non-normative note … Read more

When and why will a compiler initialise memory to 0xCD, 0xDD, etc. on malloc/free/new/delete?

A quick summary of what Microsoft’s compilers use for various bits of unowned/uninitialized memory when compiled for debug mode (support may vary by compiler version): Value Name Description —— ——– ————————- 0xCD Clean Memory Allocated memory via malloc or new but never written by the application. 0xDD Dead Memory Memory that has been released with … Read more

Understanding Memory Layout of C Programs [closed]

What is ‘code’ memory and it is a part of which memory (RAM or Flash memory)? Is one of the sections of a program in an object file or in memory, which contains executable instructions. Code and read-only data are stored in flash memory. When are the local and global variables allocated in memory — … Read more

Making sense of memory gibberish (C)

First off, the code won’t compile as is because you can’t define an array without a size. Assuming you did fix the array: char some[10]; Reading past the 10th element of the array invokes undefined behavior. This does not guarantee a crash. it just means you can’t predict the behavior of the program. It could … Read more

How ‘random’ is allocation of memory when I say “new Integer” in Java?

What algorithms are used? Java uses TLAB (Thread Local Allocation Buffers) for “normal” sized objects. This means each thread grab some Eden space and turns this grab of memory into individual objects. Thus small objects are typically sequential in memory for that thread, except if a new chunk of memory needs to be grabbed. Large … Read more