473,902 Members | 5,194 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

malloc() implementation

Hello together,

We have to make our own implementation of malloc() under Linux. Does
anybody can give me a hint where to start?

Our implementation should only work under Linux.

Thanks

Karsten

May 31 '06
49 23208
Haider said:
Please go to the following link you will get a copy of K&R in pdf
format


That's incitement to copyright violation.

I've sent a complaint to gr**********@go ogle.com - hopefully they can
explain to Haider about "intellectu al property". Perhaps others here would
care to do the same.

I have also sent a note to the relevant person at Prentice-Hall, advising
him of the existence of this illicit electronic copy of K&R.

Not wanting to send him duff gen, I checked the file first. It is indeed a
copy of K&R, but it is a very poor-quality copy. Learners of C would be
much better served by buying a paper copy of K&R. This is better because:

(a) you can read it with a clear conscience;
(b) you can read it independently of any electronic device - no batteries
required;
(c) you can be sure that you have the correct text;
(d) you can look things up much more quickly.

Actually, (d) surprised me. But it's true. Having a copy of K&R right there
on the desk in front of me, I find myself reaching for it when I need to
check, say, which way round args 2 and 3 to fread/fwrite go, because it's
actually less hassle than firing up a terminal, loading the Standard into a
searchable viewer of some kind, and typing in my search criteria.

If you haven't got a paper copy of K&R2, pick up the phone, get on to your
bookshop, and order one *today*.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Jun 1 '06 #21
Eric Sosman wrote:
Clever Monkey wrote:
Flash Gordon wrote:
John Devereux wrote:

Flash Gordon <sp**@flash-gordon.me.uk> writes:

> You can't write a malloc routine in standard C which might be part of
> why it is in the standard library, hence the suggestion of asking in a
> Linux group.
What you *can* do though is statically allocate a memory buffer, then
allocate out of that with your own "malloc".
True, as long as you call is something other than malloc ;-)
It all depends on what the OP actually wants to achieve.

Wouldn't interpositionin g allow you to define a default global symbol
"malloc" that would essentially overwrite the Standard one?

Not, mind you, something I recommend, but isn't one of the hallmarks
of C that one can do this without constraint, making all code
(including any external libs that are linked in later) use the
non-standard function?


"Hallmark of C ..." Fuzzy concept, that. Yet the Rationale
invokes the "Spirit of C," so perhaps we can dither a bit without
risk of too much castigation.

I certainly hope so! I meant it simply as "a distinguishing
characteristic or characteristics ".
In my own (less than universal) experience, two obstacles to
interposing your own malloc() -- or sqrt(), or whatever -- are
fairly frequently found:

- The Standard library may be incorporated into the program
as an indivisible unit: You get the whole thing, or nothing
at all. This is/was the case on VMS in the Old Days, when
the whole schmear was implemented as one giant monolithic
shared library. VMS' linker would squawk about conflicting
symbol definitions if your home-grown malloc() had the same
name as the malloc() in the library.
Right, I recall that older C libs from Borland also had the same issue.
Point taken.
- The library itself may use unadvertised knowledge of its own
internals. (It pretty much must do so if things like atexit()
and fflush(NULL) are to work.) For example, imagine an fopen()
that for performance reasons wants to allocate an I/O buffer
on a memory page boundary. It might call a library-private
_malloc_pages() function to get such specially-aligned memory.
Fine so far: You haven't replaced _malloc_pages() , so the
library's own version is used. But what if fopen() then hands
the buffer memory to free()? It's (presumably) your free(),
not the library's free(), that gets called -- and what are
the chances that your free() and the library's free() use
identical data structures?
Ah, so if that internal symbol was linked in some other manner, it would
not see your symbol. This is, in fact, what van der Linden mentions in
his discussion of interpositionin g. On his reference platform at least
(Solaris) he indicates that danger in that everything linked in later
after your redefined symbol will see it.

