Thread: Memory Functions - Still Learning

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #16
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    This won't work because you asked memory for 1 int, not 3.

    As I like to see pointers...
    Imagine a big city. The city must be addressable (ie, each part of it must have an address). This is so someone knows where to go if you describe it (memory address).
    Then let's imagine they need to build stuff. Houses, offices, factories, stores, etc (variables).

    So let's say then that you are the head of a building company. You want to build something. But you simply do not have permission to just go and build somewhere somewhere. No sir. If you do, the police will arrest you (the OS will shut down your program).
    Instead, you ask permission to build a number of buildings (next to each other). Say, 3 houses. The state then gives you clearance to build, and later, destroy what you build, and an address where these houses may be built (you call malloc/calloc).
    So, you send your working team there and they build the houses (you assign values in the requested memory). But since you ask to build 3, you only have permission to build 3 and no more. If you try to build more, the police will arrest you.
    Now, these houses will remain there, forever (let's forget the fact that they need maintenance). And they hog valuable space in the city. Eventually, if you keep building, there's not going to be any space left (you run out of memory).
    So you then need to destroy those houses to eventually build other things there. And having destroyed them, you no longer have permission to build there. If you do, the police will arrest you.

    This theory can be applied to dynamic memory.
    The big city is the computer memory.
    The police is the operating system.
    Addresses are pointers.
    Houses are variables such as int, float, char.
    Permission is given by malloc/calloc.
    You send your team to build the house by storing data using the dereference operator (*).
    You destroy what you built with free.

    So you request space in memory with malloc.
    You store the address in a pointer.
    You dereference the pointer to store a value.
    You call free to get rid of the memory you no longer need.

    Also, as you know, it is possible to get away with doing something illegal without the police catching you (your program may crash, and it might not). You might get away with it, and you might not. But you should keep yourself to within the walls of the law (then there will be 0% chance of your app crashing).
    Doing something illegal is often caused undefined behavior in the programming world (because it might lead to undesired results, and it might not).
    Last edited by Elysia; 08-29-2008 at 04:17 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 02-06-2009, 12:27 PM
  2. Reallocating Memory
    By Booie2k1 in forum C Programming
    Replies: 3
    Last Post: 03-11-2008, 06:09 AM
  3. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 01:33 PM
  4. Accessing Video Memory Information...need help
    By KneeLess in forum C++ Programming
    Replies: 8
    Last Post: 08-24-2003, 03:53 PM
  5. Manipulating the Windows Clipboard
    By Johno in forum Windows Programming
    Replies: 2
    Last Post: 10-01-2002, 09:37 AM

Tags for this Thread