hello friends,

could you guide me how i can write my own version of malloc and free function

Read K & R. You'll know how to do that.

By the way why do you want to make your own malloc function?

Read K & R. You'll know how to do that.

By the way why do you want to make your own malloc function?

what is k & r am new in this language and ... i face this question in my interview

K&R It's a book by the creaters of the C programming language

> am new in this language and ... i face this question in my interview
If knowing C is a requirement for whatever you're applying for, then you're not qualified, even if you manage to spoof the answer to this question.

You might be able to regurgitate the answer, but you still won't be able to explain it.
If the interviewer asks you what "this" does, will your answer begin with "ummmm"?

Say for argument you get this job (which you're unqualified for), are you going to be back asking people to solve your assignments as well?

writing your own malloc is not all that easy as you can see from the GNU implementation. Its largly os-dependent and is probably not something an entry-level programmer could accomplish. And nobody could write it in just the few minutes that an interview could accommodate.:eek:

Member Avatar for iamthwee

writing your own malloc is not all that easy as you can see from the GNU implementation. Its largly os-dependent and is probably not something an entry-level programmer could accomplish. And nobody could write it in just the few minutes that an interview could accommodate.:eek:

Well if that's what they're expecting I'd say -

what a freakin' glorified waste of time!

:rolleyes:

Reciting the simple one from K&R (in some form or other) is pretty doable in an interview context, and shows the interviewer that you know some useful bits of the language.

Writing a good malloc which has decent performance and resists fragmentation on the other hand is a much harder task.

hey all of you gus " am is new in this language " by this i mean that i have no professional experiance in c... and i was also thinking that it is not easy to write malloc function that is asked by you in just 20 minute ..

and i know well the basics of c programming language and am not giving any assinment to you guys i was just waiting for ur comment to check that is any simpler way to do this.

i tell those peple the whole idea how malloc work and in very simplified manner write a c code to show them so especially MR. SALEM don't guess anything i am a worthy person and quite efficient to do my work . ok

and my question was like that

they give me an array of memory say 10,000 character array and they ask to write ur own version of free and malloc function to allocate and dealocate the memory from this memory pool?

they give me an array of memory say 10,000 character array and they ask to write ur own version of free and malloc function to allocate and dealocate the memory from this memory pool?

We would really like to know how did you solved that.

Member Avatar for Pavlush

Very careful!

commented: Thanks for that. -1

We would really like to know how did you solved that.

Actually, this is very easily done...that is if you did some basic C-language based I guess- programming including pointers. In C, since you can use pointer arithmatic on a pointer that you have malloced initially. So, if you are to say make a pooled memory-or given a memory space in which you are allowed to access- do arithmatic pointer and divide up the memory. Use strucs and/or headers to make things simpler. If you are given an "initialized" memory space as a form of an array, than it is easier. Array indexes also satisfies the arithmatic, so you can put -1 or other magic number in an array to indicate its empty, and work your way out from there on.

If you are NOT allowed to malloc a memory space in the beginning, ie, you have to make ALL memory allocator from ground to up...well you will need some knowledge on OS and other things, altough there are some shortcuts regarding this. Still, it takes good 6 months to learn programming and basic sortings and other algorithms and an year to learn OS.

Actually, this is very easily done...that is if you did some basic C-language based I guess- programming including pointers. In C, since you can use pointer arithmatic on a pointer that you have malloced initially. So, if you are to say make a pooled memory-or given a memory space in which you are allowed to access- do arithmatic pointer and divide up the memory. Use strucs and/or headers to make things simpler. If you are given an "initialized" memory space as a form of an array, than it is easier. Array indexes also satisfies the arithmatic, so you can put -1 or other magic number in an array to indicate its empty, and work your way out from there on.

It's easier than that if the system allocation options are similar enough to malloc() to facilitate a thin wrapper. For example, I wrote an implementation of the standard library that defines malloc as such:

/*
    @description:
        Allocates space for an object whose size is specified by size.
*/
void *malloc(size_t size)
{
    uintptr_t mem = (uintptr_t)_sys_heapalloc(__sys_heap, size + sizeof(size_t));

    *(size_t*)mem = 0; /* General alignment */
    mem += sizeof(size_t);

    return (void*)mem;
}

Where _sys_heapalloc() is nothing more than a portability wrapper around the Win32 API's HeapAlloc():

/*
    @description:
        Allocate the specified number of bytes from the specified heap.
*/
void *_sys_heapalloc(_sys_handle_t heap, unsigned bytes)
{
    return HeapAlloc(heap, 0, bytes);
}

Barring initialization and teardown of the __sys_heap object in the C runtime code, that's all there is to it. And even then it's more complicated than the bare minimum because my library supports C11's aligned_alloc(), hence the bookkeeping information in front of the returned pointer.

The restriction is you can't use malloc() or anything like it, such as HeapAlloc() and AFAIK that is MS-Windows-specific function.

The restriction is you can't use malloc() or anything like it, such as HeapAlloc() and AFAIK that is MS-Windows-specific function.

I didn't see any such restriction mentioned in the thread, barring the implied restriction from K&R (though it still uses POSIX sbrk()). My implementation is inherently tied to Windows, and targets the CL compiler specifically for a number of reasons. Could you elaborate on what you meant by the quoted statement?

I didn't see any such restriction mentioned in the thread,

Since we don't have post numbers anymore I can't just give you a link to it but the op was given a large char array and asked to write his own malloc() and free() functions. I miss those post numbers alot because they were really handy in this kind of situations.

Could you elaborate on what you meant by the quoted statement?

Huh? What quoted statement?

Since we don't have post numbers anymore I can't just give you a link to it but the op was given a large char array and asked to write his own malloc() and free() functions.

Ah, I see now. It's this post. The time posted for each post is actually a link to the post's permanent URL. You can copy that and link to it instead of referring to post numbers.

Huh? What quoted statement?

Your statement that I quoted in my post with the forum's quote feature. :rolleyes:

The time posted for each post is actually a link to the post's permanent URL

Maybe you need to make that feature more obvious.

I still don't understand what you want me to elaborate on. I thought what I posted was pretty clear.

Maybe you need to make that feature more obvious.

I'm open to suggestions. It's currently no more obscure than the post number representing the same link under vBulletin. Though I'll certainly look into adding actual post numbers, as that's a useful feature as well.

Maybe a different color, or neon blinking lights saying "<<< Link here" :) Dani removed the post numbers some time ago because she thought they were confusing if another post referred to the number and the original post was later deleted. But that doesn't happen very often, maybe once in 10,000 posts???

The post numbers also had negative SEO implications.

I've done a lot of research into memory management for C/C++ software, including garbage collecting versions of malloc/free, reference-counting garbage collectors that can handle non-acyclic graphs (non-trivial, but implemented in major system software), and such. This work has been written up in technical journals.

If I were asked to show how to implement malloc/free in a job interview I think I would laugh, and tell them to find some other fool to work for them!

If I were asked to show how to implement malloc/free in a job interview I think I would laugh, and tell them to find some other fool to work for them!

Although the question has little practical purposes, the reason for an employer to ask such a question is to find out how much the interviewee really knows. A lot of people go to interview and do a lot of bloating without knowing very much. Such a question as this will help weed out the chaff from the wheat. If you want the job then you would show off your great programming skills and write the program that they asked.

Googled and found this. May prove useful Mem allocator. K & R version is also nice and simple.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.