detecting memory corruption

P

prabhat143

Hi,

I was recently asked to write a function in C that would detect if
memory is corrupted. I had no clue about the solution but what I
believe is that the solution is not complicated. Does anybody know how
to write such a function?

Regards,
Prabhat
 
M

Mark McIntyre

Hi,

I was recently asked to write a function in C that would detect if
memory is corrupted. I had no clue about the solution but what I
believe is that the solution is not complicated. Does anybody know how
to write such a function?

You can't do it in standard C, as the moment the memory becomes corrupted,
you can't rely on anything in your code to work at all, and anything which
accesses corrupt memory is doomed to failure.

There may be platform specific functions or features of your compiler or
operating system which help - flags, memory tests etc. Ask in a group
dedicated to your compiler/system to find out more.
 
M

Malcolm

I was recently asked to write a function in C that would detect if
memory is corrupted. I had no clue about the solution but what I
believe is that the solution is not complicated. Does anybody know how
to write such a function?
There is no sure-fire way, because a corrupt byte may accidentally look like
valid information. Also, it is impossible to implement bounds checking in C
without delving into the internals of the compiler.

A system you can use is to implement your own malloc(). At the head of the
block, store the exact number of bytes requested. At the end of the block,
store a few bytes set to a magic value, eg 0xC0. You choose a value that
gives a large negative number when expressed as an integer, and is thus
unlikely to be real data.

Now when the user frees the memory, you can check the block for integrity.
If there has been a bounds error, the end of the block will be corrupted.

This will pick up a lot of real-life errors, but not everything. Another
problem is that you only pick up the corruption when the memory is freed,
when you really want to know the line at which the error occurred.
 
C

Chris Barts

Hi,

I was recently asked to write a function in C that would detect if
memory is corrupted. I had no clue about the solution but what I
believe is that the solution is not complicated. Does anybody know how
to write such a function?

Not with such vague specifications, no. If you know what the memory
should contain, then the solution is not complicated. If you do not, the
solution is impossible.

You should post a small, compilable function that you think does what
you want and the people here will tell you if it's well-written.
 
P

prabhat143

Thanks for all quick responses. The question was asked to me during
written test of a job posting and nobody was there to answer my queries
similar to you all are having. The OS referred to was Unix or its
flavors and rest of the question was exactly I have posted when I
started this topic.

Cheers,
Prabhat
 
E

EventHelix.com

You can use Valgrind to detect memory corruption problems.

Valgrind will instrument the code at compile time and will report
illegal
memory references.

Deepa
 
P

prabhat143

As I said it was during written test so no tools were given and it
required snippets of code.
 
E

Emmanuel Delahaye

Hi,

I was recently asked to write a function in C that would detect if
memory is corrupted. I had no clue about the solution but what I
believe is that the solution is not complicated. Does anybody know how
to write such a function?

It's more a design issue than a C question.

Depends what you are talking about. If you meant sensible data (like
backed up memory holding configuration information or the like), you
can define an integrity mecanism with a CRC16 for example (holds in an
unsigned int).

Each time you change the data you compute the CRC16 of the memory
block, and write it at a known place. Each time you access the memory
in reading mode (or with some timed event), you check the memory bloc
with the CRC16. In case of a memory corruption, the CRC16 will be
wrong, and you will be able to inform the application.

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"Mal nommer les choses c'est ajouter du malheur au
monde." -- Albert Camus.
 
K

Karthik Kumar

Hi,

I was recently asked to write a function in C that would detect if
memory is corrupted. I had no clue about the solution but what I
believe is that the solution is not complicated. Does anybody know how
to write such a function?

Regards,
Prabhat

Well - one possible way is to redefine macros for malloc and free and
write your implementation to keep track of the memory invocations.
( and deletions ). And you can identify memory leaks / corruptions
hence.

<OT>
Tools like ElectricFence and GNU mtrace are available precisely for
these purposes but they may be restricted to the implementation.

</OT>
 
K

Keith Thompson

Karthik Kumar said:
Well - one possible way is to redefine macros for malloc and free and
write your implementation to keep track of the memory invocations.
( and deletions ). And you can identify memory leaks / corruptions
hence.

That can only solve a small part of the problem. For example, it
won't detect an attempt to write beyond the bounds of an array.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top