Thread: pointers & malloc test code

  1. #1
    Registered User
    Join Date
    May 2011
    Location
    Alberta, Canada
    Posts
    34

    pointers & malloc test code

    Hello list ...

    I think I'm grokking it - but please critique the following code and comments therein:

    Code:
    /*
    program to demonstrate the use of pointers and
    malloc to dynamically allocate memory.
    
    Remember these rules:
    1.  Always assign an address to a pointer before using it!
    2.  Conversely, never assign a value to *some_ptr without first
         assigning an address to some_ptr!
    */
    
    #include <stdio.h>
    #include <malloc.h>
    
    //insert function prototypes below
    
    int main( void )
    {
        int  *ptr2_int;
    
    ptr2_int = (int *) malloc(sizeof(int));
    
    /* Breakdown of the above line:
    sizeof(int) => returns the number of bytes that a variable
                     of type "int" requires.
    malloc(some_number) => grabs "some_number" of consecutive
                                     bytes of the available (unused) memory
                                     on the target machine. Returns the starting
                                     address of those bytes.
    (int *) => This is "type casting". It says that the starting address
                 of the available memory will be a pointer to data that will be
                 of type "int".
    
    In short:
    
    "allocate from RAM enough space for a variable of type "int". Then assign
    the starting address of that memory to ptr2_int, which is a pointer to data
    of type "int"
    */
    
    *ptr2_int = 999;
    
        printf ("content of ptr2_int: %p\n", ptr2_int);
        printf ("value pointed to by ptr2_int: %d\n", *ptr2_int);
        return 0;
    }
    Much obliged!
    --
    Duke

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,663
    > #include <malloc.h>
    malloc is prototyped in stdlib.h
    malloc.h is an obsolete and non-standard location for finding malloc.

    Also, casting the result of malloc (in this case, with (int*) ) is unnecessary in a valid C program.
    See the FAQ.
    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.

  3. #3
    Registered User
    Join Date
    May 2011
    Location
    Alberta, Canada
    Posts
    34
    @Salem
    Your reply here might just have answered my question about using Turbo C (and it's manuals) in another thread

    Funny thing though - the code compiles under Linux (gcc) with no warnings or errors. I set up Code::Blocks to use -Wall.
    Regardless, I appreciate your comments, and will update my code.
    --
    Duke

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by dukester View Post
    @Salem
    Your reply here might just have answered my question about using Turbo C (and it's manuals) in another thread

    Funny thing though - the code compiles under Linux (gcc) with no warnings or errors. I set up Code::Blocks to use -Wall.
    Regardless, I appreciate your comments, and will update my code.
    --
    Duke
    Duke... please see my message in the other thread... Code::Blocks with MinGW is an ok package if you are doing both C and C++ work and don't need windows resource editors... otherwise...

  5. #5
    Registered User
    Join Date
    May 2011
    Location
    Alberta, Canada
    Posts
    34
    Saw you comments regarding Pelles C -- thanks!
    --
    Duke

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 05-19-2010, 02:12 AM
  2. Malloc and pointers
    By taurus in forum C Programming
    Replies: 8
    Last Post: 10-25-2008, 09:00 AM
  3. Problem with malloc and pointers to pointers
    By mike_g in forum C Programming
    Replies: 7
    Last Post: 03-29-2008, 06:03 PM
  4. malloc and pointers
    By St0rM-MaN in forum C Programming
    Replies: 14
    Last Post: 06-20-2007, 11:03 AM
  5. malloc () test
    By Unregistered in forum C Programming
    Replies: 5
    Last Post: 07-08-2002, 05:20 PM