1

Can someone help me get a better understand on how memory-allocation works like? I would appreciate if someone could go through step by step (hopefully with pictures) what happens in this code:

char a[3][4] = {"xy", "abcd", "!?"};
char (*b)[4], *c;
b = a + 1;
c = *a + 1;

I looked around but found nothing explaining this thoroughly, thanks!

Edit: I would be grateful if someone could explain it using memoryblocks, ex, [x][y][][a][b][c][d][]

Fjondor
  • 39
  • 7
  • 1
    It can be considered to not do anything, since it has no side-effects ... – unwind Jan 08 '15 at 14:46
  • 1
    Shall the oversized string "abcd", *five* chars in length including the terminator, stuffed into an undersized *four*-char array be accounted for in whatever answers are provided? – WhozCraig Jan 08 '15 at 14:56
  • 1
    @WhozCraig Nope, that's not a problem. `char foo[3] = "foo";` is well-defined and there is no terminator added if the literal is exactly the length of the array. – unwind Jan 08 '15 at 15:04
  • @unwind A C thing? C++ will puke on this. Egads. – WhozCraig Jan 08 '15 at 15:04
  • @WhozCraig Yes, standard C. – unwind Jan 08 '15 at 15:05
  • 1
    @unwind Thanks very much for the info. Yuck. Like *that* isn't a terminator-search overreach just waiting in the wings. – WhozCraig Jan 08 '15 at 15:07
  • @WhozCraig It's pretty awesome, for instance when you have a fixed-length string-like value which doesn't need a terminator (like the `"\x7fELF"` ELF magic identifier, and so on). – unwind Jan 08 '15 at 15:08

3 Answers3

2
char a[3][4] = {"xy", "abcd", "!?"};

The first command allocates a 3 by 4 matrix on the stack and initializes the elements of the 2-dimensional array with some chars.

char (*b)[4], *c;

Reserves a pointer to character array sized 4 named b on the stack. Additionally it creates a second char pointer named c.

b = a + 1;

You store in the pointer b the starting address of the array a + 1 that means the address of the second element (second row of 4 characters).

c = *a + 1;

You dereference a, producing the pointer to first element of a (an element of a is a char array of size 4), and add 1 to that pointer - thats the second letter of the first entry.

andreashager
  • 667
  • 3
  • 9
  • 2
    *Reserves a char pointer named b on the stack 4 elements with 4 elements*: What does it mean? – haccks Jan 08 '15 at 15:10
  • 1
    `b` is really a "pointer to array of 4 characters". I am not 100% positive, but I think that sizeof(*b) would be 4, not 1. –  Jan 08 '15 at 16:37
1
#include <stdio.h>

int main(int argc, char *argv[])
{
  // the second dimension should 5
  // since "abcd" is 4 chars + null terminator
  char a[3][5] = {"xy", "abcd", "!?"};
  char (*b)[5], *c;
  b = a + 1;
  c = *a + 1;

  // b = a + 1, thus it points to the second entry of a array
  printf("%s\n", b[0]);

  // c = *a + 1, thus it points where a points to, plus one,
  // thus in the second letter of a's first entry
  printf("%c\n", *c);
  return 0;
}

EDIT about your edit

a -> [x][y][0][0][0][a][b][c][d][0][!][?][0][0][0]
b -> [a][b][c][d][0][!][?][0][0][0]
c -> [y]

With zeroes I represent the null terminator. Notice that in your example, it's not vital to have one, but I think that is always a good thing to have null terminated strings in C. That's why I changed your code a bit.

Why not only 1 null terminator per string in a? See this answer.

Community
  • 1
  • 1
gsamaras
  • 71,951
  • 46
  • 188
  • 305
0

The variable a is an array of arrays of char. The variable b is a pointer to an array of char, c is a pointer to `char.

The variable a is initialized at declaration. The variable b is then assigned to point to the second array of a (i.e. a[1]), and c is assigned to the second letter of a[0].

As for the assignment of b, the expressions a[1] and *(a + 1) are equivalent.

For the assignment of c, a by itself is a pointer to the first element of the array, i.e. it's a pointer a[0] (a is the same as &a[0]). By dereferencing a you get the first entry of a, that is you get a[0]. Adding one to a[0] give you the second character of a[0], i.e. a[0][1].

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621