If I understand you correctly, this is not any sort of guarantee, and
your redefined symbol will not occlude the standard symbol in a properly
defined manner.
It's a shame, really, that C doesn't have a mechanism to allow
overriding parts of the library. Because abuses of dynamic memory
are so common, the malloc() suite is perhaps the most mouth-watering
override target, but there are others: A gets() that bleats whenever
called, or a scanf() that complains if given a "%s", even an acos()
that dumps a stack trace if its argument is larger than 1. The C
Standard, though, makes a point of separating the roles of implementor
and user -- and having lived on both sides of that fence, I for one
find the fence something of a comfort.

Indeed. I know that when we invented our C library back in the day when
such a thing was rare and expensive on the platforms we needed to
target, we invented a naming scheme of m_whatever(). It becomes clearer
to me why we did that.

Do be somewhat more on topic, I note that for a C project I worked on
recently that did a lot of memory allocation/deallocation we ended up
making a custom_malloc() that could be redefined to a debug_malloc() at
compile time. That caught a lot of potential crashes in the early
phases of the development. Once we shook the bugs out, custom_malloc()
was pointed back at malloc().
Jun 1 '06 #22

Richard Heathfield wrote:
Haider said:
Please go to the following link you will get a copy of K&R in pdf
format
That's incitement to copyright violation.

I've sent a complaint to gr**********@go ogle.com - hopefully they can
explain to Haider about "intellectu al property". Perhaps others here would
care to do the same.

it is for those peoples who don't have a copy and still don't want to
get one as far as gr**********@go ogle.com is concern I got the link by
as the serch result of google itself. I have also sent a note to the relevant person at Prentice-Hall, advising
him of the existence of this illicit electronic copy of K&R.

Not wanting to send him duff gen, I checked the file first. It is indeed a
copy of K&R, but it is a very poor-quality copy. Learners of C would be
much better served by buying a paper copy of K&R. This is better because:

(a) you can read it with a clear conscience;
(b) you can read it independently of any electronic device - no batteries
required;
(c) you can be sure that you have the correct text;
(d) you can look things up much more quickly.

Actually, (d) surprised me. But it's true. Having a copy of K&R right there
on the desk in front of me, I find myself reaching for it when I need to
check, say, which way round args 2 and 3 to fread/fwrite go, because it's
actually less hassle than firing up a terminal, loading the Standard into a
searchable viewer of some kind, and typing in my search criteria.

If you haven't got a paper copy of K&R2, pick up the phone, get on to your
bookshop, and order one *today*.
But do read the book. --
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)


Jun 2 '06 #23
Haider wrote:
Richard Heathfield wrote:
Haider said:
Please go to the following link you will get a copy of K&R in pdf
format
That's incitement to copyright violation.

I've sent a complaint to gr**********@go ogle.com - hopefully they can
explain to Haider about "intellectu al property". Perhaps others here would
care to do the same.

it is for those peoples who don't have a copy and still don't want to
get one...


The book is available only via purchase. Free downloads on the Internet
are illegal and a copyright violation. A newbie, who may not know
better, might read your post and go ahead and download it, thus morally
and legally compromising himself.
as far as gr**********@go ogle.com is concern I got the link by
as the serch result of google itself.


Try giving this defense in court.

Google only provides a search service for material on the Internet.
Their legality is something that the user has to be aware of.

.... snip ...
If you haven't got a paper copy of K&R2, pick up the phone, get on to your
bookshop, and order one *today*.

But do read the book.


Hahahahahaha!

Your prompting Richard Heathfield to read K&R...!?!
:)

Jun 2 '06 #24
Haider wrote:
Richard Heathfield wrote:
Haider said:
Please go to the following link you will get a copy of K&R in pdf
format That's incitement to copyright violation.

I've sent a complaint to gr**********@go ogle.com - hopefully they can
explain to Haider about "intellectu al property". Perhaps others here would
care to do the same.

it is for those peoples who don't have a copy and still don't want to
get one


You mean thieves? I'm not too fond of them myself. After all K&R put a
lot of work in to that book (including the development of the language
it describes) so they are entitled to receive payment for it.
as far as gr**********@go ogle.com is concern I got the link by
as the serch result of google itself.


Irrelevant. You can find all sorts of illegal stuff using Google, that
does not make posting links to it acceptable.

