• Home
  • Tutorials
  • Interviews
  • Info Seed
  • Forums
  • Projects
  • Links
  • Contact Us
C Language
C POINTERS
INTRODUCTION
What are POINTERS?
Illustration of POINTER assignments
Illustration - II
Illustration - III
POINTER arithmetic- POINTER + number
POINTER - number
POINTER - POINTER
NULL POINTER
Common Errors
DEREFERENCING NULL
DANGLING POINTER
REFERENCING DE-ALLOCATED MEMORY
MEMORY MANAGEMENT

 

 

REFERENCING DE-ALLOCATED MEMORY:

This problem occurs when the address of the local variable is returned from a function or the programmer deletes the allocated memory mistakedly.

main()

{

Int *y=abc();

*y=3;     A SERIOUS BUG (You’ll get the full explanation in the article Memory Management)

}

Int* abc()

{

Int x=7;

return (&x);

}

After the execution of statement1 in the main, the memory map is as:

www.exploreroots.com

Now we have reached the function abc (), the execution of statement1 of function abc() results into following memory map:

www.exploreroots.com

But when the function abc() returns, the address of the variable x is stored in y and INTENTION is to use x as pointee of y. But with the return of the function abc(), the stack frees the variable x. Hence memory location of x can be used somewhere else by the Operating System.

www.exploreroots.com

The non-belonging memory is the memory which doesn’t belong to the program anymore. Hence the situation now is similar to the DANGLING POINTER case. Hence the 2nd statement of main() may cause ERROR.

Note: REFER TO THE ARTICLE MEMORY MANAGEMENT FOR BETTER UNDERSTANDNG

  

 

 


previous next
| Copyright © 2009 exploreroots- All Rights Reserved | Disclaimer |