memcpy()

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

    memcpy()

    hello!

    i've go two structures:

    --cut here--
    const unsigned int max_pkg_data=50 00;

    typedef struct pkg_ {
    short int info;
    short int size;
    char data[max_pkg_data];
    } pkg;

    typedef struct binary_ {
    char* buffer;
    int size;
    } binary;
    --cut here--

    and i cant understand, why isnt this function working:

    --cut here--
    void SendBinary(bina ry& sent)
    {
    unsigned int n=0;
    for (n=0;n<sent.siz e/max_pkg_data;++ n)
    {
    pkg nPkg;
    memcpy((void*)n Pkg.data,(void* )sent.buffer[n*max_pkg_data],max_pkg_data);
    nPkg.info=0;
    nPkg.size=max_p kg_data;
    // tu wpisujesz swoj kod do obs³ugi binary
    Form1->CC1->Socket->SendBuf((void* )&nPkg,sizeof(p kg));
    }
    pkg nPkg;
    memcpy((void*)n Pkg.data,(void* )sent.buffer[n*max_pkg_data],max_pkg_data);
    nPkg.info=0;
    nPkg.size=sent. size-((n)*max_pkg_da ta);
    Form1->CC1->Socket->SendBuf((void* )&nPkg,sizeof(p kg));

    return;
    }
    --cut here--

    Especcialy:
    memcpy((void*)n Pkg.data,(void* )sent.buffer[n*max_pkg_data],max_pkg_data);

    Gives me an Acces Violation Message? Why is it so? Anyone could help me?
    --
    ishmael4



  • David Resnick

    #2
    Re: memcpy()

    ishmael4 wrote:[color=blue]
    > hello!
    >
    > i've go two structures:
    >
    > --cut here--
    > const unsigned int max_pkg_data=50 00;
    >
    > typedef struct pkg_ {
    > short int info;
    > short int size;
    > char data[max_pkg_data];
    > } pkg;
    >
    > typedef struct binary_ {
    > char* buffer;
    > int size;
    > } binary;
    > --cut here--
    >
    > and i cant understand, why isnt this function working:
    >
    > --cut here--
    > void SendBinary(bina ry& sent)
    > {
    > unsigned int n=0;
    > for (n=0;n<sent.siz e/max_pkg_data;++ n)
    > {
    > pkg nPkg;
    > memcpy((void*)n Pkg.data,(void* )sent.buffer[n*max_pkg_data],max_pkg_data);
    > nPkg.info=0;
    > nPkg.size=max_p kg_data;
    > // tu wpisujesz swoj kod do obs³ugi binary
    > Form1->CC1->Socket->SendBuf((void* )&nPkg,sizeof(p kg));
    > }
    > pkg nPkg;
    > memcpy((void*)n Pkg.data,(void* )sent.buffer[n*max_pkg_data],max_pkg_data);
    > nPkg.info=0;
    > nPkg.size=sent. size-((n)*max_pkg_da ta);
    > Form1->CC1->Socket->SendBuf((void* )&nPkg,sizeof(p kg));
    >
    > return;
    > }
    > --cut here--
    >
    > Especcialy:
    > memcpy((void*)n Pkg.data,(void* )sent.buffer[n*max_pkg_data],max_pkg_data);
    >
    > Gives me an Acces Violation Message? Why is it so? Anyone could help me?
    > --
    > ishmael4[/color]

    What makes you think that your buffer is an exactl multiple of
    max_pkg_data in size? For example, suppose that buffer is 4 bytes.
    You will copy those 4 bytes, but also the next 4996 bytes, which you
    ought not be reading.

    -David

    Comment

    • Eric Sosman

      #3
      Re: memcpy()



      ishmael4 wrote On 09/22/05 12:47,:[color=blue]
      > hello!
      >
      > i've go two structures:
      >
      > --cut here--
      > const unsigned int max_pkg_data=50 00;
      >
      > typedef struct pkg_ {
      > short int info;
      > short int size;
      > char data[max_pkg_data];
      > [...][/color]

      comp.lang.c++ is down the hall to your left, just
      past the vending machines.

      --
      Eric.Sosman@sun .com

      Comment

      • ishmael4

        #4
        Re: memcpy()

        Uzytkownik "Eric Sosman" <eric.sosman@su n.com> napisal w wiadomosci
        news:dgunk0$cfk $1@news1brm.Cen tral.Sun.COM...[color=blue]
        >
        >
        > ishmael4 wrote On 09/22/05 12:47,:[color=green]
        >> hello!
        >>
        >> i've go two structures:
        >>
        >> --cut here--
        >> const unsigned int max_pkg_data=50 00;
        >>
        >> typedef struct pkg_ {
        >> short int info;
        >> short int size;
        >> char data[max_pkg_data];
        >> [...][/color]
        >
        > comp.lang.c++ is down the hall to your left, just
        > past the vending machines.
        >
        > --
        > Eric.Sosman@sun .com
        >[/color]

        why do you think its c++?

        ishmael4


        Comment

        • ishmael4

          #5
          Re: memcpy()


          Uzytkownik "David Resnick" <lndresnick@gma il.com> napisal w wiadomosci
          news:1127407959 .325028.218670@ g47g2000cwa.goo glegroups.com.. .
          ishmael4 wrote:[color=blue]
          > hello!
          >
          > i've go two structures:
          >
          > --cut here--
          > const unsigned int max_pkg_data=50 00;
          >
          > typedef struct pkg_ {
          > short int info;
          > short int size;
          > char data[max_pkg_data];
          > } pkg;
          >
          > typedef struct binary_ {
          > char* buffer;
          > int size;
          > } binary;
          > --cut here--
          >
          > and i cant understand, why isnt this function working:
          >
          > --cut here--
          > void SendBinary(bina ry& sent)
          > {
          > unsigned int n=0;
          > for (n=0;n<sent.siz e/max_pkg_data;++ n)
          > {
          > pkg nPkg;
          >
          > memcpy((void*)n Pkg.data,(void* )sent.buffer[n*max_pkg_data],max_pkg_data);
          > nPkg.info=0;
          > nPkg.size=max_p kg_data;
          > // tu wpisujesz swoj kod do obs³ugi binary
          > Form1->CC1->Socket->SendBuf((void* )&nPkg,sizeof(p kg));
          > }
          > pkg nPkg;
          > memcpy((void*)n Pkg.data,(void* )sent.buffer[n*max_pkg_data],max_pkg_data);
          > nPkg.info=0;
          > nPkg.size=sent. size-((n)*max_pkg_da ta);
          > Form1->CC1->Socket->SendBuf((void* )&nPkg,sizeof(p kg));
          >
          > return;
          > }
          > --cut here--
          >
          > Especcialy:
          >
          > memcpy((void*)n Pkg.data,(void* )sent.buffer[n*max_pkg_data],max_pkg_data);
          >
          > Gives me an Acces Violation Message? Why is it so? Anyone could help me?
          > --
          > ishmael4
          >
          > What makes you think that your buffer is an exactl multiple of
          > max_pkg_data in size? For example, suppose that buffer is 4 bytes.
          > You will copy those 4 bytes, but also the next 4996 bytes, which you
          > ought not be reading.
          >
          > -David[/color]

          for (n=0;n<sent.siz e/max_pkg_data;++ n)
          guarantees me that its larger than 5000?

          ishamel4


          Comment

          • David Resnick

            #6
            Re: memcpy()


            ishmael4 wrote:[color=blue]
            > Uzytkownik "David Resnick" <lndresnick@gma il.com> napisal w wiadomosci
            > news:1127407959 .325028.218670@ g47g2000cwa.goo glegroups.com.. .
            > ishmael4 wrote:[color=green]
            > > hello!
            > >
            > > i've go two structures:
            > >
            > > --cut here--
            > > const unsigned int max_pkg_data=50 00;
            > >
            > > typedef struct pkg_ {
            > > short int info;
            > > short int size;
            > > char data[max_pkg_data];
            > > } pkg;
            > >
            > > typedef struct binary_ {
            > > char* buffer;
            > > int size;
            > > } binary;
            > > --cut here--
            > >
            > > and i cant understand, why isnt this function working:
            > >
            > > --cut here--
            > > void SendBinary(bina ry& sent)
            > > {
            > > unsigned int n=0;
            > > for (n=0;n<sent.siz e/max_pkg_data;++ n)
            > > {
            > > pkg nPkg;
            > >
            > > memcpy((void*)n Pkg.data,(void* )sent.buffer[n*max_pkg_data],max_pkg_data);
            > > nPkg.info=0;
            > > nPkg.size=max_p kg_data;
            > > // tu wpisujesz swoj kod do obs³ugi binary
            > > Form1->CC1->Socket->SendBuf((void* )&nPkg,sizeof(p kg));
            > > }
            > > pkg nPkg;
            > > memcpy((void*)n Pkg.data,(void* )sent.buffer[n*max_pkg_data],max_pkg_data);
            > > nPkg.info=0;
            > > nPkg.size=sent. size-((n)*max_pkg_da ta);
            > > Form1->CC1->Socket->SendBuf((void* )&nPkg,sizeof(p kg));
            > >
            > > return;
            > > }
            > > --cut here--
            > >
            > > Especcialy:
            > >
            > > memcpy((void*)n Pkg.data,(void* )sent.buffer[n*max_pkg_data],max_pkg_data);
            > >
            > > Gives me an Acces Violation Message? Why is it so? Anyone could help me?
            > > --
            > > ishmael4
            > >
            > > What makes you think that your buffer is an exactl multiple of
            > > max_pkg_data in size? For example, suppose that buffer is 4 bytes.
            > > You will copy those 4 bytes, but also the next 4996 bytes, which you
            > > ought not be reading.
            > >
            > > -David[/color]
            >
            > for (n=0;n<sent.siz e/max_pkg_data;++ n)
            > guarantees me that its larger than 5000?
            >
            > ishamel4[/color]

            The one after the loop sends the "remainder" . That one is the problem
            I think. You only want to copy the actual number of bytes (which is
            less
            than 5000). By copying the full 5000, you are reading where you may
            not
            go.

            -David

            Comment

            • Martin Ambuhl

              #7
              Re: memcpy()

              ishmael4 wrote what appears to be C++ (so obfuscated from the first):
              [color=blue]
              > hello!
              >
              > i've go two structures:[/color]
              [...]
              Since you have posted to comp.lang.c, I suppose you have a C question.
              I have provided an attempt at rewriting you code into C. Could you
              check that it actually conforms to your intent, modifying it so it does,
              and ask your question in that context? If you meant to actually ask
              about code in the byzantine language C++, that abomination has its own
              newsgroup <news:comp.lang .c++>

              [attempted rewrite]
              #include <string.h>

              #if 0
              /* mha: The line */
              const unsigned int max_pkg_data = 5000;
              /* mha: does not create a constant. Since the typedef pkg outside
              of a function requires a compile-time constant for the size of
              'data', this has been replaced. */
              #endif
              #define MAX_PKG_DATA 5000

              typedef struct
              {
              short int info;
              short int size;
              char data[MAX_PKG_DATA];
              } pkg;

              typedef struct
              {
              char *buffer;
              int size;
              } binary;

              void SendBinary(bina ry /* mha: C++-ism '&' replaced, uses of
              'sent' fixed, pointless casts
              removed */ * sent)
              {
              unsigned int n = 0;
              for (n = 0; n < (unsigned) sent->size / MAX_PKG_DATA; ++n) {
              pkg nPkg;
              /* mha: the following had a char (buff[n * MAX_PKG_DATA]) as the
              source for the copy. Changed */
              memcpy(nPkg.dat a, sent->buffer + (n * MAX_PKG_DATA),
              MAX_PKG_DATA);
              nPkg.info = 0;
              nPkg.size = MAX_PKG_DATA;
              #if 0
              /* mha: no information about 'Form1' was supplied. Line
              suppressed */
              Form1->CC1->Socket->SendBuf(&nPk g, sizeof(pkg));
              #endif
              }
              pkg nPkg;
              /* mha: the following had a char (buff[n * MAX_PKG_DATA]) as the
              source for the copy. Changed */
              memcpy(nPkg.dat a, sent->buffer + (n * MAX_PKG_DATA), MAX_PKG_DATA);
              nPkg.info = 0;
              nPkg.size = sent->size - (n * MAX_PKG_DATA);
              #if 0
              /* mha: no information about 'Form1' was supplied. Line suppressed */
              Form1->CC1->Socket->SendBuf(&nPk g, sizeof(pkg));
              #endif

              }

              int main(void)
              {
              binary f;
              SendBinary(&f);
              return 0;
              }


              [OP's post continued][color=blue]
              >
              > --cut here--
              > const unsigned int max_pkg_data=50 00;
              >
              > typedef struct pkg_ {
              > short int info;
              > short int size;
              > char data[max_pkg_data];
              > } pkg;
              >
              > typedef struct binary_ {
              > char* buffer;
              > int size;
              > } binary;
              > --cut here--
              >
              > and i cant understand, why isnt this function working:
              >
              > --cut here--
              > void SendBinary(bina ry& sent)
              > {
              > unsigned int n=0;
              > for (n=0;n<sent.siz e/max_pkg_data;++ n)
              > {
              > pkg nPkg;
              > memcpy((void*)n Pkg.data,(void* )sent.buffer[n*max_pkg_data],max_pkg_data);
              > nPkg.info=0;
              > nPkg.size=max_p kg_data;
              > // tu wpisujesz swoj kod do obs³ugi binary
              > Form1->CC1->Socket->SendBuf((void* )&nPkg,sizeof(p kg));
              > }
              > pkg nPkg;
              > memcpy((void*)n Pkg.data,(void* )sent.buffer[n*max_pkg_data],max_pkg_data);
              > nPkg.info=0;
              > nPkg.size=sent. size-((n)*max_pkg_da ta);
              > Form1->CC1->Socket->SendBuf((void* )&nPkg,sizeof(p kg));
              >
              > return;
              > }
              > --cut here--
              >
              > Especcialy:
              > memcpy((void*)n Pkg.data,(void* )sent.buffer[n*max_pkg_data],max_pkg_data);
              >
              > Gives me an Acces Violation Message? Why is it so? Anyone could help me?[/color]

              Comment

              • Eric Sosman

                #8
                Re: memcpy()



                ishmael4 wrote On 09/22/05 13:05,:[color=blue]
                > Uzytkownik "Eric Sosman" <eric.sosman@su n.com> napisal w wiadomosci
                > news:dgunk0$cfk $1@news1brm.Cen tral.Sun.COM...
                >[color=green]
                >>
                >>ishmael4 wrote On 09/22/05 12:47,:
                >>[color=darkred]
                >>>hello!
                >>>
                >>>i've go two structures:
                >>>
                >>>--cut here--
                >>>const unsigned int max_pkg_data=50 00;
                >>>
                >>>typedef struct pkg_ {
                >>>short int info;
                >>>short int size;
                >>>char data[max_pkg_data];
                >>>[...][/color]
                >>
                >> comp.lang.c++ is down the hall to your left, just
                >>past the vending machines.[/color]
                >
                > why do you think its c++?[/color]

                It isn't C, and it certainly looks like some of the
                bits of C++ I've seen. You're right, though: it might
                not actually be C++, but rather a snippet of code in
                Ada or Babbage or ... or Yoda or Zuniga. I apologize
                for jumping to a conclusion: non-C-ness does not imply
                C++-ness.

                The non-C-ness, however, is evident. A C compiler
                object to the line just before my elision, because in
                C a `const' variable does not qualify as a constant
                expression. A C compiler would also object to this
                subsequent line

                void SendBinary(bina ry& sent)

                .... and I admit it's this second offense that caught my
                eye first, causing me to re-examine the earlier lines
                more closely, which is when I spotted the first problem.

                Whatever your code is, it's not C.

                --
                Eric.Sosman@sun .com

                Comment

                • Old Wolf

                  #9
                  Re: memcpy()

                  Eric Sosman wrote:[color=blue]
                  > ishmael4 wrote:[color=green]
                  >> "Eric Sosman":[color=darkred]
                  >>> comp.lang.c++ is down the hall to your left, just
                  >>>past the vending machines.[/color]
                  >>
                  >> why do you think its c++?[/color]
                  >
                  > It isn't C, and it certainly looks like some of the
                  > bits of C++ I've seen.[/color]

                  Your instincts were correct. I'm not sure why the OP
                  might pretend it was not C++ ...

                  FWIW I think the OP's problem stems from the line:
                  [color=blue][color=green][color=darkred]
                  >>>> memcpy((void*)n Pkg.data,(void* )sent.buffer[n*max_pkg_data],
                  >>>> max_pkg_data);[/color][/color][/color]

                  because sent.buffer[X] is a char, and he's casting a char to a
                  pointer. He probably meant: (void *)&sent.buffer[n*max_pkg_data].

                  A good reason to avoid stupid casting.

                  BTW, I haven't bothered to check if the memcpy is actually doing
                  something valid and non-overlapping.

                  Comment

                  • ishmael4

                    #10
                    Re: memcpy()

                    Uzytkownik "Eric Sosman" <eric.sosman@su n.com> napisal w wiadomosci
                    news:dguro5$eqi $1@news1brm.Cen tral.Sun.COM...[color=blue]
                    >
                    >
                    > ishmael4 wrote On 09/22/05 13:05,:[color=green]
                    >> Uzytkownik "Eric Sosman" <eric.sosman@su n.com> napisal w wiadomosci
                    >> news:dgunk0$cfk $1@news1brm.Cen tral.Sun.COM...
                    >>[color=darkred]
                    >>>
                    >>>ishmael4 wrote On 09/22/05 12:47,:
                    >>>
                    >>>>hello!
                    >>>>
                    >>>>i've go two structures:
                    >>>>
                    >>>>--cut here--
                    >>>>const unsigned int max_pkg_data=50 00;
                    >>>>
                    >>>>typedef struct pkg_ {
                    >>>>short int info;
                    >>>>short int size;
                    >>>>char data[max_pkg_data];
                    >>>>[...]
                    >>>
                    >>> comp.lang.c++ is down the hall to your left, just
                    >>>past the vending machines.[/color]
                    >>
                    >> why do you think its c++?[/color]
                    >
                    > It isn't C, and it certainly looks like some of the
                    > bits of C++ I've seen. You're right, though: it might
                    > not actually be C++, but rather a snippet of code in
                    > Ada or Babbage or ... or Yoda or Zuniga. I apologize
                    > for jumping to a conclusion: non-C-ness does not imply
                    > C++-ness.
                    >
                    > The non-C-ness, however, is evident. A C compiler
                    > object to the line just before my elision, because in
                    > C a `const' variable does not qualify as a constant
                    > expression. A C compiler would also object to this
                    > subsequent line
                    >
                    > void SendBinary(bina ry& sent)
                    >
                    > ... and I admit it's this second offense that caught my
                    > eye first, causing me to re-examine the earlier lines
                    > more closely, which is when I spotted the first problem.
                    >
                    > Whatever your code is, it's not C.[/color]

                    U needn't have used so many words. "> void SendBinary(bina ry& sent)" as an
                    example is enough. Well I admit using C++, but this certain line:
                    "memcpy((void*) nPkg.data,(void *)sent.buffer[n*max_pkg_data],max_pkg_data); "
                    is in pure C, isnt it? This is the major problem. So what do I do? Shall i
                    start a new topic?
                    [color=blue]
                    > --
                    > Eric.Sosman@sun .com[/color]

                    --
                    ishmael4


                    Comment

                    • ishmael4

                      #11
                      Re: memcpy()

                      Uzytkownik "Old Wolf" <oldwolf@inspir e.net.nz> napisal w wiadomosci
                      news:1127448502 .527785.23040@g 43g2000cwa.goog legroups.com...[color=blue]
                      > Eric Sosman wrote:[color=green]
                      >> ishmael4 wrote:[color=darkred]
                      >>> "Eric Sosman":
                      >>>> comp.lang.c++ is down the hall to your left, just
                      >>>>past the vending machines.
                      >>>
                      >>> why do you think its c++?[/color]
                      >>
                      >> It isn't C, and it certainly looks like some of the
                      >> bits of C++ I've seen.[/color]
                      >
                      > Your instincts were correct. I'm not sure why the OP
                      > might pretend it was not C++ ...
                      >
                      > FWIW I think the OP's problem stems from the line:
                      >[color=green][color=darkred]
                      >>>>> memcpy((void*)n Pkg.data,(void* )sent.buffer[n*max_pkg_data],
                      >>>>> max_pkg_data);[/color][/color]
                      >
                      > because sent.buffer[X] is a char, and he's casting a char to a
                      > pointer. He probably meant: (void *)&sent.buffer[n*max_pkg_data].
                      >[/color]

                      Well, I thouht that char x[y] is actually (char*)x+y. Am i wrong?
                      [color=blue]
                      > A good reason to avoid stupid casting.
                      >
                      > BTW, I haven't bothered to check if the memcpy is actually doing
                      > something valid and non-overlapping.
                      >[/color]


                      Comment

                      • pete

                        #12
                        Re: memcpy()

                        ishmael4 wrote:
                        [color=blue]
                        > Well, I thouht that char x[y] is actually (char*)x+y. Am i wrong?[/color]

                        (char) *(x + y)

                        --
                        pete

                        Comment

                        • Eric Sosman

                          #13
                          Re: memcpy()

                          ishmael4 wrote:[color=blue]
                          >
                          > U needn't have used so many words.[/color]

                          U, hwvr, shd use mor ltrs.
                          [color=blue]
                          > "> void SendBinary(bina ry& sent)" as an
                          > example is enough. Well I admit using C++, but this certain line:
                          > "memcpy((void*) nPkg.data,(void *)sent.buffer[n*max_pkg_data],max_pkg_data); "
                          > is in pure C, isnt it?[/color]

                          No, it's pure C++ because it's part of a C++ program.
                          Standing by itself it's nothing at all: it will be rejected
                          by a C compiler, by a C++ compiler, by a COBOL compiler, by
                          awk, perl, MCR, and COMMAND.COM. A program in C (C++, ...)
                          cannot be minced quite so finely and still retain its
                          linguistic identity: DNA is no longer DNA once you separate
                          the atoms from each other. Thought question: is the fragment
                          `X = 42' part of a C program or part of a BASIC program?
                          [color=blue]
                          > This is the major problem. So what do I do? Shall i
                          > start a new topic?[/color]

                          Yes, but in comp.lang.c++. I'll grant you that there are
                          many similarities between C and C++ and that in many cases
                          both languages will give you the same answer. But in some
                          cases the two languages differ in important and non-obvious
                          ways, and an answer that's correct for C may be nonsense in
                          a C++ program. Since the problem has already baffled you, it
                          follows that you are particularly UNqualified to judge whether
                          the differences between the languages are involved in it.

                          Italian is just a variation on Latin, and so is Spanish,
                          right? If you're having trouble translating a tricky passage
                          from "Inferno," should you seek help from Marco or from Miguel?

                          --
                          Eric Sosman
                          esosman@acm-dot-org.invalid

                          Comment

                          • Old Wolf

                            #14
                            Re: memcpy()

                            ishmael4 wrote:[color=blue]
                            >
                            > Well, I thouht that char x[y] is actually (char*)x+y. Am i wrong?[/color]

                            Yes you are wrong. x[y] means *(x+y). x+y is a pointer (points
                            'y' chars past where 'x' is pointing). *(x+y) is what you get when
                            you dereference that pointer. x[y] is a nicer way of writing *(x+y).

                            Note that x+y means the same as (char*)x+y, since x is already a
                            char* .

                            Comment

                            • Martin Ambuhl

                              #15
                              Re: memcpy()

                              ishmael4 wrote:[color=blue]
                              > Uzytkownik "Eric Sosman" <eric.sosman@su n.com> napisal w wiadomosci[/color]
                              [color=blue]
                              > U needn't have used so many words. "> void SendBinary(bina ry& sent)" as an
                              > example is enough. Well I admit using C++, but this certain line:
                              > "memcpy((void*) nPkg.data,(void *)sent.buffer[n*max_pkg_data],max_pkg_data); "
                              > is in pure C, isnt it? This is the major problem. So what do I do? Shall i
                              > start a new topic?[/color]

                              Did you not check my answer in
                              Message-ID: <TIBYe.3072$q1. 255@newsread3.n ews.atl.earthli nk.net>
                              Date: Thu, 22 Sep 2005 17:19:47 GMT?

                              Look at the way the arguments to memcpy are changed. You are
                              inappropriately using a char (coerced with a cast) as a (void *).

                              Your present message is dated
                              Date: Fri, 23 Sep 2005 10:56:48 +0200
                              You have had plenty of time even with very bad propagation conditions.

                              If you ignore the answers given you, why should anyone bother trying to
                              help you?

                              Comment

                              Working...