Qualcomm Interview Question
Software Engineer / DevelopersThe question is just to detect the memory correction and not prevent. Hence when allocating memory of say 'x' bytes, write some pre-determined pattern in (x+1)byte. When this memory is freed, we can check the x+1 th byte to see if the pattern is still there or it is been over written. This can help to find the memory corruption.
When you ask the system for 10 bytes of memory, the systems removes 10 bytes from the list of free bytes in the heap and gives it to you.
If you write in the 11 bytes, according to me this byte should still be in the list of free bytes. So there is a chance the system might give this byte to some body else. So your program will say memory corruption has occurred even when it has not. Some body else wrote data into the (x+1) byte
"predetermined pattern" in (x+1)th byte won't work with a probability 1/256... i'm not sure if its a robust thing to do. whatsay?
would wrapper structure , and wrapper allocation, access and delete functions work?
- django October 21, 2009struct hold
{
void *ptr;
size_t sz;
}
wrap_malloc(size_t sz)
{
struct hold* h=(struct hold*) malloc(sizeof(struct hold));
h->ptr=malloc(sz)
h->sz=sz
return hold;
}
int wrapper_write(struct hold*h,size_t offset,void * val)
{
if(h->sz <=offset && h->sz+offset+sizeof(val)<h->sz)
{
*( (h->sz)+offset)=val ;
return 1;
}
else
{
return 0;
}
}
wrapper_free(struct hold* h )
{
free( hold->ptr);
free(hold);
}