473,769 Members | 2,501 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Reentrant functions and memory allocation

Dear all,
I'm trying to put some old code in a portable, cross-platform library
and I'm facing a big problem: is there a standard, platform-
independent way to write a reentrant function that allocate dynamic
memory? In other words: is it safe to use malloc() or free() in a
thread-safe function?
Thank you,
Francesco
Mar 30 '08 #1
17 3277
On 30 Mar, 15:08, Flash Gordon <s...@flash-gordon.me.ukwro te:
fmas...@gmail.c om wrote, On 30/03/08 13:45:
Dear all,
I'm trying to put some old code in a portable, cross-platform library
and I'm facing a big problem: is there a standard, platform-
independent way to write a reentrant function that allocate dynamic
memory? In other words: is it safe to use malloc() or free() in a
thread-safe function?

Re-entrancy and thread safety are not the same. The C standard does not
know about threads but does know a little about reentrancy, and
malloc/realloc/free are not required to be reentrant. You might find
comp.programmin g.threads a better place to ask about how you can solve
your threading problems.
--
Flash Gordon
This is what I was worried about. I just want to write something that
can be safely used in a multi-threaded project, if someone wants to.
Should I consider to work only with pre-allocated memory? Doing this
way the problem can be moved outside the library. Is this the only
solution I have?
Mar 30 '08 #2
Richard wrote:
) There are still people like Chuck there spouting nonsense. If you dont
) know, dont answer.

And if you *do* know where correct answers can be found ? Exactly.

If you want to ask about threads, go to comp.programmin g.threads
SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT
Mar 30 '08 #3
Willem <wi****@stack.n lwrites:
Richard wrote:
) There are still people like Chuck there spouting nonsense. If you dont
) know, dont answer.

And if you *do* know where correct answers can be found ? Exactly.

If you want to ask about threads, go to comp.programmin g.threads
And you are? If I feel I have a question I wish to address to a large
bunch of theoretically helpful (they are here after all) and
knowledgeable C programmers I will ask it here. If you do not wish to
answer then feel free to ignore it.
Mar 30 '08 #4

Why are people talking about malloc being or not being re-entrant? We
don't see the definition of malloc, so we don't care if it calls
itself internally.

As for malloc being called from within a re-entrant function, then I
see no problem:

void Func(unsigned const i)
{
static char *addresses[4];

addresses[i] = malloc(1);

if (3 == i) return;

Func(i+1);
}

int main(void)
{
Func(0);

return 0;
}

As for malloc being thread-safe, then I think the bottom line is that
the C standard doesn't guarantee that malloc is thread-safe. All is
not lost though, all you need to do is find a thread-safe
implementation of malloc... you don't have to edit all the old code.
Mar 30 '08 #5
Richard wrote:
) And you are? If I feel I have a question I wish to address to a large
) bunch of theoretically helpful (they are here after all) and
) knowledgeable C programmers I will ask it here. If you do not wish to
) answer then feel free to ignore it.

Unlike you, most people who come here with questions about something
indirectly related to C, such as threads for example, do not do this
with the specific intention of getting answers from knowledgeable
C programmers. They want answers from people knowledgeable about the
subject they are asking about, so the polite thing to do is to direct
them to where such people are most likely to be found.
SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT
Mar 30 '08 #6
Tomás Ó hÉilidhe wrote:
>
Why are people talking about malloc being or not being re-entrant?
We don't see the definition of malloc, so we don't care if it
calls itself internally.

As for malloc being called from within a re-entrant function,
then I see no problem:
Because, (and this is OT) while a malloc call is executing, an
interrupt may occur (including OS time-outs) and another process or
thread execute. This may call malloc with the underlying
structures in a dangerous condition.

Note that processes usually have unique memory areas, so that the
malloc memory is isolated. This does not apply to threads.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home .att.net>
Try the download section.
--
Posted via a free Usenet account from http://www.teranews.com

Mar 30 '08 #7
CBFalconer:
Because, (and this is OT) while a malloc call is executing, an
interrupt may occur (including OS time-outs) and another process or
thread execute. This may call malloc with the underlying
structures in a dangerous condition.

Note that processes usually have unique memory areas, so that the
malloc memory is isolated. This does not apply to threads.

I still don't understand. Here's what I think I understand:

1) If a function is thread-safe, then it can be executed more than
once simultaneously without malfunctioning. In general, to make a
function thread-safe, you avoid using static-duration objects and
instead opt for using automatic-duration objects.
2) If a function is re-entrant, then it can execute itself without
malfunctioning.

If you have a multi-threaded program that uses malloc, then I can
definitely understand why you'd want malloc to be thread-safe. But I
fail to understand why you'd want it to be re-entrant.

If you wanted malloc to be re-entrant, then that suggests to me that
you want to be able to call malloc from within malloc... which sounds
ridiculous to me because you shouldn't be going near the definition of
malloc.

I'm not saying I'm right or wrong, but I do know that I don't fully
understand. So could someone please explain why we'd want malloc to be
re-entrant... ?
Mar 30 '08 #8
Tomás Ó hÉilidhe <to*@lavabit.co mwrites:
CBFalconer:
>Because, (and this is OT) while a malloc call is executing, an
interrupt may occur (including OS time-outs) and another process or
thread execute. This may call malloc with the underlying
structures in a dangerous condition.

