I've been told to study up on four c segments: code, data, stack, heap. I know that googleing stack and heap will probably give me a mountain of information. But what the heck is code and data? If I type c code into google I'M going to get a lot of c source code and I don't think that is what I'M ment to study here. If I type c data I suspect I'M going to find a lot of pages concerning data types. When looking at the four secments of c, how can I research code & data? Thanks.

A better question to ask google is "what's the difference between code and data?"

Below is a very very brief explanation. More in-depth explanation is found in this article

All programs are divided into several segments
1. code segment: This is where the program instructions are stored. When you compile your program all the instructions are stored in an area of memory called the code segment. On MS-Windows and *nix this area of memory is read-only. Any attempt to write into this area by your program will crash the program.

  1. Data segment. This is read/write area of memory where all your program's data is stored.

  2. Stack segment: This is a read/write memory segment that is used by the program to store data, function return addresses, and registry values. You can normally not write your program to specifically access this segment, instead it's reserved for use by the operating system to keep track of what instructions are to be executed.

  3. Heap segment: Read/write memory. When the program calls new or malloc() the storage area is normally taken from this segment.

The below picture (copied from the previous link) shows how a program's memory is laid out.


Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.