Thread: what is diffences betweent link1.cpp and link2.cpp

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    168

    what is diffences betweent link1.cpp and link2.cpp

    link1.cpp
    Code:
    #include <stdio.h>
    //#define NULL 0
    struct student
    {
           long num;
           float score;
           struct student *next;
    };
    int main()
    {
        struct student a,b,c,*head,*p;
        a.num = 99101;
        a.score = 89.5;
        b.num = 99103;
        b.score = 85.0;
        c.num = 99105;
        c.score = 90.0;
        
        head = &a;
        a.next = &b;
        b.next = &c;
        c.next = NULL;
        
        p = head;
        do 
        {
            printf("%ld %5.1f\n",p->num,p->score);
            p = p->next;
        }while(p != NULL);
    }

    link2.cpp
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    //#define NULL 0
    struct student
    {
           long num;
           float score;
           //struct student *next;
    };
    int main()
    {
        struct student a,b,c;//*head,*p;
        
        struct student * p = (struct student *) malloc(3*sizeof(struct student));
        struct student * q = p;
        p->num = 99101;
        p->score = 89.5;
        p++;
        p->num = 99103;
        p->score = 85.0;
        p++;
        p->num = 99105;
        p->score = 90.0;
        
        int i = 0;
        do 
        {
            printf("%ld %5.1f\n",q->num,q->score);
            //p = p->next;
            q++;
            i++;
        }while(q->num != 0 && i < 3);
    }

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Nothing??

    They're both posted in the wrong forum - go to the C++ forum.

  3. #3
    Registered User
    Join Date
    Jul 2010
    Location
    Oklahoma
    Posts
    107
    Zcrself,

    You've got the right forum, but the file extensions should be '.c' rather than '.cpp' As for the difference between them, I used diff (man diff: DIFF). This is the output I received from 'diff -bc link1.cpp link2.cpp'

    Code:
    *** link1.cpp	Wed Aug 18 23:00:04 2010
    --- link2.cpp	Wed Aug 18 22:59:55 2010
    ***************
    *** 1,31 ****
      #include <stdio.h>
      //#define NULL 0
      struct student
      {
             long num;
             float score;
    !        struct student *next;
      };
      int main()
      {
    !     struct student a,b,c,*head,*p;
    !     a.num = 99101;
    !     a.score = 89.5;
    !     b.num = 99103;
    !     b.score = 85.0;
    !     c.num = 99105;
    !     c.score = 90.0;
          
    !     head = &a;
    !     a.next = &b;
    !     b.next = &c;
    !     c.next = NULL;
          
    !     p = head;
          do 
          {
    !         printf("%ld %5.1f\n",p->num,p->score);
    !         p = p->next;
    !     }while(p != NULL);
      }
      
    --- 1,34 ----
      #include <stdio.h>
    + #include <stdlib.h>
      //#define NULL 0
      struct student
      {
             long num;
             float score;
    !        //struct student *next;
      };
      int main()
      {
    !     struct student a,b,c;//*head,*p;
          
    !     struct student * p = (struct student *) malloc(3*sizeof(struct student));
    !     struct student * q = p;
    !     p->num = 99101;
    !     p->score = 89.5;
    !     p++;
    !     p->num = 99103;
    !     p->score = 85.0;
    !     p++;
    !     p->num = 99105;
    !     p->score = 90.0;
          
    !     int i = 0;
          do 
          {
    !         printf("%ld %5.1f\n",q->num,q->score);
    !         //p = p->next;
    !         q++;
    !         i++;
    !     }while(q->num != 0 && i < 3);
      }
    Is that what you meant by difference?

    Best Regards,

    New Ink -- Henry

  4. #4
    Registered User
    Join Date
    Aug 2009
    Posts
    168
    Yes, the file extensions should be '.c' rather than '.cpp'.
    My question is that which program is more quick? and why ?

  5. #5
    Registered User
    Join Date
    Jul 2010
    Location
    Oklahoma
    Posts
    107
    Zcrself,

    link1 is going to execute more quickly because the student structures are a part of the data section (of the executable) after compiling the program, but during run-time, link2 will have to solicit the operating system for the resources to store the three students -- a process which may never complete (or even better fail) if resources are scarce enough.

    Line 11 of link1 places the student structures into the memory that is occupied by the function main during execution.

    Line 14 of link2 requests the resources for the student structures.

    I think that 'lexical scoping' would produce a nice search result...an article from the Wikipedia: Scope (programming) - Wikipedia, the free encyclopedia. The resources for the students in link2 do not exist until they are received from malloc, but they are part of the lexical scope in link1.

    I think that's right, it's been a little bit since I did operating systems class. Anyone?

    Best Regards,

    New Ink -- Henry

  6. #6
    Registered User
    Join Date
    Aug 2009
    Posts
    168
    Quote Originally Posted by new_ink2001 View Post
    Zcrself,

    link1 is going to execute more quickly because the student structures are a part of the data section (of the executable) after compiling the program, but during run-time, link2 will have to solicit the operating system for the resources to store the three students -- a process which may never complete (or even better fail) if resources are scarce enough.

    Line 11 of link1 places the student structures into the memory that is occupied by the function main during execution.

    Line 14 of link2 requests the resources for the student structures.

    I think that 'lexical scoping' would produce a nice search result...an article from the Wikipedia: Scope (programming) - Wikipedia, the free encyclopedia. The resources for the students in link2 do not exist until they are received from malloc, but they are part of the lexical scope in link1.

    I think that's right, it's been a little bit since I did operating systems class. Anyone?

    Best Regards,

    New Ink -- Henry
    I have watched your post and "lexical scoping" , but I can't understand clearly.
    Can you tell me detailed by a simple example?
    thank you !

  7. #7
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Array vs Linked list?
    For some operations, linked list is faster. for some, array.

  8. #8
    Registered User
    Join Date
    Jul 2010
    Location
    Oklahoma
    Posts
    107
    Zcrself,

    I just lost another reply on this page.... The distinction is automatic vs. dynamic memory allocation. A keyword search should find more answers. As for an example:

    Code:
    #include <stdio.h>
    #include <time.h>   // for measuring time elapsed
    #include <sys/time.h>
    
    struct aggregate
    {
       char a;
       int b;
       float c;
       double d;
       
       struct aggregate *next;
    };
    
    int main()
    {
       struct aggregate arr[500];   // is allocated when this program is compiled
       
       struct aggregate *head;
       struct aggregate *lnk = NULL;
       int i;
       
       clockid_t clock;
       struct timespec now;
       struct timespec start;
       
       // setting the resolution for the clock measurements...
       getcpuclockid( getpid(), &clock );
       now.tv_sec = 0;
       now.tv_sec = 100;
       clock_setres( clock, &now );
       now.tv_sec = 0;
       clock_settime( clock, &now );
       
       // get the current time
       clock_gettime( clock, &start );
       
       // allocation from the system resources during runtime
       for( i = 0; i < 500; i++ )
       {
          if( NULL == lnk )
          {
             head = (struct aggregate*) malloc( sizeof(struct aggregate) );
             
             if( NULL == head )
             {
                fprintf( stderr, "Memory allocation error...exiting." );
                
                return 1;
             }
             lnk = head;
          }
          else
          {
             lnk->next = (struct aggregate*) malloc( sizeof(struct aggregate) );
             
             if( NULL == lnk->next )
             {
                fprintf( stderr, "Memory allocation error...exiting." );
                
                return 1;
             }
             lnk = lnk->next;         
          }
       }
       
       // now I can use the structures in the list, and must free them upon completion...
       
       lnk = head;
       
       while( NULL != lnk )
       {
          head = lnk->next;
          free( lnk );
          lnk = head;
       }
       
       clock_gettime( clock, &now );
       
       printf( "Time elapsed: %d sec. %d nanosec.\n", now.tv_sec -start.tv_sec, now.tv_nsec -start.tv_nsec );
    
       return 0;
    }
    I don't have any output for the example, because I'm not going to dig a native linux box out of a closet in order to link to the rt library. I guess I'll have to upgrade to cygwin 1.7 sometime. The command for compiling should be: "gcc -lrt link.c"

    The point I am trying to illustrate is that the memory for arr is present when the program is loaded/executed by the operating system. In contrast, the memory, for the list pointed to by head, must be requested during program execution -- a time consuming procedure, but it produces a smaller executable file. A useful feature of dynamic memory allocation, because the size of a program's input isn't always known in advance.

    Best Regards,

    New Ink -- Henry

  9. #9
    Registered User
    Join Date
    Aug 2009
    Posts
    168
    Quote Originally Posted by new_ink2001 View Post
    Zcrself,

    I just lost another reply on this page.... The distinction is automatic vs. dynamic memory allocation. A keyword search should find more answers. As for an example:

    Code:
    #include <stdio.h>
    #include <time.h>   // for measuring time elapsed
    #include <sys/time.h>
    
    struct aggregate
    {
       char a;
       int b;
       float c;
       double d;
       
       struct aggregate *next;
    };
    
    int main()
    {
       struct aggregate arr[500];   // is allocated when this program is compiled
       
       struct aggregate *head;
       struct aggregate *lnk = NULL;
       int i;
       
       clockid_t clock;
       struct timespec now;
       struct timespec start;
       
       // setting the resolution for the clock measurements...
       getcpuclockid( getpid(), &clock );
       now.tv_sec = 0;
       now.tv_sec = 100;
       clock_setres( clock, &now );
       now.tv_sec = 0;
       clock_settime( clock, &now );
       
       // get the current time
       clock_gettime( clock, &start );
       
       // allocation from the system resources during runtime
       for( i = 0; i < 500; i++ )
       {
          if( NULL == lnk )
          {
             head = (struct aggregate*) malloc( sizeof(struct aggregate) );
             
             if( NULL == head )
             {
                fprintf( stderr, "Memory allocation error...exiting." );
                
                return 1;
             }
             lnk = head;
          }
          else
          {
             lnk->next = (struct aggregate*) malloc( sizeof(struct aggregate) );
             
             if( NULL == lnk->next )
             {
                fprintf( stderr, "Memory allocation error...exiting." );
                
                return 1;
             }
             lnk = lnk->next;         
          }
       }
       
       // now I can use the structures in the list, and must free them upon completion...
       
       lnk = head;
       
       while( NULL != lnk )
       {
          head = lnk->next;
          free( lnk );
          lnk = head;
       }
       
       clock_gettime( clock, &now );
       
       printf( "Time elapsed: %d sec. %d nanosec.\n", now.tv_sec -start.tv_sec, now.tv_nsec -start.tv_nsec );
    
       return 0;
    }
    I don't have any output for the example, because I'm not going to dig a native linux box out of a closet in order to link to the rt library. I guess I'll have to upgrade to cygwin 1.7 sometime. The command for compiling should be: "gcc -lrt link.c"

    The point I am trying to illustrate is that the memory for arr is present when the program is loaded/executed by the operating system. In contrast, the memory, for the list pointed to by head, must be requested during program execution -- a time consuming procedure, but it produces a smaller executable file. A useful feature of dynamic memory allocation, because the size of a program's input isn't always known in advance.

    Best Regards,

    New Ink -- Henry
    thanks a lot! I do see

  10. #10
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    What's quicker... chopping down a one inch diameter tree using an axe, or using a chainsaw?
    It depends on whether you're counting the time taken to start the chainsaw, and whether the axe is sharp etc. Basically there is no right or wrong answer.

    The point being that if you're only doing a small amount of work, then how you do it makes no difference.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  11. #11
    Registered User
    Join Date
    Jul 2010
    Location
    Oklahoma
    Posts
    107
    By the way, after I dug out some source code for computing a dot product, and reviewed my work the function gettimeofday is more readily accessible. It is also supposed to be accurate to the micro-second. After I used a pair of struct timeval from time.h and sys/time.h, the output that I got is:

    Code:
    Time elapsed: 0 sec. 15000 microsec.
    Of course, I had to raise the number of struct agregates to 1000 in the array and the loop allocating them before the timer was updated. The print branch from main looked like:

    Code:
       // get time at completion
       if( 0 > gettimeofday( &now, NULL ) )
       {
          fprintf( stderr, "Current time of day is unavailable.\n" );
       }
       else
       {
          fprintf( stdout, "Time elapsed: %d sec. %d microsec.\n",
             now.tv_sec -start.tv_sec, now.tv_usec -start.tv_usec );
       }
    Best Regards,

    New Ink -- Henry

Popular pages Recent additions subscribe to a feed