<snip>
If you haven't got a paper copy of K&R2, pick up the phone, get on to your
bookshop, and order one *today*.

But do read the book.


Only *after* buying a copy.
--
Flash Gordon, living in interesting times.
Web site - http://home.flash-gordon.me.uk/
comp.lang.c posting guidelines and intro:
http://clc-wiki.net/wiki/Intro_to_clc
Jun 2 '06 #25

santosh wrote:
Haider wrote:
Richard Heathfield wrote:
Haider said:

> Please go to the following link you will get a copy of K&R in pdf
> format

That's incitement to copyright violation.

I've sent a complaint to gr**********@go ogle.com - hopefully they can
explain to Haider about "intellectu al property". Perhaps others here would
care to do the same.
it is for those peoples who don't have a copy and still don't want to
get one...


The book is available only via purchase. Free downloads on the Internet
are illegal and a copyright violation. A newbie, who may not know
better, might read your post and go ahead and download it, thus morally
and legally compromising himself.

but now newbie will purchase a book also till now Richard Heathfield
inform Prentice Hall
and they took steps to remove the link.
so don't worry.
as far as gr**********@go ogle.com is concern I got the link by
as the serch result of google itself.


Try giving this defense in court.

Good. Google only provides a search service for material on the Internet.
Their legality is something that the user has to be aware of.

... snip ...
If you haven't got a paper copy of K&R2, pick up the phone, get on to your
bookshop, and order one *today*.

But do read the book.


Hahahahahaha!

Your prompting Richard Heathfield to read K&R...!?!
:)


Jun 2 '06 #26

Flash Gordon wrote:
Haider wrote:
Richard Heathfield wrote:
Haider said:

Please go to the following link you will get a copy of K&R in pdf
format
That's incitement to copyright violation.

I've sent a complaint to gr**********@go ogle.com - hopefully they can
explain to Haider about "intellectu al property". Perhaps others here would
care to do the same.

it is for those peoples who don't have a copy and still don't want to
get one


You mean thieves? I'm not too fond of them myself. After all K&R put a
lot of work in to that book (including the development of the language
it describes) so they are entitled to receive payment for it.

they deserves and they will bcoz this book is such a good one that
anyone who have a look of it will purchase a copy.
> as far as gr**********@go ogle.com is concern I got the link by
as the serch result of google itself.


Irrelevant. You can find all sorts of illegal stuff using Google, that
does not make posting links to it acceptable.

<snip>
If you haven't got a paper copy of K&R2, pick up the phone, get on to your
bookshop, and order one *today*.

But do read the book.


Only *after* buying a copy.
--
Flash Gordon, living in interesting times.
Web site - http://home.flash-gordon.me.uk/
comp.lang.c posting guidelines and intro:
http://clc-wiki.net/wiki/Intro_to_clc


Jun 2 '06 #27
2006-06-02 <11************ *********@u72g2 000cwu.googlegr oups.com>, santosh wrote:
as far as gr**********@go ogle.com is concern I got the link by
as the serch result of google itself.


Try giving this defense in court.

Google only provides a search service for material on the Internet.
Their legality is something that the user has to be aware of.


