473,385 Members | 1,732 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

detecting memory corruption

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
9 6853
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
<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
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
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
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
As I said it was during written test so no tools were given and it
required snippets of code.

Nov 14 '05 #7
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Noa Garnett | last post by:
I'm developing on C++, using visual studio 6.0 with service pack 5. I have a memory corruption while debugging. Some of the variables I'm using are suddenly set to zero while progressing along the...
10
by: eyh5 | last post by:
Hi, My C code (running on Soalris Unix) has some "segmentation fault" that I wish to use purify to do it. I poked around the web, and found some information about adding some lines in a Makefile...
4
by: indushekara | last post by:
Hi, We are having memory corruption in our application somewhere, unable to find out. one part of code we found that we are specifying wrong format specifier. Could anyone let me know if the...
8
by: ranjeet.gupta | last post by:
Dear All Is the Root Cause of the Memory corruption is the Memory leak, ?? suppose If in the code there is Memory leak, Do this may lead to the Memory Corruption while executing the program ? ...
1
by: Jack Orenstein | last post by:
My company is developing a PostgreSQL 7.4 application. We don't want our customers to have to manage the database, so we're automating as much maintenance as possible. If the database ever becomes...
4
by: Harsha | last post by:
Hi All, There is some memory corruption in my application. It is due to the fact that it is multithreaded. Is this due to some missing mutex locking? Is there any way to find where it is...
4
by: Tomassus | last post by:
Hi there, I have a problem with dynamic memory allocation. I know that it would have been easier to use vectors methods, but i want to know what i do here wrong. This is one of my methods in...
14
by: =?Utf-8?B?UHVjY2E=?= | last post by:
Hi, I'm using VS2005 and .net 2.0. I'm creating an application that has 3 forms. I want allow users to move forward and backward with the forms and retain the data users have entered. I thought...
66
by: Why Tea | last post by:
typedef struct some_struct { int i; short k, int m; char s; } some_struct_t; Assuming 16 bit or 32-bit alignment, can I assume that s always gets 4 or 8 bytes of allocation due to padding
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.