-2

So this is a simple assignment that works pretty well:

int **a = malloc(sizeof(int*));
int *b;
a[0] = b;

But when I try to do the same inside a struct via a pointer to struct:

typedef struct jojo
{
  int **a ;
}jojo;

Lets say I allocate for a pointer to jojo called alpha a lot of space like for 100 integers...

jojo *alpha = malloc(sizeof(int)*100);
int *b;
alpha->a[0] = b;

Here there is a segmentation fault. I am completely lost, so I am asking for help. Thanks.

Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
badabum
  • 73
  • 5
  • One problem: `malloc(sizeof(int)*100)` -> `malloc(sizeof(jojo)*100)` – Jabberwocky Aug 24 '17 at 10:27
  • 1
    `alpha` is initialized, but `a` field isn't initialized, even less allocated – Jean-François Fabre Aug 24 '17 at 10:27
  • 1
    So [how many questions using invalid pointers](https://stackoverflow.com/questions/37549594/crash-or-segmentation-fault-when-data-is-copied-scanned-to-an-uninitialized-po) do we need per hour? – Antti Haapala -- Слава Україні Aug 24 '17 at 10:30
  • @MichaelWalz `malloc(sizeof(jojo)*100)` -> `malloc(sizeof(*alpha)*100)` – unalignedmemoryaccess Aug 24 '17 at 10:30
  • In any case, this sounds like an XY problem: **who needs pointers to pointers to integers in an array**? You say yourself: "space for 100 integers" but you actually don't have integers there, but pointers to integer objects... could you enlighten us about the actual use case, the actual [X in your problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) – Antti Haapala -- Слава Україні Aug 24 '17 at 10:34
  • `alpha` points to a `jojo`, but `jojo` has inside of it a pointer to `int` pointer (`**a`) which you didn't initialize but ar etrying to access. – lurker Aug 24 '17 at 10:40
  • @AnttiHaapala since the space for integers is not qualittively diferent from the space for any other type, I used it just for getting a massive amount of space to experiment. Obviously the example I put is an instance of my real problem in wich I deal with more complex data, but since here is the root of the trouble itś fine like this. – badabum Aug 24 '17 at 10:41

3 Answers3

1

When you do

jojo *alpha = malloc(sizeof(int)*100);

you only allocate memory for the structure (and much more than is needed for the structure) and not somewhere for the members inside to point. What's worse is that malloc doesn't initialize the memory it allocates, its contents will be indeterminate.

That last part means the pointer a will be seemingly random and dereferencing it will lead to undefined behavior.

To solve your problem you need to allocate memory for the structure first, then allocate memory for the pointer inside the structure:

jojo *alpha = malloc(sizeof *alpha);  // Allocate memory for the structure
alpha->a = malloc(100 * sizeof *alpha->a);  // Allocate memory for a 100 pointers to `int`
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
0

a is not pointing anywhere. You only reserved memory for structure. You have to reserve memory for a too.

int *b;

//Reserve memory for 100 structures
jojo *alpha = malloc(sizeof(*alpha)*100);

//Reserve memory for a pointer on first structure
alpha->a = malloc(sizeof(*alpha->a)*1);
alpha->a[0] = b;

Let me just tell you that if b has no value, it is undefined behavior if you dereference it.

Also, please check how I used arguments in sizeof keyword for detecting size of memory to allocate.

unalignedmemoryaccess
  • 7,246
  • 2
  • 25
  • 40
0

First of all,

  jojo *alpha = malloc(sizeof(int)*100);

is bad, because, alpha is not related to sizeof (int) * 100 and once it changes, you'll be in trouble. In case you meant (or, actually you want) to write

 jojo *alpha = malloc(sizeof(jojo)*100);

or better,

 jojo *alpha = malloc(sizeof(*alpha)*100);  // no dependency on hard-coded type

That said, you have allocated memory only for alpha, not for it's members!

You also need to make the member a to point to some valid memory location before you can dereference it.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261