Thread: What do these two lines mean?

  1. #1
    Registered User CompiledMonkey's Avatar
    Join Date
    Feb 2002
    Location
    Richmond, VA
    Posts
    438

    What do these two lines mean?

    I'm exploring some C and I came across this code...

    Code:
    #include <stdio.h>
    
    struct rec
    {
        int i;
        float f;
        char c;
    };
    
    int main()
    {
        struct rec *p;
        p=(struct rec *) malloc (sizeof(struct rec));
        (*p).i=10;
        (*p).f=3.14;
        (*p).c='a';
        printf("%d %f %c\n",(*p).i,(*p).f,(*p).c);
        free(p);
        return 0;
    }
    I can run it with or without those two lines. Can anybody explain what they do and why they where included?

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Well, to begin with, *without* those two lines you have a very dangerous program!

    malloc() simply returns a chunk of memory (or NULL on failure). When you're done with the memory, you need to return it to the heap with free(). Simple, right?

    Now why do we need it? Quite simply because an uninitialized pointer points to some random memory location (maybe something *really* important). You have to point it to some valid location before you can use it. Does that make sense?
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 11
    Last Post: 10-07-2008, 06:19 PM
  2. Print out first N lines
    By YoYayYo in forum C Programming
    Replies: 1
    Last Post: 02-21-2008, 12:58 AM
  3. Finding number of lines of code
    By arron in forum Linux Programming
    Replies: 8
    Last Post: 01-06-2006, 05:35 AM
  4. Reading lines from a file
    By blackswan in forum C Programming
    Replies: 9
    Last Post: 04-26-2005, 04:29 PM
  5. count only lines of code...
    By flightsimdude in forum C Programming
    Replies: 13
    Last Post: 09-23-2003, 07:08 PM