Thread: A question about memcpy between two 2D arrays

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    74

    A question about memcpy between two 2D arrays

    Hi,

    I have the following question,

    Consider the following structure.

    Code:
    typedef struct ABC_SET{
    	int message_id;
    	int **A_array;
    	int size_a;
    }abc_t, *abc_ptr;
    next, I have an array of this structure type i.e.,

    Code:
    abc_ptr EXAMPLE_SET[40];
    next, I have a 2D array defined like this:
    Code:
    int **a_dummy_set;
    The memory has been allocated to
    Code:
    a_dummy_set
    through malloc. And, it holds some values.

    Next, I want to copy the contents of
    Code:
    a_dummy_set
    to A_array in the structure above i.e.,
    Code:
    memcpy(EXAMPLE_SET[i]->A_array, a_dummy_set, size)
    But, my program gives me segmentation fault. Can you help me why I am getting this behaviour and how to fix this.

    Thanks,

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,360
    Is this supposed to be C or C++? I note that you posted in the C++ programming forum, yet the code is entirely C, though admittedly also valid C++.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    74
    was supposed to be in C, you are right, do you know if there is way to move it to C programming forum, without having to post it again there

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,360
    *Moved to C programming forum*

    How exactly do you allocate memory for a_dummy_set? It would be easier if you posted the smallest and simplest compilable program that demonstrates the problem.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Apr 2011
    Posts
    74
    Code:
    signed char allocate2D(int ***arr, int nrows, int ncolumns)
    {
    	int **array2;
    	int i;
    
    	array2 = malloc(nrows * sizeof(int *));
    	if (!array2) {  
    		printf("\n**TOP LEVEL ERROR****\n");		    
          	return -1;
        }
    
    	array2[0] = malloc(nrows * ncolumns * sizeof(int));
    	if (!array2[0]) {
    		printf("\n**ERROR****\n");	
    		/* Free memory already allocated */
    		free(array2[0]);
            free(array2);
    		return -1;
    	}
    
    	for (i = 1; i < nrows; i++)
    		array2[i] = array2[0] + i * ncolumns;
    
    	*arr = array2;	
    	return 0;
    }
    and, then, I allocate memory to
    Code:
    a_dummy_set
    as follows:
    Code:
     ret = allocate2D( &a_dummy_set, 5, 7);
    but another important thing is that,

    there is a function that takes "a_dummy_set" as one of the arguments, and memory is allocated within this function, then I assume that once the function returns, the "a_dummy_set" with values stored inside is available to the calling function.

  6. #6
    Registered User
    Join Date
    Apr 2011
    Posts
    74
    Hi,

    thanks, I think, I was able to find different sources of problem,
    One of them was that I was instantiating a variable of type "abc_ptr", and I was using this to assign values to structure members without allocating memory to it first.
    I am debugging, I will ask in case I have further problems.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,360
    Why can't you just write:
    Code:
    EXAMPLE_SET[i]->A_array = a_dummy_set;
    ? The mempcy looks pointless.

    Quote Originally Posted by zahid990170
    thanks, I think, I was able to find different sources of problem,
    One of them was that I was instantiating a variable of type "abc_ptr", and I was using this to assign values to structure members without allocating memory to it first.
    This is why I told you to post the smallest and simplest compilable program that demonstrates the problem. I got this far:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    typedef struct ABC_SET{
        int message_id;
        int **A_array;
        int size_a;
    }abc_t, *abc_ptr;
    
    signed char allocate2D(int ***arr, int nrows, int ncolumns)
    {
        int **array2;
        int i;
     
        array2 = malloc(nrows * sizeof(int *));
        if (!array2) {  
            printf("\n**TOP LEVEL ERROR****\n");            
            return -1;
        }
     
        array2[0] = malloc(nrows * ncolumns * sizeof(int));
        if (!array2[0]) {
            printf("\n**ERROR****\n");  
            /* Free memory already allocated */
            free(array2[0]);
            free(array2);
            return -1;
        }
     
        for (i = 1; i < nrows; i++)
            array2[i] = array2[0] + i * ncolumns;
     
        *arr = array2;  
        return 0;
    }
    
    int main(void)
    {
        abc_ptr EXAMPLE_SET[40];
        size_t i;
        size_t size;
        abc_t x;
        int **a_dummy_set;
        if (allocate2D(&a_dummy_set, 5, 7) != 0) {
            printf("Error: allocate2D failed.\n");
            return EXIT_FAILURE;
        }
    
        i = 0;
        EXAMPLE_SET[i] = &x;
        EXAMPLE_SET[i]->A_array = a_dummy_set;
        free(a_dummy_set[0]);
        free(a_dummy_set);
        return 0;
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. About using memcpy on arrays:
    By Arthur91 in forum C Programming
    Replies: 5
    Last Post: 01-13-2013, 02:14 PM
  2. Question about memcpy
    By nik in forum C++ Programming
    Replies: 7
    Last Post: 04-02-2012, 05:49 PM
  3. memcpy question
    By absoult-tech1 in forum C Programming
    Replies: 5
    Last Post: 10-03-2010, 12:24 PM
  4. memcpy question
    By newbie30 in forum C Programming
    Replies: 2
    Last Post: 12-09-2009, 08:37 AM
  5. Arrays of Strings. With functions/memcpy?
    By Kleid-0 in forum C Programming
    Replies: 7
    Last Post: 01-11-2005, 07:56 PM