0

I have two structs:

struct Parent {
   struct *Child child; // Pointer to a child
}

struct Child {
   int id;
}

I wish to init an array of 'Parent'

int size = 2;
struct Parent *parents = (struct Parent*) malloc(sizeof(struct Parent) * size);

this breaks when runs.

Any solution for this?

I want to initialize in such way:

struct Parent {
    struct *Child child = nullptr; // Points to null upon initialization.
}
Joon. P
  • 2,238
  • 7
  • 26
  • 53

4 Answers4

1

Don't mix C and C++; compile your C code using a C compiler, your C++ code using a (compatible) C++ compiler then link it using the linker, as you would with any other language.

Don't use malloc in C++; use new instead.

Don't cast malloc (or other void * values) in C.

Don't use int for size values in C; use size_t, instead.

Don't use nullptr in C; it doesn't exist. Use NULL instead.

Community
  • 1
  • 1
autistic
  • 1
  • 3
  • 35
  • 80
0

Since you mentioned you wanted C - you could use memset to initialize the memory (including the child pointer) to all zeroes.

size_t size = 2 * sizeof(struct Parent);
struct Parent* parents = (struct Parent*)malloc(size);
memset(parents, 0, size);

This will initialize the entire parents array to all zeroes. Otherwise it will be initialized to whatever happened to be in the memory when it was allocated.

The proper solution in C++ will be significantly different (use new[] and constructors to initialize the structs).

Chris Vig
  • 8,552
  • 2
  • 27
  • 35
0

In C I use calloc() instead of malloc().

Because, calloc() zeroes the memory returned, malloc() doesn't.

But if you want to zero memory after allocation, I personally prefer bzero() because it's unambiguous about it's purpose and takes one fewer argument than memset() does. I would generally use memset() can fill with non-zero values for that reason.

clearlight
  • 12,255
  • 11
  • 57
  • 75
  • Note that it is not guaranteed that a pointer representation consisting of all zero bytes is a valid null pointer. – Random832 Feb 03 '17 at 03:43
  • @Random832 Huh? I don't understand, calloc() doesn't zero the pointer, it zeroes the contents of the allocated memory – clearlight Feb 03 '17 at 03:46
  • Yes and the contents of the allocated memory include the `child` pointer. There's no guarantee that zeroing that is equivalent to setting it to null (or to a zero value for floating point types), even if it happens to be true on many implementations. – Random832 Feb 03 '17 at 03:47
  • @Random832 - OK, but that's no different from memset() or bzero(). In what situations is a zeroed pointer not a null pointer? Usually people test the validity of pointers in C by comparing them to NULL., and set them to NULL after freeing associated memory, so they can always know whether the pointer points to valid memory or not in their code – clearlight Feb 03 '17 at 03:49
  • @clearlight-- `NULL` is a macro, and does not _have_ to be represented by all bits zero according to the Standard. I have heard of implementations where this is in fact the case, but I don't think that it is common. Setting a pointer to `NULL` is safer than setting to `0`, just like terminating a string with `\0` is safer than terminating with `0`. – ad absurdum Feb 03 '17 at 03:58
  • So then the take away is, that for zeroing memory, `calloc()`, `bzero()`, and `memset(p, 0, n)` accomplish that but if you're allocating structs containing pointers and know your code might be run in some really weird environment, you could explicitly set each pointer to NULL afterwords. Good to know. Wonder if I'll ever need it? – clearlight Feb 03 '17 at 04:01
  • @DavidBowling - I'll do my best not to need it, let's put it that way :-) – clearlight Feb 03 '17 at 04:06
0

Chris Vig brings up a good point, but I think what you were tying to do was this

#include <iostream>
struct Child {
   int id;
};
struct Parent {
   struct Child* c ; // Pointer to a child
};
int main() {
    int size = 2;
    struct Parent *parents = (struct Parent*) malloc(sizeof(struct Parent) * size);
}
mreff555
  • 1,049
  • 1
  • 11
  • 21
  • 1
    Probably, but OP hasn't cleared up with actual code yet.... IAC, OP claims C code in comments, so `#include ` and `int main(void)`. Also, better C style to use `struct Parent *parents = malloc(sizeof(*parents) * size);` Better to avoid casting `malloc()` and to use identifier in `sizeof()`. – ad absurdum Feb 03 '17 at 03:45
  • @DavidBowling Could also be `struct Parent *parents = malloc(sizeof * parents * size);`. – RoadRunner Feb 03 '17 at 05:27