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.
}
5
  • C or C++, the answer will differ. Commented Feb 3, 2017 at 3:24
  • @user975989 c please Commented Feb 3, 2017 at 3:25
  • 1
    This code will not compile. Missing semicolons after struct declarations; struct *Child; // Pointer to a child is meaningless, should be struct Child *field;; need to forward declare struct Child. Please provide minimal reproducible example. Commented Feb 3, 2017 at 3:32
  • @DavidBowling sry that was a typo for this post. Even with that fixed, malloc breaks Commented Feb 3, 2017 at 3:33
  • You should fix the many typos in the question post so that we can find what the actual problem is. Commented Feb 3, 2017 at 3:37

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

OK, a bit terse, but very well referenced.
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);
}

2 Comments

Probably, but OP hasn't cleared up with actual code yet.... IAC, OP claims C code in comments, so #include <stdio.h> 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().
@DavidBowling Could also be struct Parent *parents = malloc(sizeof * parents * size);.
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).

Comments

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.

7 Comments

Note that it is not guaranteed that a pointer representation consisting of all zero bytes is a valid null pointer.
@Random832 Huh? I don't understand, calloc() doesn't zero the pointer, it zeroes the contents of the allocated memory
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 - 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-- 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.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.