Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > What standard library functions must deal with allocated memory?

Reply
Thread Tools

What standard library functions must deal with allocated memory?

 
 
Francois Grieu
Guest
Posts: n/a
 
      04-23-2012
I'm trying to list all the standard C99/C2011 library functions that, by
design, must deal with allocated memory in any way.

I've seen calloc malloc realloc free. Is there any other?
If strdup was a standard function (it is not), it would be in my list.

Francois Grieu
 
Reply With Quote
 
 
 
 
James Kuyper
Guest
Posts: n/a
 
      04-23-2012
On 04/23/2012 02:01 PM, Francois Grieu wrote:
> I'm trying to list all the standard C99/C2011 library functions that, by
> design, must deal with allocated memory in any way.
>
> I've seen calloc malloc realloc free. Is there any other?
> If strdup was a standard function (it is not), it would be in my list.


In C2011, add aligned_alloc(). Otherwise, your list is complete.

Note that the rules for fopen() and fclose() are written in such a way
that the FILE* objects they return, or the buffers managed by those
objects, could be dynamically allocated, though not necessarily by the
malloc() family. The behavior is undefined if you make any use of a
FILE* returned by fopen(), after it has been passed to fclose(), as
would naturally be the case if it were allocated by a call to malloc().
 
Reply With Quote
 
 
 
 
Francois Grieu
Guest
Posts: n/a
 
      04-23-2012
On 23/04/2012 20:17, James Kuyper wrote:
> On 04/23/2012 02:01 PM, Francois Grieu wrote:
>> I'm trying to list all the standard C99/C2011 library functions that, by
>> design, must deal with allocated memory in any way.
>>
>> I've seen calloc malloc realloc free. Is there any other?
>> If strdup was a standard function (it is not), it would be in my list.

>
> In C2011, add aligned_alloc(). Otherwise, your list is complete.


Thanks both for the missed one, and the reassurance.

Francois Grieu
 
Reply With Quote
 
Barry Schwarz
Guest
Posts: n/a
 
      04-23-2012
On Mon, 23 Apr 2012 20:01:49 +0200, Francois Grieu <>
wrote:

>I'm trying to list all the standard C99/C2011 library functions that, by
>design, must deal with allocated memory in any way.
>
>I've seen calloc malloc realloc free. Is there any other?
>If strdup was a standard function (it is not), it would be in my list.


strdup doesn't really "deal" with allocated memory so much as it uses
the allocated memory malloc provided. If you are going to include all
the functions that call malloc et al then you may need to add:

fopen and fclose if the FILE* returned is allocated dynamically.

memmove and wmemmove if the "as if temporary array" is allocated
dynamically.

It is not a library function but do you want to include variable
length arrays as dealing with allocated memory in some way?

--
Remove del for email
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      04-23-2012
Barry Schwarz <> writes:
> On Mon, 23 Apr 2012 20:01:49 +0200, Francois Grieu <>
> wrote:
>
>>I'm trying to list all the standard C99/C2011 library functions that, by
>>design, must deal with allocated memory in any way.
>>
>>I've seen calloc malloc realloc free. Is there any other?
>>If strdup was a standard function (it is not), it would be in my list.

>
> strdup doesn't really "deal" with allocated memory so much as it uses
> the allocated memory malloc provided. If you are going to include all
> the functions that call malloc et al then you may need to add:
>
> fopen and fclose if the FILE* returned is allocated dynamically.
>
> memmove and wmemmove if the "as if temporary array" is allocated
> dynamically.


fopen, fclose, memmove, and wmmemmove, *might* use allocated
memory (via malloc or something similar), but not in any way that
a programmer can or should be concerned with. For example, passing
a FILE* that was returned by fopen to free has undefined behavior.
(And memmove and wmemmove are more likely to simply copy in reverse
order if the source and destination happen to overlap.)

For that matter, any standard library function might allocate and
deallocate memory behind the scenes.

> It is not a library function but do you want to include variable
> length arrays as dealing with allocated memory in some way?


Depends on what you mean by "allocated" -- and fixed-size arrays,
or any other declared objects, have the same issue. VLAs do not
have allocated storage duration; I'm not sure if that's the criterion
Francois is most concerned with.

--
Keith Thompson (The_Other_Keith) kst- <http://www.ghoti.net/~kst>
Will write code for food.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
Francois Grieu
Guest
Posts: n/a
 
      04-23-2012
On 23/04/2012 21:26, Barry Schwarz wrote:
> On Mon, 23 Apr 2012 20:01:49 +0200, Francois Grieu<>
> wrote:
>
>> I'm trying to list all the standard C99/C2011 library functions that, by
>> design, must deal with allocated memory in any way.
>>
>> I've seen calloc malloc realloc free. Is there any other?
>> If strdup was a standard function (it is not), it would be in my list.

>
> strdup doesn't really "deal" with allocated memory so much as it uses
> the allocated memory malloc provided.


It would interest me because one can not implement strdup without calling
calloc malloc realloc (since the result must be a valid argument to free).


> If you are going to include all
> the functions that call malloc et al then you may need to add:
>
> fopen and fclose if the FILE* returned is allocated dynamically.
>
> memmove and wmemmove if the "as if temporary array" is allocated
> dynamically.


