Thread: malloc function

  1. #1
    Registered User
    Join Date
    Sep 2018
    Posts
    9

    malloc function

    Im learning the dynamic storage allocation functions and I have a question about why this is happening. I made a function my_malloc that takes my request in bytes as argument and should return a pointer to the block of memory. And I wanted to allocate space for an array of 10 numbers, so it should be 40 bytes right? After the call of my_malloc, when I check the sizeof of the new array it tells me that has 8 bytes, so having a length of 2 elements. When I initialize the array to 100 elements(more than what I asked for) there is no error or something..
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void *my_malloc(int n);
    
    int main(void)
    {
        int *p, len;
        
        printf("my malloc request: %d bytes\n",(int)(10 * sizeof(int)));
        
        p = my_malloc(10 * sizeof(int));
        
        printf("Values after my_malloc call:\n\n"
               "size p: %d bytes\n", (int)sizeof(p));
        
        for(int i = 0; i < 100; i++) //Initializing all elements of p in 1
            p[i] = 1;
        
        len = sizeof(p)/sizeof(int);
        printf("len: %d\n", len);
        
        for(int i = 0; i < 100; i++) //printing the array
            printf("%d ", p[i]);
        printf("\n");
        
        len = sizeof(p)/sizeof(int);
        
        printf("len: %d\n", len);
        printf("size p: %d bytes\n", (int)sizeof(p));
        printf("\n");
        
        return 0;
    }
    
    void *my_malloc(int n)
    {
        void *p = malloc(n);
        printf("Values inside my_malloc:\n\n"
                "n : %d\n", n);
        printf("size p : %d bytes\n\n",(int)sizeof(p));
        if (p == NULL) {
            printf("malloc failed in my_malloc function.\n");
            exit(EXIT_FAILURE);
        } else
            return p;
    }
    malloc function-captura-de-pantalla-de-2019-04-27-14-13-48-jpg

    Can someone please explain me what's happening here? and/or what i'm doing wrong?
    Last edited by Griffith; 04-27-2019 at 11:22 AM.

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    sizeof cannot give you the size of dynamically allocated memory. It ends up telling you the size of the pointer you use.

    A static array's size on the other hand is known at compile time, so sizeof does give the expected value.
    Devoted my life to programming...

  3. #3
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,101
    Simple. You are returning a pointer to the allocated memory, not an array of ints!

    Code:
    sizeof(p)
    This returns the size of the pointer, NOT the size of the memory allocated. "sizeof(p)" will always return the same value if you are allocating 4 ints or 400 ints!

    If you malloc() space for 10 ints, and the return from malloc() is not NULL, then you can assume the size of the memory allocated is 10 * sizeof(int), no more and no less!

    You can then use p as an array name, as in "p[offset]"

    Please study a good book on C, on the subject of Dynamic Memory Allocation, not online tutorials, or YouTube videos.

  4. #4
    Registered User
    Join Date
    Sep 2018
    Posts
    9
    Quote Originally Posted by rstanley View Post
    no more and no less!
    But when im initializing the array, it lets me do it far ahead of the 10 ints that I wanted. I was expecting some kind of warning from the compiler, or weird numbers after the 10th element, but no. And before that I thought that malloc gives me a block of memory that I cannot overflow it. Or is like I have the guarantee that 40 bytes will be asigned for me, after that who knows?

  5. #5
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,101
    Quote Originally Posted by Griffith View Post
    But when im initializing the array, it lets me do it far ahead of the 10 ints that I wanted. I was expecting some kind of warning from the compiler, or weird numbers after the 10th element, but no. And before that I thought that malloc gives me a block of memory that I cannot overflow it. Or is like I have the guarantee that 40 bytes will be asigned for me, after that who knows?
    That is one of the main problems with C Programming. There is NO BOUNDS CHECKING of buffer overflow in either static arrays, or in dynamically allocated memory!!!

    It is the responsibility of the programmer to prevent such errors!!! You can easily corrupt the stack or the heap if you don't!!!

    If only Mickey$oft understood that fact from day one!

  6. #6
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    Yeah, well, writing to memory past the one that's officially allocated for you invokes "undefined behavior", meaning that we don't know what might happen. In most modern systems, the operating system allocates memory by chunks, so it may not crash if you're lucky. But, the library may be using that space before or after the memory it returned to save metadata information, and you writing to it would corrupt it, maybe making the library unable to "realloc()" or "free()" it later.
    Devoted my life to programming...

  7. #7
    Registered User
    Join Date
    Apr 2019
    Posts
    62
    Quote Originally Posted by rstanley View Post
    If only Mickey$oft understood that fact from day one!
    Oh, they knew all about bounds checking. It turns out that this is a moderately easy problem to solve from the perspective of a single programmers, but a relatively difficult probably to solve from a project management perspective. If you have 20 programmers working on the Windows kernel, the amount of times they'll will make mistakes with bounds checking grows (probably) exponentially. Programmers may assume the size of an array is not what it is, or the size changes due to requirements, or due to complex conditionals or other interactions the index into the array grows more than they had anticipated.

    It's not like Microsoft is unique in having this problem, virtually all large software written in C ends up with bounds checking problems. Modern compilers can help with this, but to act like MS was ignorant of the problem is just wrong.

  8. #8
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    [quote]It's not like Microsoft is unique in having this problem [\quote]
    *Like*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. malloc function
    By Nikosant03 in forum C Programming
    Replies: 3
    Last Post: 02-10-2019, 02:24 PM
  2. malloc in function
    By mugen in forum C Programming
    Replies: 13
    Last Post: 03-14-2010, 11:27 AM
  3. malloc function
    By roaan in forum C Programming
    Replies: 9
    Last Post: 08-14-2009, 04:48 AM
  4. using malloc in a function
    By cuizy in forum C Programming
    Replies: 10
    Last Post: 08-13-2009, 01:56 PM
  5. about malloc function
    By enes in forum C Programming
    Replies: 1
    Last Post: 01-27-2002, 09:33 AM

Tags for this Thread