memset o/p not correct

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • aarklon@gmail.com

    memset o/p not correct

    Hi,

    the following is actually a part of the pattern matching program which
    i tried ,memset is not setting the entire integer array with
    -1

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

    int main(void)
    {

    int maxpat[80];
    memset(maxpat,-1,80);

    for(i=0;i<80;i+ +)
    printf("%d\t",m axpat[i]);

    return EXIT_SUCCESS;
    }

    but i am getting o/p as

    -1 -1 -1 -1 -1 -1 -1 -1
    -1 -1
    -1 -1 -1 -1 -1 -1 -1 -1
    -1 -1
    -370086 -370086 -370086 -370086 -370086 -370086 -370086 -370086
    -370086 -370086
    -370086 -370086 -370086 -370086 -370086 -370086 -370086 -370086
    -370086 -370086
    -370086 -370086 -370086 -370086 -370086 -370086 -370086 -370086
    -370086 -370086
    -370086 -370086 -370086 -370086 -370086 -370086 -370086 -370086
    -370086 -370086
    -370086 -370086 -370086 -370086 -370086 -370086 -370086 -370086
    -370086 -370086
    -370086 -370086 -370086 -370086 -370086 -370086 -370086 -370086
    -370086 -370086

    "D:\siju\lcc6\n oisedisp.exe"
    Return code 0
    Execution time 0.048 seconds
    Press any key to continue...

    why is this so..????
  • aarklon@gmail.com

    #2
    Re: memset o/p not correct

    sorry I forgot to declare i , but still the o/p is same

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


    int main(void)
    {


    int maxpat[80],i;
    memset(maxpat,-1,80);


    for(i=0;i<80;i+ +)
    printf("%d\t",m axpat[i]);


    return EXIT_SUCCESS;
    }


    Comment

    • aarklon@gmail.com

      #3
      Re: memset o/p not correct

      On Apr 20, 2:28 am, aark...@gmail.c om wrote:
      Hi,
      >
      the following is actually a part of the pattern matching program which
      i tried ,memset is not setting the entire integer array with
      -1
      >
       #include <string.h>
       #include <stdio.h>
       #include <stdlib.h>
      >
       int main(void)
       {
      >
         int maxpat[80];
         memset(maxpat,-1,80);
      >
         for(i=0;i<80;i+ +)
         printf("%d\t",m axpat[i]);
      >
         return EXIT_SUCCESS;
       }
      >
      but i am getting o/p as
      >
      -1      -1      -1      -1      -1      -1      -1      -1
      -1      -1
      -1      -1      -1      -1      -1      -1      -1      -1
      -1      -1
      -370086 -370086 -370086 -370086 -370086 -370086 -370086 -370086
      -370086 -370086
      -370086 -370086 -370086 -370086 -370086 -370086 -370086 -370086
      -370086 -370086
      -370086 -370086 -370086 -370086 -370086 -370086 -370086 -370086
      -370086 -370086
      -370086 -370086 -370086 -370086 -370086 -370086 -370086 -370086
      -370086 -370086
      -370086 -370086 -370086 -370086 -370086 -370086 -370086 -370086
      -370086 -370086
      -370086 -370086 -370086 -370086 -370086 -370086 -370086 -370086
      -370086 -370086
      >
      "D:\siju\lcc6\n oisedisp.exe"
      Return code 0
      Execution time 0.048 seconds
      Press any key to continue...
      >
      why is this so..????
      sorry I forgot to declare i, but still the o/p is same

      Comment

      • Ian Collins

        #4
        Re: memset o/p not correct

        aarklon@gmail.c om wrote:
        Hi,
        >
        the following is actually a part of the pattern matching program which
        i tried ,memset is not setting the entire integer array with
        -1
        >
        #include <string.h>
        #include <stdio.h>
        #include <stdlib.h>
        >
        int main(void)
        {
        >
        int maxpat[80];
        memset(maxpat,-1,80);
        >
        You are only setting the first 80 bytes of the the array, not 80 ints.

        --
        Ian Collins.

        Comment

        • Bartc

          #5
          Re: memset o/p not correct


          <aarklon@gmail. comwrote in message
          news:c7633ee9-954f-4ad8-a0c0-646fbe8e8e81@b5 g2000pri.google groups.com...
          Hi,
          >
          the following is actually a part of the pattern matching program which
          i tried ,memset is not setting the entire integer array with
          -1
          >
          #include <string.h>
          #include <stdio.h>
          #include <stdlib.h>
          >
          int main(void)
          {
          >
          int maxpat[80];
          memset(maxpat,-1,80);
          You're setting 80 bytes. The array is bigger than that (80*sizeof(int) ).
          Try:

          memset(maxpat,-1,sizeof maxpat);

          --
          Bart



          Comment

          • Flash Gordon

            #6
            Re: memset o/p not correct

            aarklon@gmail.c om wrote, On 19/04/08 22:31:
            sorry I forgot to declare i , but still the o/p is same
            >
            #include <string.h>
            #include <stdio.h>
            #include <stdlib.h>
            >
            int main(void)
            {
            int maxpat[80],i;
            memset(maxpat,-1,80);
            <snip>

            Check the definition of memset in your reference book, it sets a
            specified number of *bytes*. This also means that having corrected the
            error (sizeof *maxpat is your friend) it won't be portable to
            implementations using 1s complement or sign-magnitude if you can find
            such an implementation.
            --
            Flash Gordon

            Comment

            • Default User

              #7
              Re: memset o/p not correct

              aarklon@gmail.c om wrote:
              Hi,
              >
              the following is actually a part of the pattern matching program which
              i tried ,memset is not setting the entire integer array with
              -1
              int maxpat[80];
              memset(maxpat,-1,80);
              As the others have pointed out, it sets each byte to the value. There's
              almost no way to do what you want with memset(), unless you happen to
              be on a machine that had one-byte ints.

              To set each int in the array to -1, you need a loop. Better yet,
              explain WHY you want to do this. There may be a better construct.



              Brian

              Comment

              • Richard Tobin

                #8
                Re: memset o/p not correct

                In article <66vedbF2m6h5lU 1@mid.individua l.net>,
                Default User <defaultuserbr@ yahoo.comwrote:
                > int maxpat[80];
                > memset(maxpat,-1,80);
                >As the others have pointed out, it sets each byte to the value. There's
                >almost no way to do what you want with memset(), unless you happen to
                >be on a machine that had one-byte ints.
                Unless you really on the machines being 2s-complement, which is true
                of all general-purpose computers today.

                -- Richard
                --
                :wq

                Comment

                • Richard

                  #9
                  Re: memset o/p not correct

                  "Default User" <defaultuserbr@ yahoo.comwrites :
                  aarklon@gmail.c om wrote:
                  >
                  >Hi,
                  >>
                  >the following is actually a part of the pattern matching program which
                  >i tried ,memset is not setting the entire integer array with
                  >-1
                  >
                  > int maxpat[80];
                  > memset(maxpat,-1,80);
                  >
                  As the others have pointed out, it sets each byte to the value. There's
                  almost no way to do what you want with memset(), unless you happen to
                  be on a machine that had one-byte ints.
                  >
                  To set each int in the array to -1, you need a loop. Better yet,
                  explain WHY you want to do this. There may be a better construct.
                  I would be interested to hear how you knowing WHY he needs to set a
                  sequence of ints to a single value would in any way "optimise" your
                  advice.

                  >
                  >
                  >
                  Brian

                  Comment

                  • CBFalconer

                    #10
                    Re: memset o/p not correct

                    Richard Tobin wrote:
                    Default User <defaultuserbr@ yahoo.comwrote:
                    >
                    >> int maxpat[80];
                    >> memset(maxpat,-1,80);
                    >
                    >As the others have pointed out, it sets each byte to the value.
                    >There's almost no way to do what you want with memset(), unless
                    >you happen to be on a machine that had one-byte ints.
                    >
                    Unless you really on the machines being 2s-complement, which is
                    true of all general-purpose computers today.
                    What's the point of the foolishness, when:

                    #define MPSIZE 80
                    #define VALUE -1
                    int maxpat[MPSIZE], i;

                    for (i = 0; i < MPSIZE; i++) maxpat[i] = VALUE;

                    handles it accurately and portably, doesn't care about what VALUE
                    is, and preserves the value of MPSIZE for any other uses. Note
                    that the bit of code above doesn't have to be in the scope of
                    maxpat, rather it can receive it as a parameter.

                    --
                    [mail]: Chuck F (cbfalconer at maineline dot net)
                    [page]: <http://cbfalconer.home .att.net>
                    Try the download section.


                    ** Posted from http://www.teranews.com **

                    Comment

                    • Willem

                      #11
                      Re: memset o/p not correct

                      Richard wrote:
                      ) In article <66vedbF2m6h5lU 1@mid.individua l.net>,
                      ) Default User <defaultuserbr@ yahoo.comwrote:
                      )
                      )> int maxpat[80];
                      )> memset(maxpat,-1,80);
                      )
                      )>As the others have pointed out, it sets each byte to the value. There's
                      )>almost no way to do what you want with memset(), unless you happen to
                      )>be on a machine that had one-byte ints.
                      )
                      ) Unless you really on the machines being 2s-complement, which is true
                      ) of all general-purpose computers today.

                      And then he wants to set the entire array to -2 instead.


                      SaSW, Willem
                      --
                      Disclaimer: I am in no way responsible for any of the statements
                      made in the above text. For all I know I might be
                      drugged or something..
                      No I'm not paranoid. You all think I'm paranoid, don't you !
                      #EOT

                      Comment

                      • Keith Thompson

                        #12
                        Re: memset o/p not correct

                        CBFalconer <cbfalconer@yah oo.comwrites:
                        [...]
                        #define MPSIZE 80
                        #define VALUE -1
                        int maxpat[MPSIZE], i;
                        >
                        for (i = 0; i < MPSIZE; i++) maxpat[i] = VALUE;
                        [...]

                        Quibble:
                        #define VALUE (-1)
                        It doesn't matter in this case, but it's a good habit.

                        --
                        Keith Thompson (The_Other_Keit h) <kst-u@mib.org>
                        Nokia
                        "We must do something. This is something. Therefore, we must do this."
                        -- Antony Jay and Jonathan Lynn, "Yes Minister"

                        Comment

                        • Ian Collins

                          #13
                          Re: memset o/p not correct

                          Keith Thompson wrote:
                          CBFalconer <cbfalconer@yah oo.comwrites:
                          [...]
                          > #define MPSIZE 80
                          > #define VALUE -1
                          > int maxpat[MPSIZE], i;
                          >>
                          > for (i = 0; i < MPSIZE; i++) maxpat[i] = VALUE;
                          [...]
                          >
                          Quibble:
                          #define VALUE (-1)
                          It doesn't matter in this case, but it's a good habit.
                          >
                          Better still

                          const int value = -1;

                          --
                          Ian Collins.

                          Comment

                          • CBFalconer

                            #14
                            Re: memset o/p not correct

                            Ian Collins wrote:
                            Keith Thompson wrote:
                            >CBFalconer <cbfalconer@yah oo.comwrites:
                            >>
                            >[...]
                            >>
                            >> #define MPSIZE 80
                            >> #define VALUE -1
                            >> int maxpat[MPSIZE], i;
                            >>>
                            >> for (i = 0; i < MPSIZE; i++) maxpat[i] = VALUE;
                            >[...]
                            >>
                            >Quibble:
                            > #define VALUE (-1)
                            >>
                            >It doesn't matter in this case, but it's a good habit.
                            >
                            Better still
                            >
                            const int value = -1;
                            The quibble is fine, but this last is a horror. We want a
                            constant, not a pre-initialized int. If you want c++, use the
                            appropriate newsgroup.

                            --
                            [mail]: Chuck F (cbfalconer at maineline dot net)
                            [page]: <http://cbfalconer.home .att.net>
                            Try the download section.


                            ** Posted from http://www.teranews.com **

                            Comment

                            • Ian Collins

                              #15
                              Re: memset o/p not correct

                              CBFalconer wrote:
                              Ian Collins wrote:
                              >Keith Thompson wrote:
                              >>CBFalconer <cbfalconer@yah oo.comwrites:
                              >>>
                              >>[...]
                              >>>
                              >>> #define MPSIZE 80
                              >>> #define VALUE -1
                              >>> int maxpat[MPSIZE], i;
                              >>>>
                              >>> for (i = 0; i < MPSIZE; i++) maxpat[i] = VALUE;
                              >>[...]
                              >>>
                              >>Quibble:
                              >> #define VALUE (-1)
                              >>>
                              >>It doesn't matter in this case, but it's a good habit.
                              >Better still
                              >>
                              >const int value = -1;
                              >
                              The quibble is fine, but this last is a horror. We want a
                              constant, not a pre-initialized int. If you want c++, use the
                              appropriate newsgroup.
                              >
                              I think you've topped your own stupidity this time.

                              --
                              Ian Collins.

                              Comment

                              Working...