Array elements get overridden?

This is a discussion on Array elements get overridden? within the C Programming forums, part of the General Programming Boards category; Hello, first the code that works: Code: int main(int argc, char *argv[]) { int r; int rc; int c; float ...

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    23

    Array elements get overridden?

    Hello,

    first the code that works:

    Code:
    int main(int argc, char *argv[])
    
    {
    
    	int r;
    
    	int rc;
    
    	int c;
    
    	float *m1;
    
    	float *m2;
    
    	float *m3;
    
    	int i;
    
    	float scan;
    
    
    
    	printf("Please enter rows of matrix 1: ");
    
    	scanf("%i", &r);
    
    	printf("Please enter columns/rows of matrix 1/2: ");
    
    	scanf("%i", &rc);
    
    	printf("Please enter columns of matrix 2: ");
    
    	scanf("%i", &c);
    
    
    
    	m1 = malloc(r*rc);
    
    	m2 = malloc(rc*c);
    
    	m3 = malloc(r*c);
    
    
    
    	printf("Enter elements of matrix 1:\n");
    
    
    
    	for(i = 0; i < r*rc; i++)
    
    	{
    
    		scanf("%f", &scan);
    
    		m1[i] = scan;
    
    	}
    
    
    
    	printf("Enter elements of matrix 2:\n");
    
    
    
    	for(i = 0; i < rc*c; i++)
    
    	{
    
    		scanf("%f", &scan);
    
    		m2[i] = scan;
    
    	}
    
    
    
    
    	for(i = 0; i < rc*c; i++)
    
    	{
    
    		printf("%f\n", m2[i]);
    
    	}
    
    
    
    	printf("Thank you, come again\n");
    
    	
    
    	return 0;
    
    }
    When I enter 9 1s (3*3 matrix) in the 2nd matrix here, I get 9 1s printed out.

    Now the broken code:

    Code:
    int main(int argc, char *argv[])
    
    {
    
    	int r;
    
    	int rc;
    
    	int c;
    
    	float *m1;
    
    	float *m2;
    
    	float *m3;
    
    	int i;
    
    	float scan;
    
    
    
    	printf("Please enter rows of matrix 1: ");
    
    	scanf("%i", &r);
    
    	printf("Please enter columns/rows of matrix 1/2: ");
    
    	scanf("%i", &rc);
    
    	printf("Please enter columns of matrix 2: ");
    
    	scanf("%i", &c);
    
    
    
    	m1 = malloc(r*rc);
    
    	m2 = malloc(rc*c);
    
    	m3 = malloc(r*c);
    
    
    
    	printf("Enter elements of matrix 1:\n");
    
    
    
    	for(i = 0; i < r*rc; i++)
    
    	{
    
    		scanf("%f", &scan);
    
    		m1[i] = scan;
    
    	}
    
    
    
    	printf("Enter elements of matrix 2:\n");
    
    
    
    	for(i = 0; i < rc*c; i++)
    
    	{
    
    		scanf("%f", &scan);
    
    		m2[i] = scan;
    
    	}
    
    	for(i = 0; i < r*c; i++)
    
    	{
    
    		m3[i] = 0.f;
    
    	}
    
    
    	for(i = 0; i < rc*c; i++)
    
    	{
    
    		printf("%f\n", m2[i]);
    
    	}
    
    
    
    	printf("Thank you, come again\n");
    
    	
    
    	return 0;
    
    }
    Here I get 1.0 1.0 1.0 1.0 0.0 0.0 ...

    As an output.

    What's wrong? Get the elements of my 2nd matrix overriden? If so, why? Or is it something else?
    Last edited by ThatDudeMan; 11-22-2010 at 12:26 PM.

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,171
    Double-spaced code is unnecessarily hard to read.
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,630
    I think it's because you're not malloc'ing enough space for a 2-d array of ints. You need to do malloc(r*c*sizeof(int)) or calloc(r*c, sizeof(int)) for all your malloc calls, since an int is more than 1 byte (4 on a 32-bit machine). malloc takes the number of bytes, not elements. It doesn't know what type of data you're mallocing, and an int on your system is likely 4 bytes (32-bit machine). You may also be having the same issue with m1, but since you don't print it, you don't notice it.

  4. #4
    Registered User
    Join Date
    Oct 2010
    Posts
    23
    I figured out that in the 2nd example some of my m3 elements override some m2 elements.

    Why?

    /edit: nvm

  5. #5
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    You need to multiply the amount of bytes to malloc by sizeof(float) in each calculation.

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,630
    D'oh! Nice catch nonoob.

    As a matter of fact, this could all be avoided if we did this the smart way. Multiply by sizeof(*m1), etc. That way, if you change m1 from float to double, you don't need to change your code.

    @ThatDudeMan: Try this on for size:
    Code:
    m1 = malloc(r*rc * sizeof(*m1));
    m2 = malloc(rc*c * sizeof(*m2));
    m3 = malloc(r*c * sizeof(*m3));

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 02-25-2010, 11:38 AM
  2. coping elements not in array
    By lord in forum C++ Programming
    Replies: 2
    Last Post: 08-04-2008, 08:53 PM
  3. way to check 3 elements of array and set 4th
    By Syneris in forum C++ Programming
    Replies: 3
    Last Post: 01-09-2006, 11:30 AM
  4. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 05:15 AM
  5. A simple question about selecting elements in an array
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 08-30-2001, 11:37 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21