printing values of "arrays of pointers"

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

    printing values of "arrays of pointers"

    PURPOSE: see the comments.
    WHAT I GOT: infinite loop



    /* This program will simply create an array of pointers to integers
    * and will fill it with some values while using malloc to create
    * pointers to fill the array and then will print the values pointed
    * by those pointers
    *
    */



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

    enum MAXSIZE { ARRSIZE = 100 };


    int main( void )
    {
    int* arr_p[ARRSIZE];
    int** pp;
    int *mp, *np;
    int i;

    int null_int = 0;

    pp = arr_p;
    np = &null_int;


    for( i = 0; i < ARRSIZE - 1; ++i )
    {
    mp = malloc( sizeof( int ) );
    mp = &i;
    arr_p[i] = mp;
    }


    arr_p[i] = np;

    while( **pp )
    {
    printf("**pp = %d\n", **pp);
    }

    return 0;
    }





    --

    my email ID is @ the above address

  • Richard Heathfield

    #2
    Re: printing values of &quot;arrays of pointers&quot;

    arnuld said:
    PURPOSE: see the comments.
    WHAT I GOT: infinite loop
    >
    >
    >
    /* This program will simply create an array of pointers to integers
    * and will fill it with some values while using malloc to create
    * pointers to fill the array and then will print the values pointed
    * by those pointers
    *
    */
    >
    >
    >
    #include <stdio.h>
    #include <stdlib.h>
    >
    enum MAXSIZE { ARRSIZE = 100 };
    >
    >
    int main( void )
    {
    int* arr_p[ARRSIZE];
    int** pp;
    int *mp, *np;
    int i;
    >
    int null_int = 0;
    >
    pp = arr_p;
    np = &null_int;
    >
    >
    for( i = 0; i < ARRSIZE - 1; ++i )
    {
    mp = malloc( sizeof( int ) );
    You don't check to see whether this fails to allocate the requested
    memory...
    mp = &i;
    ....which doesn't really matter, since you throw it away here.
    arr_p[i] = mp;
    }
    Well done. You just filled arr_p with a bunch of pointers to i. Why bother
    with the malloc call, then? You could save yourself (ARRSIZE-1) *
    sizeof(int) bytes of memory leak by removing the malloc call completely.
    arr_p[i] = np;
    >
    while( **pp )
    pp has the value &arr_p[0], so **pp attempts to find the int value at
    *arr_p[0]. Since arr_p[0] points to i, we're looking for the value of i.
    That value is indeterminate, because the object i never had a value
    assigned to it. Thus, the behaviour is undefined. This loop could execute
    forever (the most likely possibility) or not at all (probably the second
    most likely) or something completely weird could happen.
    {
    printf("**pp = %d\n", **pp);
    }
    You don't even /try/ to iterate through the array, do you? You might want
    to take a long hard look at Visual Basic or RCX.

    --
    Richard Heathfield <http://www.cpax.org.uk >
    Email: -http://www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999

    Comment

    • arnuld

      #3
      Re: printing values of &quot;arrays of pointers&quot;

      On Fri, 02 May 2008 13:44:50 +0000, Richard Heathfield wrote:

      > for( i = 0; i < ARRSIZE - 1; ++i )
      > {
      > mp = malloc( sizeof( int ) );
      You don't check to see whether this fails to allocate the requested
      memory...
      done

      > mp = &i;
      ...which doesn't really matter, since you throw it away here.

      well, then how can I generate a new variable every-time I enter into the
      loop ?


      Well done. You just filled arr_p with a bunch of pointers to i. Why bother
      with the malloc call, then? You could save yourself (ARRSIZE-1) *
      sizeof(int) bytes of memory leak by removing the malloc call completely.
      I can but I did not because I want to understand the how pointers to
      arrays to pointers behave and learn them.


      You don't even /try/ to iterate through the array, do you?
      okay, here is a little fixed version. I can't do anything about variable i
      in for loop as I said, I can't find a way to generate new variables at
      every iteration through the loop.

      You might want to take a long hard look at Visual Basic





      or RCX.
      never heard of it


      --

      my email ID is @ the above address

      Comment

      • Bartc

        #4
        Re: printing values of &quot;arrays of pointers&quot;


        "arnuld" <NoSpam@microso ft.comwrote in message
        news:pan.2008.0 5.02.13.37.10.5 70440@microsoft .com...
        PURPOSE: see the comments.
        WHAT I GOT: infinite loop
        >
        I've modified your code so that it behaves better:


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

        enum MAXSIZE { ARRSIZE = 100 };


        int main( void )
        {
        int *arr_p[ARRSIZE];
        int **pp;
        int *np;
        int i;

        int null_int = 0;

        pp = arr_p;
        np = &null_int;


        for( i = 0; i < ARRSIZE - 1; ++i )
        {
        arr_p[i] = malloc( sizeof( int ) );
        *arr_p[i] = 1000+i; /* initial the ints to some recognisable
        (and non-0!) value */
        }


        arr_p[i] = np; /* sentinel (*np contains 0) */

        while( **pp) /* Step through array using pp */
        {
        printf("**pp = %d\n", **pp);
        ++pp;
        }

        return 0;
        }

        -- Bartc


        Comment

        • Ben Bacarisse

          #5
          Re: printing values of &quot;arrays of pointers&quot;

          [BTW, there seems to be somthing odd in the way you newsreader add
          attribution lines. I've fixed it, I think.]

          arnuld <NoSpam@microso ft.comwrites:
          On Fri, 02 May 2008 13:44:50 +0000, Richard Heathfield wrote:
          >
          >> for( i = 0; i < ARRSIZE - 1; ++i )
          >> {
          >> mp = malloc( sizeof( int ) );
          >
          >You don't check to see whether this fails to allocate the requested
          >memory...
          >
          done
          >
          >> mp = &i;
          >
          >...which doesn't really matter, since you throw it away here.
          >
          well, then how can I generate a new variable every-time I enter into the
          loop ?
          That is what malloc is for. C calls them objects, and you make them
          exactly as you have written it. The error is overwriting the pointer
          you get from malloc with one to i. Pointers to local variables like
          i (automatic storage in C terms) are very rarely used.

          --
          Ben.

          Comment

          • pete

            #6
            Re: printing values of &quot;arrays of pointers&quot;

            arnuld wrote:
            PURPOSE: see the comments.
            WHAT I GOT: infinite loop
            >
            >
            >
            /* This program will simply create an array of pointers to integers
            * and will fill it with some values while using malloc to create
            * pointers to fill the array and then will print the values pointed
            * by those pointers
            *
            */
            >
            >
            >
            #include <stdio.h>
            #include <stdlib.h>
            >
            enum MAXSIZE { ARRSIZE = 100 };
            >
            >
            int main( void )
            {
            int* arr_p[ARRSIZE];
            int** pp;
            int *mp, *np;
            int i;
            >
            int null_int = 0;
            >
            pp = arr_p;
            np = &null_int;
            >
            >
            for( i = 0; i < ARRSIZE - 1; ++i )
            {
            mp = malloc( sizeof( int ) );
            mp = &i;
            arr_p[i] = mp;
            }
            >
            >
            arr_p[i] = np;
            >
            while( **pp )
            {
            printf("**pp = %d\n", **pp);
            }
            >
            return 0;
            }
            /* BEGIN new.c */
            /*
            ** This program will simply create an array of pointers to integers
            ** and will fill it with some values while using malloc to create
            ** pointers to fill the array and then will print the values pointed
            ** by those pointers
            */
            #include <stdio.h>
            #include <stdlib.h>

            #define MAXSIZE 100

            void free_ptrs(int **p, size_t n);

            int main(void)
            {
            int i;
            int *arr_p[MAXSIZE];
            int **pp;

            puts("/* BEGIN new.c output */\n");
            for (i = 0; i < MAXSIZE - 1; ++i ) {
            arr_p[i] = malloc(sizeof *arr_p[i]);
            if (arr_p[i] == NULL) {
            printf("arr_p[%d] == NULL\n", i);
            break;
            }
            *arr_p[i] = i;
            }
            arr_p[i] = NULL;
            for (pp = arr_p; *pp != NULL; ++pp) {
            printf("**pp = %d\n", **pp);
            }
            free_ptrs(arr_p , i);
            puts("\n/* END new.c output */");
            return 0;
            }

            void free_ptrs(int **p, size_t n)
            {
            while (n-- != 0) {
            free(*p);
            *p++ = NULL;
            }
            }

            /* END new.c */

            --
            pete

            Comment

            • Bartc

              #7
              Re: printing values of &quot;arrays of pointers&quot;


              "santosh" <santosh.k83@gm ail.comwrote in message
              news:fvh64c$mht $1@registered.m otzarella.org.. .
              arnuld wrote:
              >
              Path: news.motzarella .org!motzarella .org!newsfeed.s traub-nv.de!
              goblin1!goblin2 !goblin.stu.nev a.ru!news.net.u ni-c.dk!dotsrc.org !
              filter.dotsrc.o rg!news.dotsrc. org!not-for-mail
              From: arnuld <NoSpam@microso ft.com>
              You have no legal right to use that email address unless you are an
              employee of Microsoft and have received their permission to do so.
              Don't know the OP's newsreader but: if you open a news account with Outlook
              Express, it does ask:

              "Email address: ..... for example: someone@microso ft.com"

              Could someone really be sued for following this advice too literally?

              -- Bartc



              Comment

              • Richard Tobin

                #8
                Re: printing values of &quot;arrays of pointers&quot;

                In article <fvh64c$mht$1@r egistered.motza rella.org>,
                santosh <santosh.k83@gm ail.comwrote:
                >You have no legal right to use that email address unless you are an
                >employee of Microsoft and have received their permission to do so.
                Do you have any basis for this claim? I don't recall any laws being
                passed on the subject. Trademark law isn't relevant, since he's not
                engaged in trade. He is clearly not using their name for any
                fraudulent purpose. He's not misrepresenting himself (even if that
                were illegal) since no-one would take it as a real email address.
                RFCs have no legal force.

                Nothing stops me from going around wearing a badge saying "Bill Gates,
                Microsoft", unless I do it for some illegal purpose. Why should a
                header in a news posting be any different?

                At most he might be violating his ISP's terms and conditions.

                -- Richard
                --
                :wq

                Comment

                • santosh

                  #9
                  Re: printing values of &quot;arrays of pointers&quot;

                  Richard Tobin wrote:
                  In article <fvh64c$mht$1@r egistered.motza rella.org>,
                  santosh <santosh.k83@gm ail.comwrote:
                  >
                  >>You have no legal right to use that email address unless you are an
                  >>employee of Microsoft and have received their permission to do so.
                  >
                  Do you have any basis for this claim? I don't recall any laws being
                  passed on the subject. Trademark law isn't relevant, since he's not
                  engaged in trade. He is clearly not using their name for any
                  fraudulent purpose. He's not misrepresenting himself (even if that
                  were illegal) since no-one would take it as a real email address.
                  RFCs have no legal force.
                  >
                  Nothing stops me from going around wearing a badge saying "Bill Gates,
                  Microsoft", unless I do it for some illegal purpose. Why should a
                  header in a news posting be any different?
                  >
                  At most he might be violating his ISP's terms and conditions.
                  I don't know about other countries, but I believe we have rules in our
                  so-called "Cyber Laws" that prohibit willfull impersonation and
                  misrepresentati on. This would be particularly applicable if arnuld were
                  to comment upon Microsoft under this false email address. And why would
                  no one take it as a real email address? It's a perfectly valid domain
                  and hostname, though the latter may not exist. It's not a proper munged
                  address, nor an invalid address.

                  More practically Sunsite could terminate his account with them if they
                  were to be alerted to his behaviour. Their rules clearly say that users
                  are only to use their own email addresses which actually exist and can
                  be delivered to, in the Sender field.

                  Comment

                  • Flash Gordon

                    #10
                    Re: printing values of &quot;arrays of pointers&quot;

                    santosh wrote, On 03/05/08 11:51:
                    Richard Tobin wrote:
                    >
                    >In article <fvh64c$mht$1@r egistered.motza rella.org>,
                    >santosh <santosh.k83@gm ail.comwrote:
                    >>
                    >>You have no legal right to use that email address unless you are an
                    >>employee of Microsoft and have received their permission to do so.
                    >Do you have any basis for this claim? I don't recall any laws being
                    >passed on the subject. Trademark law isn't relevant, since he's not
                    <snip>
                    I don't know about other countries, but I believe we have rules in our
                    so-called "Cyber Laws" that prohibit willfull impersonation and
                    misrepresentati on. This would be particularly applicable if arnuld were
                    to comment upon Microsoft under this false email address. And why would
                    no one take it as a real email address? It's a perfectly valid domain
                    and hostname, though the latter may not exist. It's not a proper munged
                    address, nor an invalid address.
                    Also some of us (well, me anyway) deliberately use addresses in our
                    domains that look like spamtraps. I've found that spam@ gets spammed
                    less than other addresses I've used.
                    More practically Sunsite could terminate his account with them if they
                    were to be alerted to his behaviour. Their rules clearly say that users
                    are only to use their own email addresses which actually exist and can
                    be delivered to, in the Sender field.
                    Ah well, I'm sure at some point someone will decide to report him to
                    them if he continues.
                    --
                    Flash Gordon

                    Comment

                    • arnuld

                      #11
                      Re: printing values of &quot;arrays of pointers&quot;

                      On Sat, 03 May 2008 16:21:35 +0530, santosh wrote:

                      More practically Sunsite could terminate his account with them if they
                      were to be alerted to his behaviour. Their rules clearly say that users
                      are only to use their own email addresses which actually exist and can
                      be delivered to, in the Sender field.

                      If I use my real email ID that means I am inviting 100% of spam. I don't
                      want to be spammed and no one wants to be spammed.

                      Regarding microsoft.com, I got that idea from my friend. He told me that
                      when he is using Outlook Express on WindowsXP, he gets this:
                      ....@microsoft. com, as default address.



                      --

                      my email ID is @ the above address

                      Comment

                      • arnuld

                        #12
                        Re: printing values of &quot;arrays of pointers&quot;

                        On Sat, 03 May 2008 16:21:35 +0530, santosh wrote:

                        More practically Sunsite could terminate his account with them if they
                        were to be alerted to his behaviour. Their rules clearly say that users
                        are only to use their own email addresses which actually exist and can
                        be delivered to, in the Sender field.

                        I think this Server Policy is good:





                        --

                        my email ID is @ the above address

                        Comment

                        • arnuld

                          #13
                          Re: printing values of &quot;arrays of pointers&quot;

                          On Sat, 03 May 2008 16:21:35 +0530, santosh wrote:

                          More practically Sunsite could terminate his account with them if they
                          were to be alerted to his behaviour. Their rules clearly say that users
                          are only to use their own email addresses which actually exist and can
                          be delivered to, in the Sender field.

                          I have mailed them and have asked them to change their policy.





                          --

                          my email ID is @ the above address

                          Comment

                          • Richard Heathfield

                            #14
                            Re: printing values of &quot;arrays of pointers&quot;

                            arnuld said:
                            >On Sat, 03 May 2008 16:21:35 +0530, santosh wrote:
                            >
                            >
                            >More practically Sunsite could terminate his account with them if they
                            >were to be alerted to his behaviour. Their rules clearly say that users
                            >are only to use their own email addresses which actually exist and can
                            >be delivered to, in the Sender field.
                            >
                            >
                            If I use my real email ID that means I am inviting 100% of spam. I don't
                            want to be spammed and no one wants to be spammed.
                            Nobody wants to be spammed. On the other hand, how would you like it if
                            someone used *your* real address in *their* From field?

                            All you have to do is invent an address that is in the correct form but
                            guaranteed not to exist. For example, spam@spam.invalid is a well-formed
                            email address that definitely doesn't exist (because there is no top-level
                            domain called "invalid"). This is not an area where originality is
                            terribly important. If in doubt, use spam@spam.invalid for From and
                            Reply-To, and mung your real email address in your sig block.

                            --
                            Richard Heathfield <http://www.cpax.org.uk >
                            Email: -http://www. +rjh@
                            Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
                            "Usenet is a strange place" - dmr 29 July 1999

                            Comment

                            • arnuld

                              #15
                              Re: printing values of &quot;arrays of pointers&quot;

                              On Mon, 05 May 2008 04:48:11 +0000, Richard Heathfield wrote:
                              Nobody wants to be spammed. On the other hand, how would you like it if
                              someone used *your* real address in *their* From field?
                              :(

                              All you have to do is invent an address that is in the correct form but
                              guaranteed not to exist. For example, spam@spam.invalid is a well-formed
                              email address that definitely doesn't exist (because there is no top-level
                              domain called "invalid"). This is not an area where originality is
                              terribly important. If in doubt, use spam@spam.invalid for From and
                              Reply-To, and mung your real email address in your sig block.

                              how about my present address ? It doe not use any real domain name and is
                              different enough from common words like spam, invalid, nospam etc.

                              BTW, I don't see Reply-To header for my own posts, not even for the posts
                              of BartC and santosh but I do see Reply-To header of yours. ( I have
                              already enabled all headers in PAN preferences)



                              --

                              my email ID is @ the above blog.
                              just check the "About Myself" page :)

                              Comment

                              Working...