pointers

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • RoSsIaCrIiLoIA

    #1

    pointers

    If sizeof(unsigned )=4 is this allowed?

    #include <stdio.h>
    #include <stdlib.h>

    int main(void)
    {unsigned *i;
    i = malloc(1);
    return 0;
    }

    and this

    int main(void)
    {unsigned *i;
    i = malloc(1);
    *i=98;
    return 0;
    }
  • Emmanuel Delahaye

    #2
    Re: pointers

    RoSsIaCrIiLoIA wrote on 28/07/04 :[color=blue]
    > If sizeof(unsigned )=4 is this allowed?
    >
    > #include <stdio.h>
    > #include <stdlib.h>
    >
    > int main(void)
    > {unsigned *i;
    > i = malloc(1);[/color]

    Yes, as far as you don't use 'i'. (Is it useful is another story).
    [color=blue]
    > return 0;
    > }
    >
    > and this
    >
    > int main(void)
    > {unsigned *i;
    > i = malloc(1);
    > *i=98;[/color]

    No. Using 'i' invokes an undefined behaviour.
    [color=blue]
    > return 0;
    > }[/color]


    Two more things : malloc() could fail, and you are supposed to free the
    allocated memory...

    --
    Emmanuel
    The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html

    "C is a sharp tool"

    Comment

    • Fred L. Kleinschmidt

      #3
      Re: pointers



      RoSsIaCrIiLoIA wrote:[color=blue]
      >
      > If sizeof(unsigned )=4 is this allowed?
      >
      > #include <stdio.h>
      > #include <stdlib.h>
      >
      > int main(void)
      > {unsigned *i;
      > i = malloc(1);
      > return 0;
      > }
      >
      > and this
      >
      > int main(void)
      > {unsigned *i;
      > i = malloc(1);
      > *i=98;
      > return 0;
      > }[/color]

      Why bother? Why not just use
      i = malloc(sizeof(* i));
      This is the same speed and much more useful - it works on all platforms,
      i.e., when sizeof(unsigned ) is 4 or 8 or anything else, and you can
      change the type later and not worry about haviong to change all of the
      malloc's.


      --
      Fred L. Kleinschmidt
      Boeing Associate Technical Fellow
      Technical Architect, Common User Interface Services
      M/S 2R-94 (206)544-5225

      Comment

      • Keith Thompson

        #4
        Re: pointers

        Emmanuel Delahaye <emdel@YOURBRAn oos.fr> writes:[color=blue]
        > RoSsIaCrIiLoIA wrote on 28/07/04 :[color=green]
        > > If sizeof(unsigned )=4 is this allowed?
        > >
        > > #include <stdio.h>
        > > #include <stdlib.h>
        > >
        > > int main(void)
        > > {unsigned *i;
        > > i = malloc(1);[/color]
        >
        > Yes, as far as you don't use 'i'. (Is it useful is another story).
        >[color=green]
        > > return 0;
        > > }
        > >
        > > and this int main(void)
        > > {unsigned *i;
        > > i = malloc(1);
        > > *i=98;[/color]
        >
        > No. Using 'i' invokes an undefined behaviour.[/color]
        [...]

        Using 'i' (e.g., by comparing it to NULL) should be ok; using *i, as
        the quoted code fragment does, invokes undefined behavior.

        --
        Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
        San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
        We must do something. This is something. Therefore, we must do this.

        Comment

        • RoSsIaCrIiLoIA

          #5
          Re: pointers

          On Wed, 28 Jul 2004 15:22:56 GMT, RoSsIaCrIiLoIA <n@esiste.ee> wrote:
          I forget free()[color=blue]
          >If sizeof(unsigned )=4 is this allowed?
          >
          >#include <stdio.h>
          >#include <stdlib.h>
          >
          >int main(void)
          >{unsigned *i;
          > i = malloc(1);[/color]
          free(i);[color=blue]
          > return 0;
          >}
          >
          >and this
          >
          >int main(void)
          >{unsigned *i;
          > i = malloc(1);
          > *i=98;[/color]
          free(i);[color=blue]
          > return 0;
          >}[/color]

          Comment

          Working...