malloc() are my comments correct

This is a discussion on malloc() are my comments correct within the C Programming forums, part of the General Programming Boards category; /* First of all am I right in thinking that malloc(10) in the program */ /* below could be malloc(6) ...

  1. #1
    Unregistered
    Guest

    malloc() are my comments correct

    /* First of all am I right in thinking that malloc(10) in the program */
    /* below could be malloc(6) hello + '\0' = 6 and if so whats the */
    /* reason for malloc(10), secondly could I have something like */
    /* malloc(amount) if I declared a variable int amount; then did */
    /* printf("Input amount of numbers to be stored"); */
    /* scanf("%d",&amount); */

    /* Finally this memory has to be freed before the program terminates */
    /* to avoid a memory leak, what is and what does a memory leak do */


    #include <stdio.h>
    #include <string.h>
    #include <alloc.h>
    #include <process.h>
    #include<conio.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);

    return 0;
    }

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145

    reply

    1)
    Yes, 5 letters + 1 NULL terminator. malloc(6) should be enough.

    2)
    malloc(sizeofstring) is ok. This is one of the great advantages of dynamic memory allocation. You can't make this: char String[size];

    3)
    A memory leak is if you exit the program without freeing the memory, or if you tell the pointer to point at something else without freeing that memory first. That memory will then remain allocated with no chance of deallocating it.
    Good structuration in your program is the best way to prevent leaks.

    Hope this helps
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >str = (char *) malloc(10)
    In ISO standard C you shouldn't need to cast malloc, in fact if you do it could hide other errors in your program.
    My best code is written with the delete key.

  4. #4
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    Really? I thought malloc() by definition returned a void *.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  5. #5
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    I took a look at the standard and noticed that malloc does return void *. But in the examples, no casting is done.

    A comment to the code:

    Code:
        /* allocate memory for string */ 
        if ((str = (char *) malloc (10)) == NULL) 
        { 
            printf("Not enough memory to allocate buffer\n"); 
            
            #ifdef SHIRO_CODE
            /* I would use a return instead of exit, mainly since
                main is of type int, so it should return an int when
                exiting. This int can be an errorvalue. */
            return ERROR_VALUE;
            #else
            exit(1); /* terminate program if out of memory */ 
            #endif
        }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. removing comments of type '//' and '/*'
    By rohit83.ken in forum C++ Programming
    Replies: 3
    Last Post: 10-20-2007, 03:24 AM
  2. Random number + guessing game trouble
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-08-2007, 04:33 AM
  3. Alternative to malloc
    By stellastarr in forum C Programming
    Replies: 13
    Last Post: 04-30-2007, 05:10 PM
  4. quick malloc question
    By thedoofus in forum C Programming
    Replies: 2
    Last Post: 10-15-2005, 06:41 AM
  5. Correct usage of malloc()
    By cboard_member in forum C++ Programming
    Replies: 9
    Last Post: 07-24-2005, 07:28 AM

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