2

I read tons of tutos and snippets, but I still don't understand why I get a segfault with this:

int fun(char **p) {

  int i;

  *p = malloc(2);
  *p[0]=10;
  *p[1]=20; // segfault NULL pointer

  printf("fun()/n");
  for (i=0; i<2; i++)
   printf("%d ",*p[i]);
}

int main(int argc, const char *argv[])
{
  char* buffer;
  int i;

  fun(&buffer);

  printf("main()\n");

  for (i=0; i<2; i++)
   printf("%d ",buffer[i]);

  return 0;
}

In gdb, it gives:

Program received signal EXC_BAD_ACCESS, Could not access memory.
Reason: KERN_INVALID_ADDRESS at address: 0x0000000000000000
0x0000000100000dea in fun (p=0x7fff5fbffab0) at test.c:10
10    *p[1]=20;
(gdb) p *p[0]
$1 = 10 '\n'
(gdb) p *p[1]
Cannot access memory at address 0x0
(gdb)

I have seen a lot of similar snippets, but there is surely something I am deeply misunderstanding.

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
phocean
  • 31
  • 2
  • 3
    I feel strangely guarded when someone learns from "tons of tutos and snippets"... – Kerrek SB Nov 15 '12 at 17:07
  • 1
    You should check your `malloc` call for errors. – squiguy Nov 15 '12 at 17:07
  • @KerrekSB: Very interesting. Who are you and do you happen to know? – phocean Nov 15 '12 at 19:10
  • 1
    @phocean: Well, for starters I've seen a good share of "tutos and snippets" in the wild, and similar questions; and moreover I know a bit about the complexity of C++ and wonder if one can tackle it if spelling out "tutorial" is taxing the attention span already... :-) (But do have a look at our recommended book list!) – Kerrek SB Nov 15 '12 at 19:15
  • @KerrekSB This is a C question, not that C is a good language either to learn from disparate bits of Internet resources. – Pascal Cuoq Sep 08 '13 at 13:49

2 Answers2

4

You mean (*p)[1]. What you said is *(p[1]).

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
  • That was it. But sad to read your other comment. You didn't respect me without knowing. And where is the crime to begin in C, not understanding something and ask a question? I spent days on a shitty syntax issue. So what? Thanks anyway. – phocean Nov 15 '12 at 19:13
  • @phocean: Hey, no offense, and best of luck! Spend some time on SO if you like, I'm sure it'll be a rewarding experience. You'll be answering questions yourself in no time. – Kerrek SB Nov 15 '12 at 19:17
0

try returning the address of the same data type of the variable at the LHS. malloc() by default returns a void value. p = (char) malloc(2); this way compiler knows how many bytes it has to move the pointer to fetch new variable.

Nitish
  • 11
  • 1
  • 1
    This question was answered and accepted a long time ago. Why add another? – Morten Kristensen Sep 08 '13 at 13:31
  • This answer would need to be fixed before it could be terrible and unhelpful. The function `malloc()` does not return `void` and casting to `char` the result of `malloc()` is awful. You may have meant `void*` and `char*`, and then it is still a bad idea: http://stackoverflow.com/a/605858/139746 . And even then this does not answer the question. – Pascal Cuoq Sep 08 '13 at 13:53