Neither of these is forced to use calloc malloc realloc, since the number
of files can be limited, and memmove/wmemmove are implementable with no
intermediary array.

> It is not a library function but do you want to include variable
> length arrays as dealing with allocated memory in some way?


I suspect this is often done using the stack.

Francois Grieu
 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      04-24-2012
On 4/23/2012 5:18 PM, Francois Grieu wrote:
> On 23/04/2012 21:26, Barry Schwarz wrote:
>> On Mon, 23 Apr 2012 20:01:49 +0200, Francois Grieu<>
>> wrote:
>>
>>> I'm trying to list all the standard C99/C2011 library functions that, by
>>> design, must deal with allocated memory in any way.
>>>
>>> I've seen calloc malloc realloc free. Is there any other?
>>> If strdup was a standard function (it is not), it would be in my list.

>>
>> strdup doesn't really "deal" with allocated memory so much as it uses
>> the allocated memory malloc provided.

>
> It would interest me because one can not implement strdup without calling
> calloc malloc realloc (since the result must be a valid argument to free).


When did strdup() appear in the Standard library? I don't have
the actual Standard, but strdup() is absent from the ISO/IEC 9899:201x
draft of 2011-04-12.

--
Eric Sosman
d
 
Reply With Quote
 
Quentin Pope
Guest
Posts: n/a
 
      04-24-2012
On 24 Apr 2012 at 1:55, Eric Sosman wrote:
> On 4/23/2012 5:18 PM, Francois Grieu wrote:
>>> On Mon, 23 Apr 2012 20:01:49 +0200, Francois Grieu<>
>>> wrote:
>>>> If strdup was a standard function (it is not), it would be in my list.

<snip>
>> It would interest me because one can not implement strdup without calling
>> calloc malloc realloc (since the result must be a valid argument to free).

>
> When did strdup() appear in the Standard library? I don't have
> the actual Standard, but strdup() is absent from the ISO/IEC 9899:201x
> draft of 2011-04-12.


Why deliberately misrepresent people Eric? Francois said right at the
start that he knows strdup is not standard.

In fact I think he is wrong for another reason. Pointers from malloc
needed to be aligned for all types: a library strdup() function could
potentially use an unaligned malloc-like function as long as the data
structure was still compatible with free().

 
Reply With Quote
 
Dr Nick
Guest
Posts: n/a
 
      04-24-2012
Quentin Pope <> writes:

> On 24 Apr 2012 at 1:55, Eric Sosman wrote:
>> On 4/23/2012 5:18 PM, Francois Grieu wrote:
>>>> On Mon, 23 Apr 2012 20:01:49 +0200, Francois Grieu<>
>>>> wrote:
>>>>> If strdup was a standard function (it is not), it would be in my list.

> <snip>
>>> It would interest me because one can not implement strdup without calling
>>> calloc malloc realloc (since the result must be a valid argument to free).

>>
>> When did strdup() appear in the Standard library? I don't have
>> the actual Standard, but strdup() is absent from the ISO/IEC 9899:201x
>> draft of 2011-04-12.

>
> Why deliberately misrepresent people Eric? Francois said right at the
> start that he knows strdup is not standard.
>
> In fact I think he is wrong for another reason. Pointers from malloc
> needed to be aligned for all types: a library strdup() function could
> potentially use an unaligned malloc-like function as long as the data
> structure was still compatible with free().


I /think/ it could use something else entirely as long as the pointer so
generated had a magic value that free() could recognise.

So strdup could allocate from an entirely separate pool or pools and
free knows the bounds of any such pools and so knows what do with it.
Could be useful if the use pattern is lots and lots of roughly equal
sized allocations and frees. It would need to do some undefined stuff
in checking pointers against each other to know, but it's part of the
implementation and so allowed to do this.

Am I right?
--
Online waterways route planner | http://canalplan.eu
Plan trips, see photos, check facilities | http://canalplan.org.uk
 
Reply With Quote
 
Malcolm McLean
Guest
Posts: n/a
 
      04-24-2012
בתאריך יום ש*י,23 באפריל 2012 19:01:49 UTC+1, מאת Francois Grieu:
> I'm trying to list all the standard C99/C2011 library functions that, by
> design, must deal with allocated memory in any way.
>
> I've seen calloc malloc realloc free. Is there any other?
> If strdup was a standard function (it is not), it would be in my list.
>

None of them, except for malloc(), realloc() and free() themselves.

This appears to have been a deliberate design decision. strdup() is an obvious candidate for a library function, but including it would make the string library dependent on an allocation package being available.

--
Basic Algorithms: memory games - how to write your own fast allocators
http://www.malcolmmclean.site11.com/wwww

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
AMD64 or Semperon, deal or no deal? Tad Confused Computer Information 7 04-13-2006 05:43 PM
deal or no deal rbt Python 7 12-28-2005 08:57 PM
How can I insure that the objects of a class must be allocated in the heap? PengYu.UT@gmail.com C++ 10 11-10-2005 01:24 AM
Dynamically Allocated Memory vs. Statically allocated Memory csnerd@gmail.com C++ 5 12-09-2004 01:44 AM
Must I free allocated memory before the program exits? Ning C Programming 4 09-26-2003 02:08 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57