Thread: memcpy problem

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    204

    memcpy problem

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main(void)
    {
        int i, j;
        int lines, columns;
        int *matrix1, *matrix2;
        
        printf("lines: ");
        scanf("%d", &lines);
        
        printf("columns: ");
        scanf("%d", &columns);
        
        matrix1 = malloc(lines * columns);
        /*matrix2 = malloc(lines * columns);*/
        
        printf("\nvalues for matrix1\n");
        for (i = 0; i < lines; i++)
            for (j = 0; j < columns; j++) {
                printf("matrix1[%d][%d]: ", i, j);
                scanf("%d", &matrix1[i * columns + j]);
            }
        
        memcpy(matrix2, matrix1, sizeof(matrix1));
        
        for (i = 0; i < lines; i++)
            for (j = 0; j < columns; j++)
                printf("matrix2[%d][%d] = %d\n", i, j, matrix2[i * columns + j]);
        
        free(matrix1);
        free(matrix2);
        
        return 0;
    }
    When I run this program, this is what happens...
    Code:
    lines: 3
    columns: 3
    
    values for matrix1
    matrix1[0][0]: 1
    matrix1[0][1]: 2
    matrix1[0][2]: 3
    matrix1[1][0]: 4
    matrix1[1][1]: 5
    matrix1[1][2]: 6
    matrix1[2][0]: 7
    matrix1[2][1]: 8
    matrix1[2][2]: 9
    matrix2[0][0] = 1
    matrix2[0][1] = 1073834432
    matrix2[0][2] = 1073783856
    matrix2[1][0] = 1074016346
    matrix2[1][1] = 1074016362
    matrix2[1][2] = 1074016378
    matrix2[2][0] = 1074016394
    matrix2[2][1] = 1074265552
    matrix2[2][2] = 1074016426
    *** glibc detected *** free(): invalid next size (fast): 0x080498b8 ***
    Aborted
    What is going on here? Thanks.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    First, this method of dynamic allocation does not produce a contiguous array, so you cannot use memcpy.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    Perhaps you really didn't mean to comment this line out?
    Code:
    /*matrix2 = malloc(lines * columns);*/
    You need some memory to be able to put that information somewhere.

    [edit]
    Quote Originally Posted by Dave_Sinkula
    First, this method of dynamic allocation does not produce a contiguous array, so you cannot use memcpy.
    Why not? It's a single malloc call. Looks pretty contiguous to me.

    [edit again]
    Code:
    sizeof(matrix1)
    I'm pretty sure that will always be the size of your int pointer, not the size of the array. You need to copy (lines*columns), since that's what you malloc'ed.
    Last edited by pianorain; 07-08-2005 at 10:26 AM.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by pianorain
    [edit]Why not? It's a single malloc call. Looks pretty contiguous to me.
    Yes, I must be seeing things.
    Code:
        matrix1 = malloc(lines * columns * sizeof *matrix1);
        matrix2 = malloc(lines * columns * sizeof *matrix2);
        /* ... */
        memcpy(matrix2, matrix1, lines * columns * sizeof *matrix2);
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    204
    I changed the program to this...
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main(void)
    {
        int i, j;
        int lines, columns;
        int *matrix1, *matrix2;
        
        printf("lines: ");
        scanf("%d", &lines);
        
        printf("columns: ");
        scanf("%d", &columns);
        
        matrix1 = malloc(lines * columns);
        matrix2 = malloc(lines * columns);
        
        printf("\nvalues for matrix1\n");
        for (i = 0; i < lines; i++)
            for (j = 0; j < columns; j++) {
                printf("matrix1[%d][%d]: ", i, j);
                scanf("%d", &matrix1[i * columns + j]);
            }
        
        memcpy(matrix2, matrix1, lines * columns);
        
        for (i = 0; i < lines; i++)
            for (j = 0; j < columns; j++)
                printf("matrix2[%d][%d] = %d\n", i, j, matrix2[i * columns + j]);
        
        free(matrix1);
        free(matrix2);
        
        return 0;
    }
    Code:
    lines: 3
    columns: 3
    
    values for matrix1
    matrix1[0][0]: 1
    matrix1[0][1]: 2
    matrix1[0][2]: 3
    matrix1[1][0]: 4
    matrix1[1][1]: 5
    matrix1[1][2]: 6
    matrix1[2][0]: 7
    matrix1[2][1]: 8
    matrix1[2][2]: 9
    matrix2[0][0] = 1
    matrix2[0][1] = 2
    matrix2[0][2] = 3
    matrix2[1][0] = 8
    matrix2[1][1] = 9
    matrix2[1][2] = 0
    matrix2[2][0] = 0
    matrix2[2][1] = 0
    matrix2[2][2] = 0
    *** glibc detected *** free(): invalid next size (fast): 0x080498d0 ***
    Aborted

  6. #6
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    matrix1 = malloc(lines * columns);
    matrix2 = malloc(lines * columns);
    The elements of matrix1 and matrix2 are of type int so you should actually be allocating lines * columns * sizeof(int) bytes.
    memcpy(matrix2, matrix1, lines * columns);
    The third argument to memcpy() should be multiplied by sizeof(int) as well.
    Last edited by itsme86; 07-08-2005 at 10:42 AM.
    If you understand what you're doing, you're not learning anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  2. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  4. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  5. Replies: 14
    Last Post: 06-28-2006, 01:58 AM