0
char** surname;
surname = (char**) malloc(size*sizeof(char*));

char* middle_initial;
middle_initial = (char*) malloc(size*sizeof(char));

for(int i = 0; i<5;i++){       
  surname[i] = (char*) malloc(surname_max*sizeof(char));
  middle_initial[i] = *(char*) malloc(middle_max*sizeof(char)); // Please focus on this line
}

surname is a double char pointer, and it makes sense that surname[i] is the i-th pointer pointed to by surname.

However, middle_initial confuses me. Is middle initial[i] the i-th character pointer? If so, why does malloc returns value call for a dereferenced *(char *) instead of (char *) during debugging?

** Follow up question** I would like middle_initial [i] to be 6 char pointers each having the capacity to point to 1 character. What can I modify above to do such that?

JackW
  • 49
  • 6
  • At a minimum, it's a memory leak. – Oliver Charlesworth Aug 26 '15 at 05:47
  • `middle_initial[i]` is the i-th character and is not a pointer. But not sure what that line of code is trying to do. It's assigning a random character value to `middle_initial[i]` since `malloc` does not intialise the allocated memory. And it doesn't keep track of the allocated memory pointer so it can't be freed later. – kaylum Aug 26 '15 at 05:49
  • For the line of code, my intent is to have a character pointer to 1 column of characters. So, middle_initial[i] is a char pointer to one character, middle_initial[i+1] is char pointer to another character – JackW Aug 26 '15 at 05:54
  • [No need/Don't cast the result of malloc in C](http://stackoverflow.com/q/605845/995714). And `sizeof(char)` is always 1, no need to use it either – phuclv Aug 26 '15 at 06:35

1 Answers1

3

Dereferencing effectively "reduces the type's pointer count by one".

Every piece of code there is okay, except for...

middle_initial[i] = *(char*) malloc(middle_max*sizeof(char));

What does this line is supposed to do apart from undefined behaviour? We don't now! As the type of middle_initial is char*, the dereference by index effectively references a char object. Then, we have the illogical expression. A char* of middle_max chars is allocated on the heap, but... that * at the front dereferences that allocated memory (memory that can be unitialized, BTW...). Then, such a garbage value is assigned to the forementioned char in middle_initial. The result of this loop is a middle_initial containing garbage chars comming from unrecoverably leaked memory.

And, of course, if size < 5, UB everywhere whenever dereferencing!

3442
  • 8,248
  • 2
  • 19
  • 41
  • Thanks. I have a second question. I would like middle_initial [i] to be 6 char pointers each having the capacity to point to 1 character. What can I modify above to do such that? – JackW Aug 26 '15 at 06:29
  • @JackW: Did you meant *I would like `middle_initial` to be 6 `char*` each having the capacity to point to 1 character*, or *I would like `middle_initial[i]` to be 6 `char*` each having the capacity to point to 1 character*? Depending on the answer, use respectively this for `middle_initial`: `char *middle_initial[6]`. Or this for `middle_initial[i]`: `char *(*middle_initial)[6]`. Say us if you have more questions :). – 3442 Aug 26 '15 at 06:50