Memcpy problem! Plz help me, I'm totally stuck here :(

Discussion in 'C++' started by Paul Schouten, Jan 3, 2005.

  1. Hey,

    Currently im working on a project where a dynamic database is created in
    memory, the database can be of any size both in the amount of rows aswell as
    in the amount of columns. The database is filled with data from a mdb-file.
    During this process i use the following lines to copy the data from the
    db-file into memory:
    case dbInteger:
    (((MEMFIELD*)(((MEMROW*)(mRec->Table))[x]).Field)[y]).Value = new
    int;
    memcpy((int*)(((MEMFIELD*)(((MEMROW*)(mRec->Table))[x]).Field)[y]).Value,
    (int*)varValue.intVal, sizeof(int));
    case dbLong:
    (((MEMFIELD*)(((MEMROW*)(mRec->Table))[x]).Field)[y]).Value = new
    long;
    memcpy((long*)(((MEMFIELD*)(((MEMROW*)(mRec->Table))[x]).Field)[y]).Value,
    (long*)varValue.lVal, sizeof(long));

    Im sure there are many other ways to do this, some of which probably much
    more efficient and effective, however i've decided (by lack of knowledge
    about other methods) to use this one.
    Now this all works just fine but when i try this on the double-type i get an
    "type cast: cannot convert from 'double' to 'double *'" error message.....

    Can someone plz help me with this, i have no idea why this is happening!!!!

    This is the code for the double: (nearly the same as the ones above except
    for the types)
    (((MEMFIELD*)(((MEMROW*)(mRec->Table))[x]).Field)[y]).Value = new double;
    memcpy((double*)(((MEMFIELD*)(((MEMROW*)(mRec->Table))[x]).Field)[y]).Value,
    (double*)varValue.dblVal, sizeof(double));

    Cheers,

    Paul
     
    Paul Schouten, Jan 3, 2005
    #1
    1. Advertising

  2. Paul Schouten

    Ron Natalie Guest

    Paul Schouten wrote:
    > Hey,
    >
    > Currently im working on a project where a dynamic database is created in
    > memory, the database can be of any size both in the amount of rows aswell as
    > in the amount of columns. The database is filled with data from a mdb-file.
    > During this process i use the following lines to copy the data from the
    > db-file into memory:


    Why don't you use a class that can handle dynamically sized arrays.
    std::vector would be better than what you have here. I'm sure you
    can find a 2d vector-like class in the public libraries (like boost).

    > case dbInteger:
    > (((MEMFIELD*)(((MEMROW*)(mRec->Table))[x]).Field)[y]).Value = new
    > int;


    Allocating int's one at a time isn't very efficient...why not allcoate the
    whole row at once? And what's with all the casting? Even with the technique
    you're using it shouldn't be necessary.

    What is the definition of m_rec?

    By the way...all this crossposting is also a bad idea. Several of the
    groups you posted in aren't English. This has little to do with MFC.
     
    Ron Natalie, Jan 3, 2005
    #2
    1. Advertising

  3. Hi Paul,
    you need to take an address of varValue.dblVal instead of trying to cast it
    to (double*), to do this change second parameter from
    (double*)varValue.dblVal, to &varValue.dblVal.
    I believe that you should do the same for int and long.
    You can replace memcpy with:
    *((double*)(((MEMFIELD*)(((MEMROW*)(mRec->Table))[x]).Field)[y]).Value) =
    varValue.dblVal;

    Good luck,
    -- Vladimir Khvostov


    "Paul Schouten" <> wrote in message
    news:41d94caf$1$6219$4all.nl...
    > Hey,
    >
    > Currently im working on a project where a dynamic database is created in
    > memory, the database can be of any size both in the amount of rows aswell

    as
    > in the amount of columns. The database is filled with data from a

    mdb-file.
    > During this process i use the following lines to copy the data from the
    > db-file into memory:
    > case dbInteger:
    > (((MEMFIELD*)(((MEMROW*)(mRec->Table))[x]).Field)[y]).Value = new
    > int;
    >

    memcpy((int*)(((MEMFIELD*)(((MEMROW*)(mRec->Table))[x]).Field)[y]).Value,
    > (int*)varValue.intVal, sizeof(int));
    > case dbLong:
    > (((MEMFIELD*)(((MEMROW*)(mRec->Table))[x]).Field)[y]).Value = new
    > long;
    >

    memcpy((long*)(((MEMFIELD*)(((MEMROW*)(mRec->Table))[x]).Field)[y]).Value,
    > (long*)varValue.lVal, sizeof(long));
    >
    > Im sure there are many other ways to do this, some of which probably much
    > more efficient and effective, however i've decided (by lack of knowledge
    > about other methods) to use this one.
    > Now this all works just fine but when i try this on the double-type i get

    an
    > "type cast: cannot convert from 'double' to 'double *'" error message.....
    >
    > Can someone plz help me with this, i have no idea why this is

    happening!!!!
    >
    > This is the code for the double: (nearly the same as the ones above except
    > for the types)
    > (((MEMFIELD*)(((MEMROW*)(mRec->Table))[x]).Field)[y]).Value = new double;
    >

    memcpy((double*)(((MEMFIELD*)(((MEMROW*)(mRec->Table))[x]).Field)[y]).Value,
    > (double*)varValue.dblVal, sizeof(double));
    >
    > Cheers,
    >
    > Paul
    >
    >
     
    Vladimir Khvostov, Jan 3, 2005
    #3
    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. Replies:
    1
    Views:
    415
  2. Replies:
    2
    Views:
    367
    Nick Keighley
    Nov 24, 2006
  3. Replies:
    1
    Views:
    382
  4. nocturnal
    Replies:
    1
    Views:
    560
    nocturnal
    Jul 10, 2009
  5. David Arnold

    Totally stuck

    David Arnold, Jun 29, 2004, in forum: Perl Misc
    Replies:
    15
    Views:
    178
    Michele Dondi
    Jul 3, 2004
Loading...

Share This Page