Click here to Skip to main content
16,017,238 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I just read an article which stated the following conditions that may cause memory corruption. But no proper expalantion was given there. Can anyone explain that how does these conditions lead to memory corruption and what happens internally.

1. Using an address before memory is allocated and set

void x()
{
  struct *abc_ptr;
  p = abc_ptr->name;
}



2. Using a pointer which is already freed

int *p = new int;
//code
delete p;
//use of p here may lead to memory corruption


3. Freeing memory which has already been freed.
char *a = malloc(128*sizeof(char));
free(a);
...   Do stuff
free(a);  // A check for NULL would indicate nothing.
          // This memory space may be reallocated and thus we may be freeing
          // memory we do not intend to free or portions of another block of 
          // memory. The size of the block of memory allocated is often held 
          // just before the memory block itself..
                  // some code
}
delete i;


4. Freeing memory which was not dynamically allocated

void main()
{
struct ABC abc;
struct ABC *abc_ptr = &abc;
free(abc_ptr);
}


5. Incorrect use of delete: The delete must match the use of new

void x()
{
  abc *abc_ptr = new abc[100];
  delete [] abc_ptr;
}

http://www.yolinux.com/TUTORIALS/C++MemoryCorruptionAndMemoryLeaks.html[^]
Posted
Updated 15-Mar-12 23:23pm
v2
Comments
CPallini 15-Mar-12 9:52am    
What's wrong in sample 5?
JackDingler 15-Mar-12 11:43am    
Trick question from the professor.

And a good one. It's not uncommon to be assigned to fix a problem, and be given bad information about the cause, because the assignee only sees the symptoms.

Bugs must be verified before they can be properly fixed.
CPallini 15-Mar-12 16:29pm    
I see nothing wrong in the code sample.
JackDingler 15-Mar-12 11:34am    
Smells like homework.
JackDingler 15-Mar-12 11:39am    
If other people do your homework for you, you may pass the class, but not the job interview.

These are common problems used in job interviews. If you can't stand up and explain these problems with eight people, critically staring at you, then you're gonna be living with mom and dad, longer than you might like.

If you want to make it in the professional world, then you must master these basic concepts.

1) Using an address before memory is allocated and set
If you assign a pointer, that doesn't mean it points somewhere useful.
If you wanted to call your mother, would you just press buttons at random and talk to whoever answered?

2) Using a pointer which is already freed
If you delete a memory block, then any other memory allocation is free to use that area of memeory. Just because the pointer value has nit changed, does not mean that you can use the memory it points to!
If you sold your house, would you expect to still be allowed to go back there and sleep in "your" bedroom?

3. Freeing memory which has already been freed.
If it is already freed, then trying to free it again could cause problems - because the same block of memory would be in teh free list twice. So two different requests for memory could refer to the same physical memory.

4. Freeing memory which was not dynamically allocated

If you free memory which isn't allocated on the heap, then it is going to cause problems - the stack space will be reused by a different function, and any memory allocation which gets the freed block will refer to it at the same time...

5. Incorrect use of delete: The delete must match the use of new
If you free memory by calling it a different type to that which you allocated it, then it is quite possible that the memory freed is not the same size as the block that was allocated. As a result, either memory is no longer available to the heap when it should be, or the block freed could include memory which is not part of the allocation, but "owned" by a different memory block.
 
Share this answer
 
Comments
Richard MacCutchan 15-Mar-12 9:36am    
Nice analogies.
JackDingler 15-Mar-12 11:50am    
I don't see where you're coming from on #5. Was the question updated?
To avoid erroneous use of unassigned pointers all pointers should be set to NULL when declared. They should be set to NULL again when the memory they point to is freed.

The language specification does not require the compiler to do this.

If this is done then using the unassigned pointer will throw an exception.

For example #1 should be
C++
struct *abc_ptr = NULL;


#2 should be
C#
int *p = new int;
//code
delete p;
p = NULL;



and #5 should be
C++
void x()
{
  abc *abc_ptr = new abc[100];
  delete [] abc_ptr;
  abc_ptr = NULL;
}


In all the other examples the pointer should either point to valid memory or be set to NULL.

If the pointer is not set to NULL it could have any value and subsequent use of that pointer will have unpredictable results.

The is the cause of many subtle bugs.
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900