Would I maybe use register in this case?

Discussion in 'C Programming' started by Chad, Sep 3, 2007.

  1. Chad

    Chad Guest

    I'm not too sure if this is the right forum to ask the following
    question.

    Given the following code

    #include <stdio.h>

    int main(void)
    {
    int a=5;
    int b=6;

    int c, i;

    for(i=0; i< 30; i++) {
    c = a * b;
    }

    return 0;
    }

    Now let's assume the values of a and b never change. Each time the
    'for' loop executes in the above code, a and b gets multiplied. If I
    would change the value of c to register, would the values of and b get
    cached? Ie not get re-loaded into the memory everytime the for loop
    gets executed.

    Chad
     
    Chad, Sep 3, 2007
    #1
    1. Advertising

  2. Chad <> writes:

    > I'm not too sure if this is the right forum to ask the following
    > question.
    >
    > Given the following code
    >
    > #include <stdio.h>
    >
    > int main(void)
    > {
    > int a=5;
    > int b=6;
    >
    > int c, i;
    >
    > for(i=0; i< 30; i++) {
    > c = a * b;
    > }
    >
    > return 0;
    > }
    >
    > Now let's assume the values of a and b never change. Each time the
    > 'for' loop executes in the above code, a and b gets multiplied. If I
    > would change the value of c to register, would the values of and b get
    > cached? Ie not get re-loaded into the memory everytime the for loop
    > gets executed.


    You are begging the question. A good compiler will generally do
    better register allocation if you stay out of its hair (hence the
    register keyword is sometimes ignored by compilers and rarely used by
    programmers).

    Regardless of the storage class of c, a good compiler will do much
    more to above than 'cache' a and b -- it will remove the loop entirely
    and may well remove the whole program since it can prove that setting
    c makes no difference to anyone!

    --
    Ben.
     
    Ben Bacarisse, Sep 3, 2007
    #2
    1. Advertising

  3. Chad

    Chad Guest

    On Sep 2, 5:34 pm, Ben Bacarisse <> wrote:
    > Chad <> writes:
    > > I'm not too sure if this is the right forum to ask the following
    > > question.

    >
    > > Given the following code

    >
    > > #include <stdio.h>

    >
    > > int main(void)
    > > {
    > > int a=5;
    > > int b=6;

    >
    > > int c, i;

    >
    > > for(i=0; i< 30; i++) {
    > > c = a * b;
    > > }

    >
    > > return 0;
    > > }

    >
    > > Now let's assume the values of a and b never change. Each time the
    > > 'for' loop executes in the above code, a and b gets multiplied. If I
    > > would change the value of c to register, would the values of and b get
    > > cached? Ie not get re-loaded into the memory everytime the for loop
    > > gets executed.

    >
    > You are begging the question. A good compiler will generally do
    > better register allocation if you stay out of its hair (hence the
    > register keyword is sometimes ignored by compilers and rarely used by
    > programmers).
    >
    > Regardless of the storage class of c, a good compiler will do much
    > more to above than 'cache' a and b -- it will remove the loop entirely
    > and may well remove the whole program since it can prove that setting
    > c makes no difference to anyone!
    >
    > --
    > Ben.


    Thank you for taking the time to clarify my apparent brain fart.
     
    Chad, Sep 3, 2007
    #3
  4. On Sun, 02 Sep 2007 17:01:11 -0700, Chad <> wrote:

    >I'm not too sure if this is the right forum to ask the following
    >question.
    >
    >Given the following code
    >
    >#include <stdio.h>
    >
    >int main(void)
    >{
    > int a=5;
    > int b=6;
    >
    > int c, i;
    >
    > for(i=0; i< 30; i++) {
    > c = a * b;
    > }
    >
    > return 0;
    >}
    >
    >Now let's assume the values of a and b never change. Each time the
    >'for' loop executes in the above code, a and b gets multiplied. If I
    >would change the value of c to register, would the values of and b get
    >cached? Ie not get re-loaded into the memory everytime the for loop
    >gets executed.


    The C language doesn't know from cache. Whether a value is cached is
    a question usually beyond the compiler/linker/run time library, the
    things we usually discuss here.

    Does the hardware have cache? How much? Does the operating system
    support it efficiently? What else is running on the system at the
    time this code is running? Is the code generated by the compiler
    "cache-friendly"?

    None of these questions are affected by presence or absence of
    "register". It is entirely possible that adding register would cause
    data that would normally be cached during execution to not be.

    By the way, most modern compilers with a decent optimizer will ignore
    register when you specify it.


    Remove del for email
     
    Barry Schwarz, Sep 3, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. flekso
    Replies:
    1
    Views:
    339
    vijay
    Jul 7, 2003
  2. Guest
    Replies:
    5
    Views:
    632
  3. Ville Vainio
    Replies:
    11
    Views:
    627
    Hamish Lawson
    Aug 10, 2004
  4. ibrahima

    yes I would like register

    ibrahima, Jun 1, 2007, in forum: Python
    Replies:
    0
    Views:
    289
    ibrahima
    Jun 1, 2007
  5. Giovanni Gherdovich
    Replies:
    7
    Views:
    409
    rustom
    Jun 2, 2009
Loading...

Share This Page