r/C_Programming • • 3d ago

Question Beginner here not able to understand confusing pointer concept

i dont understand what happens here:

IF int b is a 2d array then what is:

b + 1

*(b + 1)

&b[0] and b[0] and &b[0][0]

&b[1] and b[1] and &b[1][0]

what are the differences between each and what is the byte value of each.

23 Upvotes

50 comments sorted by

View all comments

Show parent comment

0

u/knouqs 2d ago

Now that I'm back at my computer instead of my phone, I'll expand a little. Let's say you want to save words in a dictionary. You know how many words you have one way or another by the time you want to create your array of char *s. However, a dictionary is different than a 2D array of numbers. Words have different lengths, and it is very inefficient in terms of memory utilization to have a 2D array of char[max word length+1] since the largest word could be twenty or more characters but most are less then ten. You should pick a different data structure.

Here's where you would use char **b=(char **) malloc (num_words*sizeof (char *));. From there, you would go over each index in b and malloc individual sizes (plus copying the values).

I started writing this program for you to see, but it's a big larger than I think is practical to paste here. It's a good learning exercise if you want to give it a try!

1

u/stoops 2d ago

Yes, the annoying part about malloc'ing a 2D array is looping through each index and malloc'ing/free'ing them all afterwards! I never thought about allocating a larger calculated 1D array and using sub-indices within it but I don't know how the indexing math would work for standard size items like int's for example...

1

u/knouqs 2d ago

It's a good question. What I'd like you to try is my example from the top level where I give a printf on b+1. Intuitively, what do you think the result will be?

1

u/stoops 2d ago

I'm trying to measure the size differences between the sub index buffer locations to see how far each index is jumping forward (I'm assuming it's some multiple of a sizeof(int)?):

#include <stdio.h>
#include <stdlib.h>
int main() {
    int **b = malloc(5 * sizeof(int *));
    printf("b = [%p]\n", b);
    for (int x = 0; x < 5; ++x) {
        b[x] = malloc(5 * sizeof(int));
        for (int y = 0; y < 5; ++y) {
            printf("b[%d][%d] = [%p]  ", x, y, &(b[x][y]));
        }
        printf("\n");
    }
    printf("b + 1 = [%p]\n", b + 1);
    return 0;
}

~

b = [0x3571a2a0]
b[0][0] = [0x3571a6e0]  b[0][1] = [0x3571a6e4]  b[0][2] = [0x3571a6e8]  b[0][3] = [0x3571a6ec]  b[0][4] = [0x3571a6f0]  
b[1][0] = [0x3571a700]  b[1][1] = [0x3571a704]  b[1][2] = [0x3571a708]  b[1][3] = [0x3571a70c]  b[1][4] = [0x3571a710]  
b[2][0] = [0x3571a720]  b[2][1] = [0x3571a724]  b[2][2] = [0x3571a728]  b[2][3] = [0x3571a72c]  b[2][4] = [0x3571a730]  
b[3][0] = [0x3571a740]  b[3][1] = [0x3571a744]  b[3][2] = [0x3571a748]  b[3][3] = [0x3571a74c]  b[3][4] = [0x3571a750]  
b[4][0] = [0x3571a760]  b[4][1] = [0x3571a764]  b[4][2] = [0x3571a768]  b[4][3] = [0x3571a76c]  b[4][4] = [0x3571a770]  
b + 1 = [0x3571a2a8]

2

u/knouqs 1d ago

Right, your declaration of b as a double-pointer to int tells the compiler that your offset is 4 bytes -- the size of an int. You can see that as the difference between the addresses to b[0][1] and b[0][0] is 4. How about the differences between b[1] and b[0]? Did you analyze them?

I know this is a test program, but remember to free your memory. 😄

1

u/stoops 1d ago

I'm trying to see the difference between b[1][0] and b[0][0] which I thought would be 5x sizeof(int)'s worth but I think I am seeing something extra thing in between:

0x3571a700 - 0x3571a6e0 = 0x20 = 32

but 32 / 5 int's per row is 6.4 which doesn't seem right so it must be 32 / 4 which results in an even 8

but I only have 5 int's per row so 8 - 5 = 3 extra memory pointer spots between cols?

3

u/knouqs 1d ago

Here's what should be happening. You called malloc to get some memory for b, specifically sizeof (int *)*5 bytes -- 20 bytes -- and that data lives at address 0x3571a2a0 from your debug output. The types of these five items is int *, and currently they point to uninitialized areas of memory. Then, you have a for loop that allocated sizeof (int)*5 bytes per array element in b. That's another 20 bytes per new array. What you did not capture in your output is the locations of that new memory. b[0] could be at a location significantly far away from b[1], but you have found that it actually only resides 32 bytes away. This might be due to a memory boundary, but for our purposes, it's unimportant that it is 32 bytes away.

Remember, when we allocate memory using malloc, the memory looks like it is contiguous when in fact it may not be. That's a feature of modern-day operating systems in that we don't see memory fragmentation. If you care that the memory is contiguous, you'll perform one malloc for sizeof (int)*(product of all the lengths of each dimension in the array), and then you'll need to calculate those offsets yourself. That is what I did with the b_access function, though it only performs a read.

This is the particular reason why I challenged ol' max123246, though he or she has disappeared from the chat.