Dynamically Allocate A 2D Array | C Programming Tutorial

preview_player
Показать описание
Рекомендации по теме
Комментарии
Автор

What a clear and straightforward explanation! thanks for your time Kevin!

salomonchambi
Автор

Thanks for the in depth explanation of the whole process, respect.

D.Wapher
Автор

This is literally God's work that you're doing for newbies, Mister!!

Devva
Автор

one can also use int (*array)[5] = malloc(5 * 5 * sizeof(int)); to directly allocate a block of memory for 2d array of size 5 * 5 on the heap segment. If we do it this way, the allocated memory block is consecutive with respect to the program's address space. If we use the double-pointer method shown in this video, the address of pointers in the 1st dimension of the array is consecutive, and the memory address of elements in each row is consecutive, but the address of different rows may not form a consecutive block in the program's address space.

TL-fesi
Автор

Don't forget that to free the array correctly. Doing free(array) is wrong! Because it deallocates only the array of pointers to arrays. The arrays themselfs are now unaccessible and you will have a memory leakage. The correct way is first to deallocate each array, i.e. do for (i=0;i<3;i++) free(array[i]); and only then free(array).

hotpil
Автор

Perfect. Exacly what I needed. Thank you so much <3

wscamel
Автор

I finally found this video, thank you!!

mrawesome
Автор

You have to throw a little code to it, but one could handle 2D arrays "the Forth way". Which means allocating just the array and let the runtime figure out the nitty gritty details. You can make as many 2D arrays you want this way - and with a bit of effort you can even resize them. The overhead is just 2 elements; the first and the second. You might be surprised at the lvalue, but it is legal. It is a dereferenced pointer expression. The *#define* is just there to remove a bit of the ugliness. Don't judge too hard on a quick hack ;-)

#include <stdlib.h>
#include <stdio.h>

#define A2D(a, b, c) *(array2d((a), (b), (c)))

int* make2darray (int rows, int cols)
{
int* a = (int*) calloc (rows * cols + 2, sizeof(int));
if (a == NULL) return (NULL);
a [0] = rows; a [1] = cols; return (a);
}

int* array2d (int* a, int row, int col)
{
if ((row < a[0]) && (col < a[1])) return (a + 2 + col + a[1] * row);
return (a + 2); /* just limit possible corruption, avoid segfaults */
}

int main (char** argv, int argc)
{
int* a;

if ((a = make2darray (3, 5)) == NULL) return (1);
A2D(a, 2, 3) = 5;
printf ("%d\n", A2D(a, 2, 3));
return (0);
}

HansBezemer
Автор

Wow what a explanation 😲 was stuck in a question but now its clear 🎉

yogeshchauhan
Автор

In my limited experience, most of the time only the first dimension of a dynamic 2D int array needs to change. That being the case, memory management is much easier when you use a pointer to array of integers instead of a pointer to a pointer of ints.

#define DIM2 5
{
int i;
int array_count = 5;
int (*array)[DIM2];
int (*temp)[DIM2]

array = malloc((array_count * DIM2) * sizeof(int)); /* one call */
/* do work with 5 int arrays */
temp = realloc(array, ((array_count + 5) * DIM2) * sizeof(int));
array = temp;
/* do more work with 5 more int arrays */
free(array) /* everything free in one call */
}

Brock-Landers
Автор

Very simple and easy to understand explanation - many thanks.

fifaham
Автор

0:45 array[1] is a pointer the the FIRST ELEMENT of the array you said it was pointing to. &array[1] points to the entire array

Whoeveriam
Автор

Such a great explanation, been referencing many books but this video has made it clear ❤👌💕

vykuntamyogasimha
Автор

Thank you for that video. It's verry helpful

lampartzjardu
Автор

Thanks for the great video! Just wondering what editor you use for C in this video? It looks nice and tidy!

nicolaikystaubayev
Автор

you are great ms portfolio, i really thank you

mouadflp
Автор

14:31 should be "delete array; array = NULL;", otherwise you can still add new data albeit having a different address.

AnalogDude_
Автор

I was waiting for this video for so so long when I wanted to learn about dynamically allocating 2D array few days ago I was not able to find video from your channel and got sad

GodsOnlyMonster
Автор

OMG thank you so much!! I was struggling with 2d arrays in my assignment, you got a sub!
also are you coding in vscode? How to get plain white text background 😲

wc
Автор

Ok, but how do you initialize? With arrays, I can do:
int array[2][3] = {{1, 2, 3}, {4, 5, 6}};
How would I do this with pointers and malloc()?
Especially, if I only want to store the data in the allocated memory, that I can "free()" lateron.

HLubenow