So it's legal for Google to link to it but not for a private citizen to
do so? (Not that I agree with copyright violation, but how's he supposed
to know that a search result he found isn't legitimate?)
Jun 2 '06 #28

Jordan Abel wrote:
2006-06-02 <11************ *********@u72g2 000cwu.googlegr oups.com>, santosh wrote:
as far as gr**********@go ogle.com is concern I got the link by
as the serch result of google itself.


Try giving this defense in court.

Google only provides a search service for material on the Internet.
Their legality is something that the user has to be aware of.


So it's legal for Google to link to it but not for a private citizen to
do so? (Not that I agree with copyright violation, but how's he supposed
to know that a search result he found isn't legitimate?)


[1] It is difficult (if not impossible) to build such discretion in a
system.
It is however perfectly acceptable to ask for such a quality among
human beings.

[2] Google is not soliciting that you try that out. The poster in
question did.
That's illegal, if I'm not mistaken, from a legal perspective.

Jun 2 '06 #29
Jordan Abel wrote:
2006-06-02 <11************ *********@u72g2 000cwu.googlegr oups.com>, santosh wrote:
as far as gr**********@go ogle.com is concern I got the link by
as the serch result of google itself.


Try giving this defense in court.

Google only provides a search service for material on the Internet.
Their legality is something that the user has to be aware of.


So it's legal for Google to link to it but not for a private citizen to
do so? (Not that I agree with copyright violation, but how's he supposed
to know that a search result he found isn't legitimate?)


Google uses automated methods to build their indexes. Their staff
cannot review each and every webpage their web crawlers traverse.

Usually, when authors want to protect their IP, they provide some sort
of copyright notice or license etc. If this is available, people are
expected to read and follow it. Now granted tons of legal and illegal
stuff on the Internet is accompanied by no terms of use. A newbie might
not even know that the said material is illegal. That's why Richard
Heathfield posted a follow-up to Haider's unwanted link.

Ultimately, people have one or two little things called intelligence,
common sense and conscience, which they are expected to exercise.
Companies and web-bots have none of the above. Err...atleast not the
third faculty.

Jun 2 '06 #30

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

Similar topics

11
8582
by: Aire | last post by:
On some small-memory systems, is it true that "malloc()" is not supported by the C library? Why is 'dynamic memory allocation' so avoided on such systems? What are major problems of using malloc() here? Thanks!
42
2186
by: Joris Adriaenssens | last post by:
This is my first posting, please excuse me if it is off-topic. I'm learning to program in C. It's been almost ten years I've been programming and a lot of things have changed apparently. I understand from other postings that casting a result from malloc isn't good. In the past I have always been casting the malloc. I think it was even necessary. (But that's a long time ago, I hadn't heard of a standard for C these days). Was it...
36
2292
by: Martin Andert | last post by:
Hello, I have a question regarding malloc and free. Here my code sample: int main() { /* allocating dynamic memory for array */ int* array = (int*) malloc(5 * sizeof(int)); /* ... program code ... */
12
3289
by: gooch | last post by:
I originally posted the following code in a group discussing threads but after further research I think I have a c question about the code. I know there are a couple of non standard c includes here and the POSIX stuff is non standard but this is how I stumbled onto this question. #include <INTEGRITY.h> #include <stdlib.h> #include <stdio.h> #include <pthread.h> #include <signal.h>
21
470
by: ramu | last post by:
Hi, Will the memory allocated by malloc and realloc be contiguous? regards
82
31186
by: quiberon2 | last post by:
Hi, Sorry if it might be a stupid question but what should returns malloc(0) ? void *ptr = malloc(0); I am running gcc 3.3.5 and a non-null address is returned. ( in the compiler that I am currently implementing, NULL is returned. Is it wrong ?)
6
5500
by: sam_cit | last post by:
Hi Everyone, I wanted to know as to how malloc() works, if my understanding is correct, it is implementation specific of the vendor who provides the library(alloc.h). If so, is there any standard as to how it should be implemented? If there is no space in the RAM, will malloc() return NULL or will it allocate a memory in a new page using virtual memory and return the
71
19169
by: desktop | last post by:
I have read in Bjarne Stroustrup that using malloc and free should be avoided in C++ because they deal with uninitialized memory and one should instead use new and delete. But why is that a problem? I cannot see why using malloc instead of new does not give the same result.
10
1916
by: somenath | last post by:
Hi All, I have one question regarding return value cast of malloc. I learned that we should not cast the return value of malloc because it is bug hider. But my question is as mentioned bellow . Lets say I have not included stdlib.h in my program still I am using malloc so compiler will throw warring because with out prototype
8
7270
by: ramsatishv | last post by:
Hi Group, I have one question. If I am allocating memory of 38 bytes to an integer pointer, what will be the memory actually allocated? Will there be any memory alignment concept in malloc? Please let me know the reasons if it is yes / no.
0
9997
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
9845
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
11279
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
10499
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
8047
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
5893
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
6085
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4307
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3323
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.