sizeof

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

    sizeof

    I am using sizeof to doa memcpy but run into probs because of the
    following problem.

    static char arr1[] = {0x01,0x00,0x03 ,0x04};
    static char arr2[] = {0x00,0x03,0x04 ,0x06,0x07};

    const char* string_CDR[8]= {arr1,arr2};


    cout<<"The size of is " <<sizeof(arr1)< <" and "<<sizeof(a rr2) <<endl;
    cout<<"The size of is " <<sizeof(string _CDR[0])<<" and
    "<<sizeof(strin g_CDR[1]) <<endl;

    I expect the sizeof(string_C DR[0]) to be 4 and of
    sizeof(string_C DR[1]) to be 5. But the output is as below.
    The size of is 4 and 5
    The size of is 4 and 4


    This is messing up my memcpy in the main function as the sizeof is not
    what I am expecting. Can anyone point out the problem.

    Thanks
    Manny
  • Ian Collins

    #2
    Re: sizeof

    Slain wrote:
    I am using sizeof to doa memcpy but run into probs because of the
    following problem.
    >
    static char arr1[] = {0x01,0x00,0x03 ,0x04};
    static char arr2[] = {0x00,0x03,0x04 ,0x06,0x07};
    >
    const char* string_CDR[8]= {arr1,arr2};
    >
    >
    cout<<"The size of is " <<sizeof(arr1)< <" and "<<sizeof(a rr2) <<endl;
    cout<<"The size of is " <<sizeof(string _CDR[0])<<" and
    "<<sizeof(strin g_CDR[1]) <<endl;
    >
    This line outputs sizeof(char*) twice.
    >
    This is messing up my memcpy in the main function as the sizeof is not
    what I am expecting. Can anyone point out the problem.
    >
    There is no way to calculate the size of the arrays pointed to in
    string_CDR.

    Use std::vector.

    --
    Ian Collins.

    Comment

    • Slain

      #3
      Re: sizeof

      On Mar 7, 4:19 pm, Ian Collins <ian-n...@hotmail.co mwrote:
      Slain wrote:
      I am using sizeof to doa memcpy but run into probs because of the
      following problem.
      >
      static char arr1[] = {0x01,0x00,0x03 ,0x04};
      static char arr2[] = {0x00,0x03,0x04 ,0x06,0x07};
      >
      const char* string_CDR[8]= {arr1,arr2};
      >
      cout<<"The size of is " <<sizeof(arr1)< <" and "<<sizeof(a rr2)  <<endl;
      cout<<"The size of is " <<sizeof(string _CDR[0])<<" and
      "<<sizeof(strin g_CDR[1])  <<endl;
      >
      This line outputs sizeof(char*) twice.
      >
      >
      >
      This is messing up my memcpy in the main function as the sizeof is not
      what I am expecting. Can anyone point out the problem.
      >
      There is no way to calculate the size of the arrays pointed to in
      string_CDR.
      >
      Use std::vector.
      >
      --
      Ian Collins.
      DOn't have STL's. Is there any other way to define either string_CDR
      or some kind of casting?

      Comment

      • Ian Collins

        #4
        Re: sizeof

        Slain wrote:
        On Mar 7, 4:19 pm, Ian Collins <ian-n...@hotmail.co mwrote:
        >Slain wrote:
        >>I am using sizeof to doa memcpy but run into probs because of the
        >>following problem.
        >>static char arr1[] = {0x01,0x00,0x03 ,0x04};
        >>static char arr2[] = {0x00,0x03,0x04 ,0x06,0x07};
        >>const char* string_CDR[8]= {arr1,arr2};
        >>cout<<"The size of is " <<sizeof(arr1)< <" and "<<sizeof(a rr2) <<endl;
        >>cout<<"The size of is " <<sizeof(string _CDR[0])<<" and
        >>"<<sizeof(str ing_CDR[1]) <<endl;
        >This line outputs sizeof(char*) twice.
        >>
        >>This is messing up my memcpy in the main function as the sizeof is not
        >>what I am expecting. Can anyone point out the problem.
        >There is no way to calculate the size of the arrays pointed to in
        >string_CDR.
        >>
        >Use std::vector.
        >>
        *PLease* don't quote signatures.
        >
        DOn't have STL's. Is there any other way to define either string_CDR
        or some kind of casting?
        Then you have to save the length of the arrays somewhere. Why not use a
        small struct (with the pointer to the array and its size as members) as
        the type for string_CDR?

        --
        Ian Collins.

        Comment

        • Shobhit Gupta

          #5
          Re: sizeof

          You could do something like this: (I chose to stick closely with your
          problem, otherwise you would be better off with dynamically allocated
          arrays)



          struct MyArray{
          public:
          char *arr;
          unsigned int size;
          MyArray() {}
          void initialize(char tempArr[], int tempSize){
          arr = tempArr;
          size = tempSize;
          }


          };

          #define initialize(arg) initialize(arg, sizeof(arg))

          int main()
          {
          static char arr1[] = {0x01,0x00,0x03 ,0x04};
          static char arr2[] = {0x00,0x03,0x04 ,0x06,0x07};

          MyArray m1, m2;
          m1.initialize(a rr1);
          m2.initialize(a rr2);
          const MyArray string_CDR[8]= {m1,m2};

          cout<<"The size of is " << string_CDR[0].size<<" and "<<
          string_CDR[1].size <<endl;
          return 0;
          }

          -Shobhit
          This is our endeavor to share with rest of the world what we know about software engineering and the business around it.



          Comment

          • Ian Collins

            #6
            Re: sizeof

            [please retain context and attributions]

            Shobhit Gupta wrote:
            You could do something like this: (I chose to stick closely with your
            problem, otherwise you would be better off with dynamically allocated
            arrays)
            >
            struct MyArray{
            public:
            char *arr;
            unsigned int size;
            MyArray() {}
            void initialize(char tempArr[], int tempSize){
            arr = tempArr;
            size = tempSize;
            }
            >
            Why not do this in the constructor?
            >
            };
            >
            #define initialize(arg) initialize(arg, sizeof(arg))
            >
            You never use this.

            --
            Ian Collins.

            Comment

            • Shobhit Gupta

              #7
              Re: sizeof

              Why not do this in the constructor?
              >
              #define initialize(arg) initialize(arg, sizeof(arg))
              >
              You never use this.
              I thought of making code inside main() more readable for Slain guy
              ( Manny )

              So I thought of having only 1 argument, by which both array and its
              size could be set.
              Thats why I used a #define.
              And #defining a constructor didn't seem like a good idea, so I created
              an initialize().

              I just wanted to float the idea of grouping "The array" & "its size"
              in a struct; and as I said earlier there could be definitely be much
              more better way of implementing it.






              Comment

              • Ian Collins

                #8
                Re: sizeof

                [*please* don't snip attributions!]

                Shobhit Gupta wrote:
                Ian Collins wrote:
                >Why not do this in the constructor?
                >>
                >>#define initialize(arg) initialize(arg, sizeof(arg))
                >You never use this.
                >
                I thought of making code inside main() more readable for Slain guy
                ( Manny )
                >
                So I thought of having only 1 argument, by which both array and its
                size could be set.
                Thats why I used a #define.
                And #defining a constructor didn't seem like a good idea, so I created
                an initialize().
                >
                I just wanted to float the idea of grouping "The array" & "its size"
                in a struct; and as I said earlier there could be definitely be much
                more better way of implementing it.
                >
                OK. The simple template approach hinted at by Victor has the advantage
                of not adding a size member to the struct.

                --
                Ian Collins.

                Comment

                • James Kanze

                  #9
                  Re: sizeof

                  On 7 mar, 22:37, Slain <Slai...@gmail. comwrote:
                  On Mar 7, 4:19 pm, Ian Collins <ian-n...@hotmail.co mwrote:
                  Slain wrote:
                  I am using sizeof to doa memcpy but run into probs because of the
                  following problem.
                  static char arr1[] = {0x01,0x00,0x03 ,0x04};
                  static char arr2[] = {0x00,0x03,0x04 ,0x06,0x07};
                  const char* string_CDR[8]= {arr1,arr2};
                  cout<<"The size of is " <<sizeof(arr1)< <" and "<<sizeof(a rr2) <<endl;
                  cout<<"The size of is " <<sizeof(string _CDR[0])<<" and
                  "<<sizeof(strin g_CDR[1]) <<endl;
                  This line outputs sizeof(char*) twice.
                  This is messing up my memcpy in the main function as the
                  sizeof is not what I am expecting. Can anyone point out
                  the problem.
                  There is no way to calculate the size of the arrays pointed to in
                  string_CDR.
                  Use std::vector.
                  DOn't have STL's.
                  Why not? The alternative is reimplementing all of the logic it
                  contains manually. You seem to want the behavoir of a
                  container. So you really only have two choices: use the
                  standard containers, or implement one yourself.

                  --
                  James Kanze (GABI Software) email:james.kan ze@gmail.com
                  Conseils en informatique orientée objet/
                  Beratung in objektorientier ter Datenverarbeitu ng
                  9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

                  Comment

                  • Micah Cowan

                    #10
                    Re: sizeof

                    James Kanze <james.kanze@gm ail.comwrites:
                    On 7 mar, 22:37, Slain <Slai...@gmail. comwrote:
                    >On Mar 7, 4:19 pm, Ian Collins <ian-n...@hotmail.co mwrote:
                    Slain wrote:
                    I am using sizeof to doa memcpy but run into probs because of the
                    following problem.
                    >
                    static char arr1[] = {0x01,0x00,0x03 ,0x04};
                    static char arr2[] = {0x00,0x03,0x04 ,0x06,0x07};
                    >
                    const char* string_CDR[8]= {arr1,arr2};
                    >
                    cout<<"The size of is " <<sizeof(arr1)< <" and "<<sizeof(a rr2) <<endl;
                    cout<<"The size of is " <<sizeof(string _CDR[0])<<" and
                    "<<sizeof(strin g_CDR[1]) <<endl;
                    >
                    This line outputs sizeof(char*) twice.
                    >
                    This is messing up my memcpy in the main function as the
                    sizeof is not what I am expecting. Can anyone point out
                    the problem.
                    >
                    There is no way to calculate the size of the arrays pointed to in
                    string_CDR.
                    >
                    Use std::vector.
                    >
                    >DOn't have STL's.
                    >
                    Why not? The alternative is reimplementing all of the logic it
                    contains manually.
                    Might be using a freestanding implementation.
                    You seem to want the behavoir of a
                    container. So you really only have two choices: use the
                    standard containers, or implement one yourself.
                    Or learn to use arrays properly.

                    I suppose one could take an approach similar to:

                    struct Sized_array {
                    const char *ary;
                    size_t size;
                    };

                    Sized_aray string_CDR[] = {
                    {arr1, sizeof arr1}, {arr2, sizeof arr2}
                    };

                    --
                    Micah J. Cowan
                    Programmer, musician, typesetting enthusiast, gamer...

                    Comment

                    • James Kanze

                      #11
                      Re: sizeof

                      Micah Cowan wrote:
                      James Kanze <james.kanze@gm ail.comwrites:
                      DOn't have STL's.
                      Why not? The alternative is reimplementing all of the logic it
                      contains manually.
                      Might be using a freestanding implementation.
                      In which case, he might have to do some additional work (which
                      probably requires a higher level of experience as well).
                      You seem to want the behavoir of a
                      container. So you really only have two choices: use the
                      standard containers, or implement one yourself.
                      Or learn to use arrays properly.
                      You mean, learn enough to be able to implement std::vector
                      oneself.

                      Sooner or later, I think that most C++ programmers probably
                      should get to that point. (It's not as difficult as it seems.)
                      But it's certainly not where one should start.

                      --
                      James Kanze (GABI Software) email:james.kan ze@gmail.com
                      Conseils en informatique orientée objet/
                      Beratung in objektorientier ter Datenverarbeitu ng
                      9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

                      Comment

                      Working...