473,698 Members | 2,076 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 6867
On 26 Dec 2004 11:14:08 -0800, in comp.lang.c , pr********@gmai l.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********@gma il.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********@gmai l.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********@gmai l.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********@gmai l.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.c om> writes:
pr********@gmai l.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_Keit h) 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
3686
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 code. The specific location of the memory corruption depends on the names I give my local variables, on putting some of the codes in curly brackets - {}, and on having the watch window open. I already cleaned and re-built my project. Can anyone...
10
14041
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 file to use purify. However, my code is a rather simple single-source C program, and I didn't write a Makefile for it. I'm wondering if anybody can tell me which commands are to be entered at the Unix prompt to use purify. And, I don't know if...
4
2434
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 following code may cause memory corruption. #include <stdio.h>
8
3409
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 ? In nut shell, what is/are the realtion/s between the Memory Leak and Memory Corruption. Juts Theoritical Assumtion below:
1
3120
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 corrupt, we have procedures for restoring the database from backup data. The question we've been wondering about is how to detect a corrupt database. False positives are acceptable (database isn't really corrupt but we think it is); false...
4
2128
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 crashing or where the corruption is occuring? I have the applications FATAL log. (it's on windows) I am not familiar with debugging core dumps and also since it is
4
2789
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 t_Item class - I use it to store Item Objects (which are classes too). xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx class t_Item { public:
14
3360
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 I'll make the inactive forms invisible but this is creating a memory corruption problem when user close the form2 or form3 and not the formMain. My main form has a Next button which makes the main form invisible and starts a new form which I'll...
66
3688
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
8674
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8603
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9157
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9023
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8861
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6518
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5860
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4615
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
1999
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.