473,654 Members | 3,109 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Best strategy for handling memory allocations

Hi.

I have to design new API, which some of them have to return a string.
I have mixed feelings about the various strategies I can choose :

1. API(char *s, long *size)

=> the client gives a string pointer and its size; If the server has
to return a longer string, it fails, and the client calls back with a
longer string.

2. API(char *s)

=> the client receives a statically allocated string pointer from the
server. It can be changed when the client calls other APIs.

3. API(char *s)

=> the client receives a dynamically allocated copy of the string from
the server. The client has to call back the server to release this
pointer (APIRelease(s)) ;

Can you help me to choose the "best" one ?

My constraints : multi-thread, Windows / UNIX.

Thanks a lot in advance.

Fred
Nov 14 '05 #1
1 1289
Fr?d?ric Ledain wrote:

Hi.

I have to design new API, which some of them have to return a string.
I have mixed feelings about the various strategies I can choose :

1. API(char *s, long *size)

=> the client gives a string pointer and its size; If the server has
to return a longer string, it fails, and the client calls back with a
longer string.
Strengths: buffer protection.
Weaknesses: possibly refuses the server unnecessarily.
Observations: size_t would be an improvement over long int. Did you
really want a pointer? (But see below.)

2. API(char *s)

=> the client receives a statically allocated string pointer from the
server. It can be changed when the client calls other APIs.
Strengths: none that I can think of.
Weaknesses: no buffer protection. "static" and "multi-threaded"
don't mix well.
Observations: it sucks.
3. API(char *s)

=> the client receives a dynamically allocated copy of the string from
the server. The client has to call back the server to release this
pointer (APIRelease(s)) ;
Strengths: none that I can think of.
Weaknesses: no buffer protection.
Observations: it sucks.
Can you help me to choose the "best" one ?

My constraints : multi-thread, Windows / UNIX.


Consider this possibility:

int API(char **s, size_t *size, size_t max);

Caller passes a pointer to a char * that initially has the value NULL,
and a size_t with the initial value 0.

Function reallocates buffer size as necessary to accommodate
incoming data, but refuses to expand past the "max" safety limit
(to avoid hostile connections attempting to tie up all your RAM).
Buffer can be reused over and over, as long as you continue to
allow the size_t object (whose address is passed) to remain
uncorrupted.

Caller can simply free() when done.

Strengths: buffer protection, reusable buffer, protection against
RAM denial.
Weaknesses: several params. Struct is tempting.

HTH. HAND.
Nov 14 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
1849
by: Jakob Bieling | last post by:
Hi, Whenever allocating memory using operator new or operator new I feel like I should only use it very sparingly, because otherwise memory is wasted (by additional overhead needed to manage all those allocations) which also loses some performance. I am specifically talking about many little allocations (approx. 16-512 bytes). Is my feeling about this justified? thanks! --
1
2089
by: Manuel Massing | last post by:
Hi, I was wondering if there is an easy way to keep track of all allocations issued by instances of a specific class and its subclasses (i.e. keep track of memory consumption of the class instances, including e.g. heap-allocated member variables). A global overloading of new / delete operators is out of question, because I have to track memory consumption for a specific class, and also because the base class implementation will be...
136
9301
by: Matt Kruse | last post by:
http://www.JavascriptToolbox.com/bestpractices/ I started writing this up as a guide for some people who were looking for general tips on how to do things the 'right way' with Javascript. Their code was littered with document.all and eval, for example, and I wanted to create a practical list of best practices that they could easily put to use. The above URL is version 1.0 (draft) that resulted. IMO, it is not a replacement for the FAQ,...
13
5725
by: Michael B Allen | last post by:
Hi, I've tried to write the *simplest* memory allocator possible. I think it would be useful in many cases such as allocating memory on stack as a poor man's garbage collection perhaps. I was hoping the clc crowd had some ideas for making it even simpler! In-lined below the full program (132 lines). It's a circular singular linked list of "cells" inspired by the one in Plauger's text
55
12793
by: Steve | last post by:
I have to develop several large and complex C++ hardware test programs that should work under DOS, most likely with 32-bit DOS extender. Development workstation OS would be Microsoft XP. Quite some time ago I worked in DOS, with Borland BC++ 4.1. I do not have it any more. Which compiler would you recommend me now? Which ones support serious DOS program development? Criterion should be number of available free library modules (graphic menu...
2
2980
by: Petr Jakes | last post by:
I am a little bit confused by all possibilities for exceptions handling in Python (probably because I am not skilled enough??) I did try to search trough this list and reading Python tutorial about Errors and Exceptions but didn't find some "general" answer about exception handling policy (strategy). In the following example each row can IMHO raise an exception (if the Firebird service is not running for example, if the database is...
1
1458
by: kiplring | last post by:
List<string> effectList = new List<string>(); effectList.Clear(); effectList = null; using (List<string> effectList = new List<string>()) { } If there are so many calls, I should save as much memory as I can.
6
1666
by: Ark Khasin | last post by:
My pet project is a command-line utility (preprocessor), so it runs and terminates. It uses lots of memory allocations; most of them are quite small. What to do with the allocated objects when they are no longer used? I consider the following "pure" strategies: - Meticulously free anything ever malloc'ed. This involves inefficiencies of calling free, but worse yet, in certain cases object duplication appears necessary to ensure uniqueness...
12
9931
by: bomahony | last post by:
Im runnign various versions of DB2 on various UNIX platforms (Solaris / AIX). DB2 is generally eating up about 30% of memory on these systems (4Gb of RAM, which equates to over a gig). i want to limit DB2s memory usage to a couple of hundred meg in total for the entire application, but cannot seem to find out how to do this. When i check the instance memory usage with db2mtrk, it says its only using a few Megs. I cannot find out how...
0
8706
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...
1
8475
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8591
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
6160
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
5621
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
4149
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4293
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2709
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1592
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.