silly with memcpy()

Discussion in 'C Programming' started by Mark, Sep 30, 2011.

  1. Mark

    Mark Guest

    Hi,

    I have stuck with simple problem -- to copy number of bytes.

    #define MAX_QUEUES 4

    static int gweights[MAX_QUEUES] = { 1, 2, 3, 4 };
    int foo (int *weights)
    {
    if (!weights)
    return -1;

    /* weights is actually an array defined by the caller */
    memcpy(weights, gweights, MAX_QUEUES * sizeof weights);
    return 0;
    }

    It looks to me that code is correct, i.e. it will copy (MAX_QUEUES * sizeof
    weights) bytes from source to destination. Given that the size of pointer on
    my system is always 32bit, then regadless of MAX_QUEUE value the resulting #
    of bytes is correct. Is my reasoning right, or I'm missing something?
     
    Mark, Sep 30, 2011
    #1
    1. Advertising

  2. Mark

    Ben Pfaff Guest

    "Mark" <> writes:

    > #define MAX_QUEUES 4
    >
    > static int gweights[MAX_QUEUES] = { 1, 2, 3, 4 };
    > int foo (int *weights)
    > {
    > if (!weights)
    > return -1;
    >
    > /* weights is actually an array defined by the caller */
    > memcpy(weights, gweights, MAX_QUEUES * sizeof weights);


    "sizeof weights" should be "sizeof *weights". You want the size
    of an int, not the size of an int *.

    --
    "If I've told you once, I've told you LLONG_MAX times not to
    exaggerate."
    --Jack Klein
     
    Ben Pfaff, Sep 30, 2011
    #2
    1. Advertising

  3. Mark

    Lew Pitcher Guest

    On September 30, 2011 13:47, in comp.lang.c, wrote:

    > In article <j64v1t$r9l$>,
    > "Mark" <> wrote:
    >
    >> Hi,
    >>
    >> I have stuck with simple problem -- to copy number of bytes.
    >>
    >> #define MAX_QUEUES 4
    >>
    >> static int gweights[MAX_QUEUES] = { 1, 2, 3, 4 };
    >> int foo (int *weights)
    >> {
    >> if (!weights)
    >> return -1;
    >>
    >> /* weights is actually an array defined by the caller */
    >> memcpy(weights, gweights, MAX_QUEUES * sizeof weights);
    >> return 0;
    >> }

    >
    > Multiply by the sizeof the element type. The element type is int not int*,
    > so it should be
    > MAX_QUEUES * sizeof *weights


    That one is "best practices"

    > or
    > MAX_QUEUES * sizeof (int)


    That one works, but should be avoided in favour of the previous example.

    Should some later alteration of the code change the declaration of weights
    to some other type (i.e. from <<int *weights>> to <<long *weights>>, then
    the programmer /must/ als change the <<MAX_QUEUES * sizeof (int)>> to
    <<MAX_QUEUES * sizeof (long)>>

    OTOH, with the same change to the declaration of weights, no change is
    necessary for <<MAX_QUEUES * sizeof *weights>> as the compiler
    automatically knows which type to use. This makes the program more
    maintainable, and thus is counted as "best practices" wrt programming in C.

    --
    Lew Pitcher
    Master Codewright & JOAT-in-training | Registered Linux User #112576
    Me: http://pitcher.digitalfreehold.ca/ | Just Linux: http://justlinux.ca/
    ---------- Slackware - Because I know what I'm doing. ------
     
    Lew Pitcher, Sep 30, 2011
    #3
  4. Mark

    Mark Guest

    Thanks a lot.

    Mark.
     
    Mark, Sep 30, 2011
    #4
  5. Mark

    Jorgen Grahn Guest

    On Fri, 2011-09-30, Mark wrote:
    > Thanks a lot.


    For what? Already in your first posting, you seemed to know you had a
    bug -- did sizeof(int*) when you meant sizeof(int) -- and your
    question seemed to be if this would work if your pointers were 32-bit.

    /Jorgen

    --
    // Jorgen Grahn <grahn@ Oo o. . .
    \X/ snipabacken.se> O o .
     
    Jorgen Grahn, Sep 30, 2011
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Don

    Silly question....

    Don, Sep 26, 2003, in forum: VHDL
    Replies:
    2
    Views:
    482
  2. John
    Replies:
    2
    Views:
    483
  3. Homa
    Replies:
    7
    Views:
    454
    John Saunders
    Nov 16, 2003
  4. John

    silly question

    John, Jul 20, 2004, in forum: ASP .Net
    Replies:
    4
    Views:
    387
  5. Hugh G. Johnson
    Replies:
    1
    Views:
    379
    Greg Burns
    Sep 7, 2004
Loading...

Share This Page