how to allocate memory for a two dimensional array dynamically please need code for that one

Dani AI

Generated

@ srinath.sec asked how to allocate a 2‑D array at runtime. Several valid approaches were suggested: showed per‑row allocation, showed a typed contiguous array, and rightly flagged the bad examples from . Below are two practical, safe patterns you can apply that avoid the common pitfalls shown in the thread.

A contiguous data block with a small array of row pointers (fast, cache friendly; rows accessible as rows[i][j]):

size_t rows = R, cols = C;
int *block = malloc(rows * cols * sizeof *block);
if (!block) { /* handle allocation failure */ }

int **rows_ptr = malloc(rows * sizeof *rows_ptr);
if (!rows_ptr) { free(block); /* handle */ }

for (size_t i = 0; i < rows; ++i)
    rows_ptr[i] = block + i * cols;

/* use rows_ptr[i][j] */

/* cleanup */
free(rows_ptr);
free(block);

A flat buffer with manual indexing (simpler, no extra pointer array):

int *data = malloc(rows * cols * sizeof *data);
if (!data) { /* handle */ }

/* access element (i,j): data[i * cols + j] */

/* cleanup */
free(data);

Safety notes and tips: always check malloc/calloc return values; guard against overflow when multiplying rows * cols (e.g., verify cols == 0 || rows <= SIZE_MAX / cols); free in the correct order (for separate-row allocations free each row then the pointer array; for the single block free the block then the pointer array or vice versa as appropriate); prefer sizeof *ptr idiom and do not cast malloc in C; include <stdlib.h>. For malloc/calloc behavior and guarantees see the documentation on dynamic allocation malloc and calloc.

Recommended Answers

All 4 Replies

how to allocate memory for a two dimensional array dynamically please need code for that one

you need a pointer to pointer like

int **myArray;

then, first allocate for the int ** like

myArray = (int**)malloc(sizeof(int*) * HOW_MANY);

and then for the int * , for each row in the column, you need to allocate memory again, perhaps in a for loop.

for (i = 0; i < HOW_MANY; i++)
{
     myArray[i] = (int *)malloc(HOW_MANY_ROW * sizeof(int));
}

now, your array is created on the runtime, dynamically

I am sure this helps...

enum { N=20, M=50 };
int(*a)[M] = malloc( sizeof( int[M] ) * N ) ; // C
int(*b)[M] = new int[N][M] ; // C++

May be this can help

void main()
{
int n,i,*p;
printf("\nHow many elements\n");
scanf("%d",&n);
p=(int *)malloc(sizeof(int));
printf("\nEnter %d Elements\n",n);
for(i=0;i<n;i++)
scanf("%d",&p);
}

Now uve alloted memory for an array dynamically and stored elements into the dynamic array

>May be this can help
No, I don't think so.

>void main()
http://www.cprogramming.com/faq/cgi-bin/smartfaq.cgi?id=1043284376&answer=1044841143

>p=(int *)malloc(sizeof(int));
You're allocating exactly 1 element in a 1-dimensional array. That is not what the original poster was asking for.

>scanf("%d",&p);
Oh great, so now you're trying to fill up this array's elements which you never allocated. Goodbye memory, hello segmentation fault.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.