-5

Currently I am studying C Programming in memory management.

  • What memory abstractions does C provide ?
  • What exactly is the distinction between stack and heap?
  • How do I use pointers to access memory locations ?
  • How do I allocate and free memory on the heap ?
trincot
  • 317,000
  • 35
  • 244
  • 286
  • 1
    Those are big topics and cannot be answered in a few words :) – Zhi Wang May 31 '13 at 04:57
  • If this is all about C, why does it have a C++ tag? Memory management between the two varies vastly. – chris May 31 '13 at 04:58
  • 3
    It's amazing what you find if you take any one of those bullet points and enter it in the search box on either StackOverflow or Google. – paddy May 31 '13 at 04:59
  • http://stackoverflow.com/questions/79923/what-and-where-are-the-stack-and-heap – Jeyaram May 31 '13 at 04:59
  • for these topics are too big to answer, you may search each one and learn it respectively from books or googles :) and this(http://stackoverflow.com/questions/79923/what-and-where-are-the-stack-and-heap?rq=1) may help for your 2nd qeustion. – Bill Hoo May 31 '13 at 05:02

1 Answers1

2

To be EXTREMELY brief:

  • Variables in a stack are lost when a function returns. Variables in a heap can be accessed by any function, and the values are not lost until they are freed. So, stacks are useful because they are simply "local" to a single environment (function). Heaps are good when we need to access more "global" types of data, between functions.
  • int * p = 4. this means that you have created a pointer to memory address 4.
  • *p is called "dereferencing the pointer p", which basically goes to the address stored in p . you can do something like *p = 100, which means storing the value 100 at the address stored in p.
  • to allocate memory in c, use malloc(...), where documentation can be found here: http://www.cplusplus.com/reference/cstdlib/malloc/
  • to free memory, use free(...), documentation found here: http://www.cplusplus.com/reference/cstdlib/free/
a_schimpf
  • 745
  • 1
  • 5
  • 16
  • 1
    I'd suggest you don't do people's homework for them or answer questions so blatantly in violation of the FAQ – xaxxon May 31 '13 at 06:40