2D dynamic array problem

This is a discussion on 2D dynamic array problem within the C Programming forums, part of the General Programming Boards category; I am in the process of converting a C++ program to C and have run into a major problem with ...

  1. #1
    Registered User
    Join Date
    Dec 2002

    2D dynamic array problem

    I am in the process of converting a C++ program to C and have run into
    a major problem with 2D dynamic arrays in C. The code I have posted
    below is just a test, but it does not work.

    What I am trying to do is read data from a file to figure out its
    dimensions (width and height) and then allocate an array with those
    values. I can get the dimensions just fine. I run into problems
    feeding those values into the dynamic allocation of the array.

    Ok heres the code...thanks in advance for the help!!!
    #include <stdio.h>
    #include <stdlib.h>
    int main()
    	int i, j;
    	int h=0, ho=0, w=0, wo=0, counter_w=0, counter_h=0, width=0;
    	char ch;
    FILE *input_file, *ouput_file;				/* Input file pointer
    	input_file = fopen("input.dat", "r");	
    	if(input_file == NULL)
    		printf("can't open file \n");
    		return 1;
    		while((ch=getc(input_file)) !=EOF)
    			if(ch != '\n')						/* Note: looking at white space also */
    				counter_w++;					/* counter_w = total character count */
    				counter_h++;					/* counter_h for height only */
    	width = ((counter_w)/16)/(counter_h);		/* calculates width */
    												                                /* '16' comes from fixed
    width of values + white space */
    	printf("width = ");
    	printf("%d", width);
    	printf("height = ");
    	printf("%d", counter_h);
    	h = counter_h;
    	ho = counter_h;
    	w = width;
    	wo = width;
    	double **test;
    	test = (double **)malloc(ho * sizeof(double *));
    	for(i=0; i<ho; i++)
    		test[i] = (double *)malloc(wo * sizeof(double));
    	return 0;
    &#91;code]&#91;/code]tagged by Salem

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    This is C, not C++. As such, unless you're running a C99 compiler, you need to declare all variables at the start of the function or scope.

    Next, there is no reason to typecast the return value of malloc. You can, there is just no point in it.*

    Finally, you can't just free the entire array in one shot. You must free each "row" first, then the rest of the array can be freed. (Free using a loop.)

    *Some people debate this, but this is neither the time nor the place. Search the forums if you disagree.
    Hope is the first step on the road to disappointment.

  4. #4
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    You're better off allocating the entire multidimensional array in one go -- IE multiply the number of rows by the number of columns and allocate an arrow of that length. Then, for accessing the array just multiply the row that you are trying to access times the number of columns in the array plus the column you are trying to access. That way you prevent memory fragmentation and you can use standard pointer arithmentic through rows as though it were a regular multidimensional array (this is how arrays are allocated internally by C++ anyways, so you might as well be more accurate in your model).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. put matrix from file into 2d dynamic array
    By meriororen in forum C Programming
    Replies: 3
    Last Post: 06-08-2009, 08:51 AM
  2. 2D array in dynamic memory
    By VanJay011379 in forum C++ Programming
    Replies: 3
    Last Post: 03-16-2003, 08:34 PM
  3. Dynamic Array Problem
    By adc85 in forum C++ Programming
    Replies: 2
    Last Post: 03-04-2003, 02:29 PM
  4. Replies: 4
    Last Post: 03-02-2003, 09:12 AM
  5. dynamic 2D array handling ?
    By (*)BLUE in forum C Programming
    Replies: 5
    Last Post: 12-04-2002, 12:50 PM

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