Question about memcpy function in C++

Discussion in 'OT Technology' started by D1G1T4L, May 13, 2005.

  1. D1G1T4L

    D1G1T4L Active Member

    Joined:
    May 4, 2001
    Messages:
    16,489
    Likes Received:
    0
    Location:
    Bay Area

    I was just wondering if there was another way to do the same thing?

    maybe using the = operator? or no?
     
  2. samm

    samm Next in Line

    Joined:
    Dec 22, 2000
    Messages:
    2,630
    Likes Received:
    0
    Location:
    San Jose, CA
    sure you can use the equal operator in a loop to accomplish the same thing.

    Code:
    double* A = malloc(sizof(double) * 100);
    for (int i=0; i < 100; i++) {
      A[i] = i;
    }
    double* B = malloc(sizeof(double) * 100);
    for (int i=0; i < 100; i++) {
      B[i] = A[i];
    }
    
    Obviously using memcopy is easier to use, but you can shoot yourself in the foot easier since it does not do bounds checking.
     
  3. skinjob

    skinjob Active Member

    Joined:
    Jan 6, 2001
    Messages:
    2,337
    Likes Received:
    0
    Location:
    Aztlán
    You could override the = operator for void* to call memcpy, but you probably don't want to that because you hide the default behavior of assigning pointers. If you're dealing with raw data buffers where you want to ignore type, just stick with memcpy. For typed data, including your own classes, you can take advantage of copy constructors where = becomes equivalent to memcpy.
     

Share This Page