Thread: How to limit the memory given by malloc

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    2

    How to limit the memory given by malloc

    Hi below is the simple c code.
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    int main()
    {
            char const*ptr;
            ptr = malloc(sizeof(char) * 4);
            strcpy(ptr,"jyoti ranjan");
            printf("ptr = %s\n",ptr);
            return 0;
    }
    output : ptr = jyoti ranjan

    above everything is fine.
    but is there any way by which malloc can only give 4 byte only.
    i.e ptr should not able to take more than 4 charecters.

    Regards
    jyoti ranjan panda

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    malloc() gives you what you ask for. Copying more than allocated is *Undefined behaviour*.
    So malloc() is not giving you more. but you're trashing heap.

    The same for stack allocated array.
    char a[4];
    strcpy(a,"foo bar egg spam");

    will perhaps works too. It's not giving you more than 4!
    It's just you've undefined behavior code that appeared to be working by luck. But later when you allocate again/ free, the error may show up. Try it.
    It might blow up with different compiler/platform/...

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Malloc only gave you what you requested - it never gives more or less. You just got lucky and used some memory that wasn't given to you, by malloc, at all.

    You got away with it this time. Don't count on that working though.

  4. #4
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    A few more comments:

    1) sizeof(char) is always going to be 1 so you don't need to do a sizeof for your allocation really.
    2) "i.e ptr should not able to take more than 4 charecters" - ptr does not take characters, it is a pointer, thus it holds a memory address, which in your case is where a char array is located. When you are strcpying you are not copying "into" ptr, you are copying at the location ptr points to.
    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.

  5. #5
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,732
    Quote Originally Posted by claudiu View Post
    1) sizeof(char) is always going to be 1 so you don't need to do a sizeof for your allocation really.
    I think that approach is better. It tells everyone else reading your code that you know what you're doing and it also becomes easier to be spotted and changed later.
    Devoted my life to programming...

  6. #6
    Registered User
    Join Date
    Nov 2008
    Posts
    127
    Quote Originally Posted by Adak View Post
    Malloc only gave you what you requested - it never gives more or less.
    I thought malloc always gave at least what you requested. Unless you don't have enough space and it then returns null. This is how wikipedia explains malloc anyway.

  7. #7
    Registered User
    Join Date
    Mar 2011
    Posts
    278
    Why would it give you more? Seems a bit indeterminate. You ask for 100 bytes and it gives you (ties up) 101 bytes? or 100 Million?

    Can you quote the part of wiki's page that says malloc allocates an arbitrarily large chunk of memory? Perhaps it says that it rounds up to some convenient boundary/number of bytes

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Cmylove View Post
    Hi below is the simple c code.
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    int main()
    {
            char const*ptr;
            ptr = malloc(sizeof(char) * 4);
            strcpy(ptr,"jyoti ranjan");
            printf("ptr = %s\n",ptr);
            return 0;
    }
    output : ptr = jyoti ranjan

    above everything is fine.
    but is there any way by which malloc can only give 4 byte only.
    i.e ptr should not able to take more than 4 charecters.

    Regards
    jyoti ranjan panda
    Believe me malloc() only gave you 4 bytes... what you did was "drawing outside the lines" leading to potentially trashing memory and causing seg-fault or access violation errors (depending on your OS). That this worked at all is the result of 2 things that both had to happen at once: a simplistic test and pure dumb luck.

    malloc() always gives you at least as much as you ask for... generally this is rounded up to the next even multiple of either 4 or 8 bytes depending on your OS (32 or 64 bits)... asking for 4 bytes will NOT give you 15.

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by mike65535 View Post
    Why would it give you more? Seems a bit indeterminate. You ask for 100 bytes and it gives you (ties up) 101 bytes? or 100 Million?

    Can you quote the part of wiki's page that says malloc allocates an arbitrarily large chunk of memory?
    It's not indeterminate and it's not wasteful...

    It's about variable alignment... most OSs allign stuff on either 4 (32bit) or 8 (64bit) byte boundaries. So if you ask for 2 bytes you will get 4 or 8... asking for 1025 (1K+1) will get you 1028 or 1032... not 10,000...
    Last edited by CommonTater; 05-10-2011 at 08:44 AM.

  10. #10
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by CommonTater View Post
    It's not indeterminate and it's not wasteful...

    It's about variable alignment... most OSs allign stuff on either 4 (32bit) or 8 (64bit) byte boundaries. So if you ask for 2 bytes you will get 4 or 8... asking for 1025 (1K+1) will get you 1028 or 1032... not 10,000...
    Actually, OS kernels allocate in entire pages. It's the C library itself that splits that up, so YMMV. This is why the standard says you must get at least what you asked for.

    Ie, modern compilers/libraries could allocate the exact amount.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fread memory size limit
    By 3Nex in forum C Programming
    Replies: 1
    Last Post: 05-22-2010, 07:33 AM
  2. how to malloc memory?
    By zcrself in forum C Programming
    Replies: 11
    Last Post: 04-22-2010, 08:47 AM
  3. allocating memory with malloc
    By steve1_rm in forum C++ Programming
    Replies: 32
    Last Post: 12-18-2008, 06:51 PM
  4. How to LIMIT share memory in C ???
    By tritong in forum C Programming
    Replies: 5
    Last Post: 09-07-2006, 04:15 AM
  5. Is there a limit on the number of malloc calls ?
    By krissy in forum Windows Programming
    Replies: 3
    Last Post: 03-19-2006, 12:26 PM