#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    30
    Rep Power
    2

    Allocating Memory Without malloc()


    Hey guys, is it possible to create a function without malloc() to allocate memory?

    If not, then how is the malloc() function able to access the memory if it is just a normal function and not any keywords in the language.

    Thanks.
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    9
    Rep Power
    0
    Originally Posted by MiguelC
    Hey guys, is it possible to create a function without malloc() to allocate memory?

    If not, then how is the malloc() function able to access the memory if it is just a normal function and not any keywords in the language.

    Thanks.
    #include <stdio.h>
    #include <string.h>

    int main(int argc, char *argv[])
    {
    char *ptr;

    /* ptr will point to the location where malloc()
    allocated the memory */
    ptr = (char *) malloc (20);
    strcpy(ptr, "Hello World!");

    printf("%s\n", ptr);

    free(ptr);

    return 0;
    }

    I have a video tutorial on malloc() function if you want to watch how it is used.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    30
    Rep Power
    2
    Originally Posted by TheShellWave
    #include <stdio.h>
    #include <string.h>

    int main(int argc, char *argv[])
    {
    char *ptr;

    /* ptr will point to the location where malloc()
    allocated the memory */
    ptr = (char *) malloc (20);
    strcpy(ptr, "Hello World!");

    printf("%s\n", ptr);

    free(ptr);

    return 0;
    }

    I have a video tutorial on malloc() function if you want to watch how it is used.
    Hey man, could you please answer my question?
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    9
    Rep Power
    0
    Originally Posted by MiguelC
    Hey man, could you please answer my question?
    No, you cannot allocate memory without malloc. Second question is in the code. Look at the "ptr" variable.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    30
    Rep Power
    2
    Originally Posted by TheShellWave
    No, you cannot allocate memory without malloc. Second question is in the code. Look at the "ptr" variable.
    Thanks.
  10. #6
  11. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,175
    Rep Power
    2222
    Originally Posted by MiguelC
    Hey guys, is it possible to create a function without malloc() to allocate memory?
    In principle, yes it should be possible. After all, somebody else wrote the code for malloc.

    In practice, no. Rewriting malloc would not be a trivial task. At the very least, you would need to have intimate knowledge of the heap and how to manipulate it, as well as to work out the data structures that would be needed, as well as thoroughly debugging that code. That you wold be asking this question tells me that you do not yet have anywhere near the skills and knowledge that you would need to rewrite malloc.

    Now my question is: why would you ever want to do such a thing?

    Originally Posted by MiguelC
    If not, then how is the malloc() function able to access the memory if it is just a normal function and not any keywords in the language.
    My understanding is that the source code for the Standard C Library functions exists out there somewhere.

    There are also utilities such as heapwalk for exploring the heap. You may be able to find source code for some of those.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    30
    Rep Power
    2
    Originally Posted by dwise1_aol
    Now my question is: why would you ever want to do such a thing?
    Nothing. I just want to know how to allocate memory using my own function like malloc().

    By the way, thanks for your answer.
  14. #8
  15. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,175
    Rep Power
    2222
    As you may know already, many development environments (eg, Visual Studio) not only support the Standard C Library and the Standard Template Library, but also provide several other functions and libraries, such as sockets, graphics, etc. Some of these extra functions/libraries are kind of standard, like sockets and some graphics libraries, while many are proprietary.

    One such group of extra functions are ones that you can use to work with the heap. I know that Microsoft's Visual Studio 2008 has several heap-oriented functions and datatypes and I kind of remember having seen some in Borland C++ v3.x back in the day. Since the heap does belong to your process, I would think that there would be no problem accessing that region of memory. As I said, it would mainly be a matter of being able to work with the heap's data structures, etc.

    Come to think of it, in C++ I've seen reference to being able to replace new and delete (the C++ versions of malloc and free) with your own. Again, I don't see why you would want to, but apparently it's not unheard of.
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2004
    Location
    Sweden
    Posts
    263
    Rep Power
    20
    Originally Posted by MiguelC
    Hey guys, is it possible to create a function without malloc() to allocate memory?

    If not, then how is the malloc() function able to access the memory if it is just a normal function and not any keywords in the language.

    Thanks.
    Is this what you asked for?

    Code:
    void* MyMalloc(size_t size)
    {
    	HANDLE hHeap = GetProcessHeap();
    	return HeapAlloc(hHeap,HEAP_ZERO_MEMORY,size);
    }
    
    void MyFree(void* ptr)
    {
    	HANDLE hHeap = GetProcessHeap();
    	HeapFree(hHeap,HEAP_NO_SERIALIZE,ptr);
    }
    I am not a c coder so you will have to excuse my code.

    To allocate memory you can also use apis GlobalAlloc, LocalAlloc, VirtualAlloc or CoGetMalloc
  18. #10
  19. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,392
    Rep Power
    1871
    And if your OS isn't windows, then what?

    Yes each OS has their own native memory allocation routine (sometimes several kinds, for different kinds of memory).

    The point of using malloc is that it is a standard function that every conforming implementation should provide. Meaning you can just use it and get on with solving your programming problems.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  20. #11
  21. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,900
    Rep Power
    481
    Or you could just fake it. Then you'd learn the considerations that go into malloc.
    Code:
    void*my_malloc(size_t size) {
      static char MySpace[1000000000];
      /* Insert your algorithm for managing the memory in MySpace here */
      return (void*)(MySpace + MyWellChosenOffset);
    }
    It's a good exercise, really. I work it in my dreams.

    Comments on this post

    • dwise1_aol agrees : Yes, play with the ideas and techniques in a sandbox. Sandboxes are great for working out all kinds of ideas safely.
    [code]Code tags[/code] are essential for python code and Makefiles!
  22. #12
  23. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    30
    Rep Power
    2
    I don't know if this is a good,stupid or correct question, but here you go.

    Are standard functions written in another language or are they built-in like they are part of the language?

    Also, I have been checking implementation of functions like getchar(), and I found things like: _IO_acquire_lock (_IO_stdin);. They do not have any type or anything. What are those?
  24. #13
  25. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,640
    Rep Power
    4247
    Originally Posted by MiguelC
    Are standard functions written in another language or are they built-in like they are part of the language?
    The C standard library functions are generally written mostly in C with a smattering of assembly language. The nice thing about standard functions is that they are guaranteed to be part of any compliant C compiler that you use on any platform. Therefore, whether you're programming in Linux, FreeBSD, Windows, Minix or whatever, you are guaranteed to have printf(), fopen(), fclose(), malloc() etc. on any standards compliant C compilers on those platforms.

    Functions such as GlobalAlloc(), GetProcessHeap() etc. can be called by your compiler on Windows, but they are not guaranteed to be available elsewhere, because they are not part of the C standard. However, a Windows compiler implementation of a standard function (say malloc()) may internally call GlobalAlloc() or whatever because, as long as malloc() is available and does what the C standard says it should, the internals of how it is implemented is not of concern. Sometimes the platform specific functions actually implement something better than the standard functions and you can use them if you like (knowing that your code may not be portable). For example, Windows has a OS specific function to seek to large file sizes (SetFilePointerEx()) because the standard-compliant version (fseek()) cannot handle offsets beyond 2GB.

    Originally Posted by MiguelC
    Also, I have been checking implementation of functions like getchar(), and I found things like: _IO_acquire_lock (_IO_stdin);. They do not have any type or anything. What are those?
    Yes, like I said, your implementation of getchar() may call any internal functions which are unique to your OS/compiler. If you look at the code for getchar() in another compiler or OS, you may notice that the code for it is completely different. For instance, here's the implementation of getchar() on libc for OpenBSD. Note that their version calls getc() internally, whose source code is in getc.c, whose source code in turn calls a few internal functions to implement it (some of those can be found here and so on). The point here is it doesn't matter what functions get called, as long as the compiler implements getchar() and getc() which are part of the standard. What happens inside those functions could be specific to the OS or compiler.
    Last edited by Scorpions4ever; October 3rd, 2013 at 11:13 PM.
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo

IMN logo majestic logo threadwatch logo seochat tools logo