I am trying to build two dimensional array by dynamically allocating. My question is that is it possible that its first dimension would take 100 values, then second dimension would take variable amount of values depending on my problem? If it is possible then how I would access it? How would I know the second dimension's boundary?
-
2This totally depends on how you implement the 2D array. Is it an array-of-arrays (e.g. `int matrix[M][N]`) or is it a standard array, that you're applying two "logical" dimensions to (e.g. `int matrix[M*N]`)? – Jonathon Reinhart Nov 12 '13 at 03:44
4 Answers
(See the comments in the code)
As a result you'll get an array such like the following:
// Create an array that will contain required variables of the required values
// which will help you to make each row of it's own lenght.
arrOfLengthOfRows[NUMBER_OF_ROWS] = {value_1, value_2, ..., value_theLast};
int **array;
array = malloc(N * sizeof(int *)); // `N` is the number of rows, as on the pic.
/*
if(array == NULL) {
printf("There is not enough memory.\n");
exit (EXIT_FAILURE);
}
*/
// Here we make each row of it's own, individual length.
for(i = 0; i < N; i++) {
array[i] = malloc(arrOfLengthOfRows[i] * sizeof(int));
/*
if(array[i] == NULL) {
printf("There is not enough memory.\n");
exit (EXIT_FAILURE);
}
*/
}

- 1,601
- 3
- 21
- 49
-
This ignores the question's request for dynamically sized inner arrays. – Martin Nov 12 '13 at 03:52
-
@Martin, What do you mean exactly? (If I understood the question incorrectly, I will delete this answer) – yulian Nov 12 '13 at 03:53
-
`
`Your code does not compile as-is. I would either remove the `#include`, or wrap the rest of the code up in a function. Including the `#include` line somewhat implies that this is a compilable example. – Jonathon Reinhart Nov 12 '13 at 03:53 -
From the question: "then second dimension would take variable amount of values depending on my problem". You're allocating a fixed number of elements. – Martin Nov 12 '13 at 03:55
-
-
1Nice work. +1 for the image, and the idea of tracking lengths in a separate array. – Martin Nov 12 '13 at 05:52
-
@Yulian i had the same implenetation but using pointers to `char`s, and eventually it was crashing on some `free`. the problem here is you are trying to overwrite some dynamically allocated memory. this will corrupt the heap. after `int **array;` `array = malloc(N * sizeof(int *));` you have an array of pointers. when you want an element to point to some other array, just copy the address of the first element of that array, to your `array[i]`. of course, the second array needs to be `malloc`'ed – Hame Nov 27 '14 at 02:55
-
@Hame, i saw your question about that. I'm sure, if you were using debugger, you would easily find out what was wrong. It's really useful tool, so you'd better use it almost every time you get a problem. But as for your comment, I'm not sure I understand you right. Do you suggest to allocate only one pointer and then assign it to the first element of the array? And what is about heap corruption there? – yulian Nov 27 '14 at 14:24
You can use array of 100 pointers:
int *arr[100];
then you can dynamically allocate memory to each of the 100 pointers separately of any size you want, however you have to remember how much memory (for each pointer) you have allocated, you cannot expect C compiler to remember it or tell it to you, i.e. sizeof
will not work here.
To access any (allowed, within boundary) location you can simply use 2D array notation e.g. to access 5th
location of memory allocated to 20th
pointer you can use arr[20][5]
or *(arr[20] + 5)
.

- 6,046
- 2
- 27
- 50
-
To keep track of the number of items in each inner array, either wrap the arrays in a struct that stores the size, or change it to `int **arr[100]` and use "null termination" on the values. – Martin Nov 12 '13 at 03:56
I believe the OP wants a single chunk of memory for the array, and is willing to fix one of the dimensions to get it. I frequently like to do this when coding in C as well.
We all used to be able to do double x[4][];
and the compiler would know what to do. But someone has apparently messed that up - maybe even for a good reason.
The following however still works and allows us to use large chunks of memory instead of having to do a lot of pointer management.
#include <stdio.h>
#include <stdlib.h>
// double x[4][];
struct foo {
double y[4];
} * x;
void
main(int ac, char * av[])
{
double * dp;
int max_x = 10;
int i;
x = calloc(max_x, sizeof(struct foo));
x[0].y[0] = 0.23;
x[0].y[1] = 0.45;
x[9].y[0] = 1.23;
x[9].y[1] = 1.45;
dp = x[9].y;
for (i = 0; i < 4; i++)
if (dp[i] > 0)
printf("%f\n", dp[i]);
}
The trick is to declare the fixed dimension in a struct. But keep in mind that the "first" dimension is the dynamic dimension and the "second" one is fixed. And this is the opposite of the old way ...
You will have to track the size of your dynamic dimension on your own - sizeof can't help you with that.
Using anonymous thingies you might even be able to git rid of 'y'.

- 936
- 13
- 17
Using a single pointer:
int *arr = (int *)malloc(r * c * sizeof(int));
/* how to access array elements */
for (i = 0; i < r; i++)
for (j = 0; j < c; j++)
*(arr + i*c + j) = ++count; //count initialized as, int count=0;
Using pointer to a pointer:
int **arr = (int **)malloc(r * sizeof(int *));
for (i=0; i<r; i++)
arr[i] = (int *)malloc(c * sizeof(int));
In this case you can access array elements same as you access statically allocated array.

- 2,771
- 3
- 28
- 43