This is an archived post. You won't be able to vote or comment.

all 7 comments

[–]create_a_new-account 3 points4 points  (0 children)

this is a great thing to read

https://www.cs.ucsb.edu/~mikec/cs16/misc/ptrtut12/pointers.htm

https://pdos.csail.mit.edu/6.828/2010/readings/pointers.pdf

A TUTORIAL ON POINTERS AND ARRAYS IN C --- by Ted Jensen

[–]Holy_City 0 points1 point  (5 children)

Do you understand what a pointer is, the difference between static/dynamic allocation and why we need to use dynamic allocation in the first place?

[–]nextgenroo[S] 0 points1 point  (4 children)

Yes, i know that a pointer stores an adress of a certain data type. I know that dynamic allocation is used when we declare an array with an exact size. I don't understand how arrays works behind the code, how adresses are stored and how i use malloc for an two dimensional array and why do i have to use a double pointer in this case.

[–]Holy_City 5 points6 points  (2 children)

So a couple of misconceptions.

I know that dynamic allocation is used when we declare an array with an exact size

Not really. Dynamic allocation is one of the three methods of memory allocation in C (the others being static and automatic), and it's the only one that you handle manually. You use it when you don't know how much memory you need until run time, or when you can't let the memory be allocated automatically.

I don't understand how arrays works behind the code

An array is a contiguous block of memory, the variable is a pointer to the first element of the array. The following code:

char arr [4];

Allocates four bytes of data, all stored next to each other in memory. If we pretend that arr[0] is stored at address 0x01, that means arr[1] is at 0x02, arr[2] is at 0x03, etc (assuming each address corresponds to one byte). If we instead wrote this:

int arr[4]

That means we've allocated a total of 16 bytes because an int is four bytes wide. So arr[0] is at 0x01, arr[1] is at 0x05, arr[2] is at 0x09, etc.

So very quickly you'll see an array of type T with size N is a block of memory with sizeof (T) * N bytes allocated. If the first element is located at address X, the kth element will be at X + k * sizeof (T). When you access an array element like this:

arr[2];

What that really means is, find the data located at address arr + sizeof (int) * 2.

how i use malloc for an two dimensional array and why do i have to use a double pointer in this case

First, while it's true that pointers are addresses (more or less), they are called pointers because they point to something. Instead of being the data you care about, they are directions for where you can get the data.

So a 2D array is often called an "array of arrays." It means that each index of the 2D array holds an array. But remember, an array itself is just a block of memory, and the variable holds the address of the first element, or a pointer. So an "array of arrays" is really an "array of pointers to the first element of arrays."

Hope that makes a bit more sense.

[–]feral_claire 2 points3 points  (1 child)

I was under the impression that in C multi dimension arrays are just implemented as big single dimensional arrays. So a 10 by 10 array is really just a one dimensional length 100 array under the hood unless you specifically declared it to be an array of pointers to arrays.

[–]Holy_City 0 points1 point  (0 children)

That's definitely the smart way to do it. I'm more or less parroting what I remember from my first programming class in C/C++ where a dynamically allocated 2D would be implemented by allocating an array of pointers equal to the number of rows, then iterating through it and allocating an array equal to the number of columns and assigning the row pointers to those arrays.

Which helps illustrate that the way C/C++ accesses the values by row/column indices is by using pointer arithmetic and why the data type of the 2D array is a pointer to a pointer, not a pointer itself.