Thread: malloc

  1. #1
    Unregistered
    Guest

    malloc

    Just getting into memory managment functions, if malloc allocates memory from the heap(any free space on the hard drive) in exactly the amounts required why is it assigned 10 here when we only need 5 plus one for the NULL at the end of the string 6 in all.



    #include<conio.h>
    #include <stdio.h>
    #include <string.h>
    #include <alloc.h>
    #include <process.h>

    int main(void)
    {
    char *str;

    /* allocate memory for string */
    if ((str = (char *) malloc(10)) == NULL)
    {
    printf("Not enough memory to allocate buffer\n");
    exit(1); /* terminate program if out of memory */
    }

    /* copy "Hello" into string */
    strcpy(str, "Hello");

    /* display string */
    printf("String is %s\n", str);

    /* free memory */
    free(str);
    getch();
    return 0;
    }

    ps. does the stack work like this, the amount of memory needed in main is places on the stack then when we go to another function main memory is pushed off the top and the memory for the new function is poped on the bottom then freed when we go to another function and all the memory used locally is freed automatically when a program terminates.

    thanks a learner(wanting to be a master).

  2. #2
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    Um...where are you learning to program in C? This way of allocating memory is really used anymore. That, and you don't have to include alloc.h, the function malloc is in stdlib.h.

    > if ((str = (char *) malloc(10)) == NULL)
    On this line, it is definitely not good practice to allocate memory in the if statement. It really should be:
    Code:
    str = (char *) malloc(10);
    if (str == NULL)
    ...etc...
    This is more readable, especially when you have to debug. Also, your allocation statement:
    > str = (char *) malloc(10);
    is right, but a little out-dated. What compiler are you using? You can allocate the memory like this:
    > str = malloc(10);
    you don't need to typecast when you allocate in the more recent compilers. You can do your way, I'm just showing you what is used today. If you are comfortable with yours, then that's fine. But you don't need to typecast when allocating.

    Anymore questions about memory allocation?

  3. #3
    Unregistered
    Guest
    > Um...where are you learning to program in C? This way of
    > allocating memory is really used anymore.

    Um....Where are _YOU_ learning to program in _C_?

    >> if ((str = (char *) malloc(10)) == NULL)
    > On this line, it is definitely not good practice to allocate memory
    > in the if statement.
    >
    > str = (char *) malloc(10);
    > if (str == NULL)

    This is _EXACTLY_ the same thing. Why wouldn't you want to do
    it in the if() statement? There is no reason not to. This is common
    practice. There is no reason not to do this.

    Quzah.

  4. #4
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    > This is _EXACTLY_ the same thing.
    Yeh, I know it is exactly the same thing. I was just making a suggestion that it is only easier to read it you separate those parts. It is much better when debugging. That's all, just easier reading. Same functionality, but better reading.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. malloc + segmentation fault
    By ch4 in forum C Programming
    Replies: 5
    Last Post: 04-07-2009, 03:46 PM
  2. 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
  3. Malloc and calloc problem!!
    By xxhimanshu in forum C Programming
    Replies: 19
    Last Post: 08-10-2005, 05:37 AM
  4. malloc and realloc
    By odysseus.lost in forum C Programming
    Replies: 3
    Last Post: 05-27-2005, 08:44 AM
  5. malloc() & address allocation
    By santechz in forum C Programming
    Replies: 6
    Last Post: 03-21-2005, 09:08 AM