By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,734 Members | 828 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,734 IT Pros & Developers. It's quick & easy.

detecting memory corruption

P: n/a
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

Nov 14 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
On 26 Dec 2004 11:14:08 -0800, in comp.lang.c , pr********@gmail.com wrote:
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.

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>

----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Nov 14 '05 #2

P: n/a
<pr********@gmail.com> wrote

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.
Nov 14 '05 #3

P: n/a
pr********@gmail.com wrote:
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.
Nov 14 '05 #4

P: n/a
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

Nov 14 '05 #5

P: n/a
You can use Valgrind to detect memory corruption problems.

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

Deepa
--
http://www.EventHelix.com/EventStudio
EventStudio 2.5 - Automate sequence diagram generation

Nov 14 '05 #6

P: n/a
As I said it was during written test so no tools were given and it
required snippets of code.

Nov 14 '05 #7

P: n/a
pr********@gmail.com wrote on 26/12/04 :
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.

Nov 14 '05 #8

P: n/a
pr********@gmail.com wrote:
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>
--
Karthik.
Nov 14 '05 #9

P: n/a
Karthik Kumar <ka*******************@yahoo.com> writes:
pr********@gmail.com wrote:
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?


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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.