473,396 Members | 2,151 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,396 software developers and data experts.

please let me know how to write the wraper function

I have a function sEntityFree() which is a function from a third party S/w we are using. For this we have our enhanced code.
sEntityFree() has been called inside our enhanced code 2000 or 3000 times. But NULL pointer checking before freeing the memory is not there.
Like the function has been called as below:

sEntityFree(Entity, ( void** )&StructPtr, sYES );
or
sEntityFree(Entity, ( void** )&StructPtr, sNO );

If I will write it as

if((Entity != NULL) && (StructPtr != NULL))
sEntityFree(Entity, ( void** )&StructPtr, sYES );

Then I have to write this repeatedly as many times the function has been called.
Even I could not change inside the function sEntityFree(), as the code is not with us.
Hence I wanted to write a wraper function like this:
Expand|Select|Wrap|Line Numbers
  1. int cMemoryFree(sENTITY * Entity,void** StructPtr,sYORN FreeMemory)
  2. {
  3.    int ret = sSUCCESS;   //#define sSUCCESS      0
  4.  
  5.    if((FreeMemory == sYES) && (Entity != NULL) && (StructPtr != NULL))
  6.    {
  7.       ret = sEntityFree(Entity, ( void** )&StructPtr, sYES );
  8.    }
  9.    else if((FreeMemory == sNO) && (StructPtr != NULL))
  10.    {
  11.       ret = sEntityFree(Entity, ( void** )&StructPtr, sNO );
  12.    }
  13.    return ret;
  14. }
  15.  
But this is giving segmentation fault. Please let me know how to write this cMemoryFree() wraper function.

Note: Here is the skeleton for sEntityFree()
Expand|Select|Wrap|Line Numbers
  1. int sEntityFree( sENTITY * Entity, 
  2.                       void**    StructPtr, 
  3.                       sYORN FreeMemory )
  4.  
Entity : Entity pointer

StructPtr : void pointer to the pointer of the entity
data structure.

FreeMemory : Flag to indicate if the entity pointer
should be freed.

Please let me know. Thanks.
Aug 29 '07 #1
1 2140
weaknessforcats
9,208 Expert Mod 8TB
You are barking up a dead tree.

No amount of code can tell you whether it's safe to free something.

You need to know of there are any other pointers in the program that point to the thing you think you want to delete. If there are, and you delete, your program will crash somewhere else when the deleted memory is deleted again of if another pointer tries to access the variable that has been deleted.

The single most dangerous thing you can do in a program is pass pointers around and then try to make decsisions about freeing up memory.

You don't say if you are in C or C++.

If you are in C, you will need to build in reference counting on all of your pointers. And even this may not work since there's no guarantee that everyone will adhere to the reference counting rules.

In C++, you just use a handle. In this link there is a handle template you can just copy out and use in your application.
Aug 29 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: todd | last post by:
Im tring to learn about classes with php. I've read alot of posts here and look at other peoples code. So I appied all the absorbed to this. Im tring to build a Shopping cart. not for live use but...
35
by: wired | last post by:
Hi, I've just taught myself C++, so I haven't learnt much about style or the like from any single source, and I'm quite styleless as a result. But at the same time, I really want nice code and I...
1
by: Az Tech | last post by:
Hi people, (Sorry for the somewhat long post). I request some of the people on this group who have good experience using object-orientation in the field, to please give some good ideas for...
9
by: Martin | last post by:
Due to the the large numbers of MS trained programmers unemployed we are looking at using MS Development environments. VB programmers are currently 10 a penny. However I find C a bit more...
23
by: Brian | last post by:
I am very new to C# programming and have run into a problem. I apologize for the repost of this article. For some reason, my news reader attached it to an existing thread. First off, I have...
7
by: Tigger | last post by:
Dear Experts, I am working on ASP.NET. I have got a problem related to the usage of Javascript in ASP.NET. Please help. The story is the following: 1) I am developing an ASP.NET application. I...
1
by: td0g03 | last post by:
Hello, I am new to C and I am new to English. I not sure what palindromes mean. I don't know exactly what my teacher wants me to do. If someone could explain it to me in a different way that would be...
6
by: jenipriya | last post by:
Hi all... its very urgent.. please........i m a beginner in oracle.... Anyone please help me wit dese codes i hv tried... and correct the errors... The table structures i hav Employee (EmpID,...
5
by: cherriecheng88 | last post by:
hi...did anyone know how to do this assignment? cuz, i have run out of time really need ppls help me out thank you if you know it... Blackjack For the third assignment you will write a program...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...
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...
0
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...

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.