Like Tree16Likes

Create malloc and free function in C?

This is a discussion on Create malloc and free function in C? within the C Programming forums, part of the General Programming Boards category; Originally Posted by manasij7479 That was actually my goal; to provide an abstraction for allocating memory to objects from the ...

  1. #16
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by manasij7479 View Post
    That was actually my goal; to provide an abstraction for allocating memory to objects from the memory owned by the program(there, the virtual machine).
    Is that possible with malloc?
    First I tried it with realloc but couldn't get the address of the remaining pool(if not returned to the system) after it was done.

    I thought that is what 'guestcheap' had in mind; since the other alternative wasn't very viable.
    Can't answer for linux but on Windows it's easy enough the OS actually provides this as API calls...

    Heap Functions (Windows)

    Basically you create a private heap... but it also gives you far better functionality than malloc() and free()... for example you can discover the size of any memory block with HeapSize() and you can confirm HeapFree() with HeapValidate()...

    I've often noticed that people seem naturally prone to go in the wrong direction ... they avoide the OS and it's first level APIs like they're going to die if they use them, when in fact getting closer to the core OS very often reveals increased power and capability.

  2. #17
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,528
    Quote Originally Posted by guestcheap View Post
    I got some help from Writing a memory manager - OSDev Wiki. I want to this but I have absolutely no idea how.
    Well, if you're interested in it (and also 'have absolutely no idea how' ) you can start by reading the tutorials and documents of the nice(possibly the best) site you found about OS Development.
    Manasij Mukherjee | gcc-4.9.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  3. #18
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    The only reason I can think of to actually write your own memory allocation library would be to provide something similar to a slab allocator (a memory manager that can allocate and deallocate identical chunks of memory, presumably for objects that are being allocated and freed very often).

    More about it here: Slab allocation - Wikipedia, the free encyclopedia

    However you are most certainly going to need to use OS specific stuff as most common sense slab allocators reside in kernel space.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  4. #19
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by guestcheap View Post
    Thanks for the replies...
    I got some help from Writing a memory manager - OSDev Wiki. I want to this but I have absolutely no idea how. Any help?
    The big question is "Why?" ... if it's a class assignment or even idle curiosity, ok I get that... but if you think you can second guess a well tested and optimized library you are probably in for a rude awakening. No matter how you do it, you're going to end up calling the underlying OS APIs at some point...

  5. #20
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,528
    Quote Originally Posted by CommonTater View Post
    Can't answer for linux but on Windows it's easy enough the OS actually provides this as API calls...

    Heap Functions (Windows)

    Basically you create a private heap... but it also gives you far better functionality than malloc() and free()... for example you can discover the size of any memory block with HeapSize() and you can confirm HeapFree() with HeapValidate()...

    I've often noticed that people seem naturally prone to go in the wrong direction ... they avoide the OS and it's first level APIs like they're going to die if they use them, when in fact getting closer to the core OS very often reveals increased power and capability.
    The Linux kernel api is still a little complicated for me because my unfortunate ignorance of most of the C standard library. I'm working to rectify that.

    Also, I'd be in a bad situation if someday, I need to demonstrate my program to someone coming from another platform.
    Manasij Mukherjee | gcc-4.9.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  6. #21
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by guestcheap View Post
    Thanks for the replies...
    I got some help from Writing a memory manager - OSDev Wiki. I want to this but I have absolutely no idea how.
    Are you writing an OS kernel? If so, I have no idea how you would start dealing with memory, but the first place I would look for clues would be the linux kernel. I imagine there could be some architecture specific issues (have a look at the link in that article to Paging, pretty sure you have to deal with that first).

    If you are not writing an OS kernel, then you are writing userspace code, which means you have no other choice except to use the routines provided by the operating system, the most fundamental of which is probably malloc.

    Ie,

    1) Short of writing kernel space code, which is going to be very platform specific (and you still haven't told us what platform!) the answer to your question is you CAN'T.

    2) Your question is more ambiguous than perhaps you perceive it to be. Are you wanting to write a memory pool? If you can't help us to understand why you want to do this and what it is for -- so that we can better understand the nature of the task -- very likely we can't help you at all.
    Last edited by MK27; 08-11-2011 at 07:33 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #22
    Registered User
    Join Date
    Aug 2011
    Posts
    10
    actually, i am making an OS kernel, on Ubuntu 11 running as a Virtual Machine on windows...
    MK27 likes this.

  8. #23
    Registered User
    Join Date
    Aug 2011
    Posts
    10
    Any updates?

    How can I allocate memory? I've told you why I need it and what OS I'm using? Could you guys at least give me a breakdown of the steps involved?

    Thanks

  9. #24
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,528
    Manasij Mukherjee | gcc-4.9.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  10. #25
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by guestcheap View Post
    Any updates?

    How can I allocate memory? I've told you why I need it and what OS I'm using? Could you guys at least give me a breakdown of the steps involved?
    Isn't that totally dependent on how your kernel works? How much of it do you have done so far?

    Quote Originally Posted by guestcheap View Post
    actually, i am making an OS kernel, on Ubuntu 11 running as a Virtual Machine on windows...
    Hmmm -- so where is the kernel going to run? Inside a virtual machine on ubuntu running inside a virtual machine on windows?
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  11. #26
    Registered User
    Join Date
    Aug 2011
    Posts
    10
    I haven't done anything except a printf, OSDEV wiki recommends memory management as the next step.
    Hmmm -- so where is the kernel going to run? Inside a virtual machine on ubuntu running inside a virtual machine on windows?
    Yes, I think its weird too, but that's how I'm running it

  12. #27
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by guestcheap View Post
    Yes, I think its weird too, but that's how I'm running it
    And you should not be surprised if it doesn't work when you un-nest it from all that virtualization...

    Virtual machines are pretty good, but they're not perfect...

  13. #28
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by CommonTater View Post
    Virtual machines are pretty good, but they're not perfect...
    They're virtually perfect.


    Quzah.
    Hope is the first step on the road to disappointment.

  14. #29
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Quote Originally Posted by quzah View Post
    They're virtually perfect.
    Quzah.
    Much like your code.....
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  15. #30
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by AndrewHunter View Post
    Much like your code.....
    I don't know if I'd go that far... I think I'd settle on "theoretically entertaining".


    Quzah.
    Hope is the first step on the road to disappointment.

Page 2 of 4 FirstFirst 1234 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. malloc/free help
    By stan4d2012 in forum C Programming
    Replies: 3
    Last Post: 04-13-2009, 08:04 PM
  2. malloc() and free()
    By someprogr in forum C Programming
    Replies: 1
    Last Post: 12-28-2007, 07:16 PM
  3. Malloc - Free giving double free or corruption error
    By andrew.bolster in forum C Programming
    Replies: 2
    Last Post: 11-02-2007, 07:22 AM
  4. need help with malloc-free
    By sleith in forum C Programming
    Replies: 4
    Last Post: 08-22-2007, 09:07 PM
  5. Function to check memory left from malloc and free?
    By Lechx in forum C Programming
    Replies: 4
    Last Post: 04-24-2006, 06:45 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21