Note that processes usually have unique memory areas, so that the
malloc memory is isolated. This does not apply to threads.


I still don't understand. Here's what I think I understand:
You dont understand because Falconer is making a mess again.

He wants to talk about "standard malloc" but at the same time talk about
non standard re-entrance.
>
1) If a function is thread-safe, then it can be executed more than
once simultaneously without malfunctioning. In general, to make a
function thread-safe, you avoid using static-duration objects and
instead opt for using automatic-duration objects.
2) If a function is re-entrant, then it can execute itself without
malfunctioning.

If you have a multi-threaded program that uses malloc, then I can
definitely understand why you'd want malloc to be thread-safe. But I
fail to understand why you'd want it to be re-entrant.

If you wanted malloc to be re-entrant, then that suggests to me that
you want to be able to call malloc from within malloc... which sounds
ridiculous to me because you shouldn't be going near the definition of
malloc.

I'm not saying I'm right or wrong, but I do know that I don't fully
understand. So could someone please explain why we'd want malloc to be
re-entrant... ?
Mar 30 '08 #9
Tomás Ó hÉilidhe wrote:
CBFalconer:
>Because, (and this is OT) while a malloc call is executing, an
interrupt may occur (including OS time-outs) and another process or
thread execute. This may call malloc with the underlying
structures in a dangerous condition.

Note that processes usually have unique memory areas, so that the
malloc memory is isolated. This does not apply to threads.


I still don't understand. Here's what I think I understand:

1) If a function is thread-safe, then it can be executed more than
once simultaneously without malfunctioning. In general, to make a
function thread-safe, you avoid using static-duration objects and
instead opt for using automatic-duration objects.
2) If a function is re-entrant, then it can execute itself without
malfunctioning.

If you have a multi-threaded program that uses malloc, then I can
definitely understand why you'd want malloc to be thread-safe. But I
fail to understand why you'd want it to be re-entrant.

If you wanted malloc to be re-entrant, then that suggests to me that
you want to be able to call malloc from within malloc... which sounds
ridiculous to me because you shouldn't be going near the definition of
malloc.

I'm not saying I'm right or wrong, but I do know that I don't fully
understand. So could someone please explain why we'd want malloc to be
re-entrant... ?
What if during a call to malloc() from thread A, the system suspends the
thread and schedules thread B which calls malloc()? Shouldn't malloc()
be reentrant to function properly in this scenario?

Mar 30 '08 #10

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

Similar topics

14
2094
by: Blue Ocean | last post by:
Hey all, I have another question. I may seem like a moron here, but where can I find out how C-standard functions like malloc() and printf() are declared? I know that I can find on my computer their .h files, but I can't find the .c files anywhere. Are they already in object code or something? Can I disassemble them to see how they are written?
2
1645
by: MJL | last post by:
I am working on a small project that involves the manipulation of dynamically allocated memory for strings. I was wondering why the string.h functions are the way they are and why not as follows: mystrcat(char** s1,char** s2); where s1's memory allocation can now be handled inside of the function and s2 can be freed and pointed to NULL inside the function. I'm not saying the built in functions are not useful as they are, but
6
11047
by: junky_fellow | last post by:
what are reentrant functions? What are the characteristics of a reentrant code ? what things should be kept in mind while writing a reentrant code ?
19
4260
by: Ross A. Finlayson | last post by:
Hi, I hope you can help me understand the varargs facility. Say I am programming in ISO C including stdarg.h and I declare a function as so: void log_printf(const char* logfilename, const char* formatter, ...); Then, I want to call it as so:
2
2847
by: TheOne | last post by:
Would anyone please point me to a list of reentrant GNU C/C++ library functions? I have searched a lot on the web for it but without any success. Man page of signal(2) has listed system calls (POSIX 1003.1-2003 list) which are safe(reentrant) but thats not sufficient for my purpose. Thanks in advance.
9
8300
by: TheOne | last post by:
Would anyone please point me to a list of reentrant C library functions? I want to know which C library functions are safe to use inside a signal handler across all platforms. Does GNU C library make few other functions reentrant on Linux platform? I have searched a lot on the web for it but without any success. Man page of signal(2) has listed system calls (POSIX 1003.1-2003 list) which are safe(reentrant) but thats not sufficient for...
24
19093
by: Ken | last post by:
In C programming, I want to know in what situations we should use static memory allocation instead of dynamic memory allocation. My understanding is that static memory allocation like using array is faster than malloc, but dynamic memory allocation is more flexible. Please comment... thanks.
1
7976
by: Peterwkc | last post by:
Hello all expert, i have two program which make me desperate bu after i have noticed the forum, my future is become brightness back. By the way, my problem is like this i the first program was compiled and run without any erros but the second program has a run time error when the function return from allocate and the ptr become NULL. How to fixed this? Second Program: /* Best Method to allocate memory for 2D Array because it's ...
12
3504
by: Bit Byte | last post by:
I am modifying some legacy code, to make the functions reentrant. I use lots of global data (large nested structs) to pass data around quicky. The global structures are far too large (in size) for each of my functions to maintain copies. Any suggestions on the best way to write a reentrant version of my library? - bearing in mind that my global vars are mostly, pointers to *HUGE* structures.
0
9583
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
9423
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
10039
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
9990
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,...
1
7406
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
6668
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
5297
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
5445
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3955
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

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.