0

I am trying to dynamically allocate an array of struct in another struct here is the code segment

I don't get any syntax errors but I get segmentation fault once I try to enter str1

could someone explain why is there a segmentation fault, and what happens in memory in the dynamic allocation in such situation

struct A {
 string str1;
 string str2;
}
struct B {
 int count;
 A* A_array;
}
void GetB (B** b)
{
 *b = (B*) malloc(1*sizeof(B));
 cout << "Enter count";
 cin >> (**b).count;
 (**b).A_array = (A*) malloc((**b).count*sizeof(A));
 cout << "Enter str1";
 cin >> (**b).A_array[0].str1;
 cout << "Enter str2";
 cin >> (**b).A_array[0].str2;
}
int main(){
 B* b;
 GetB(&b);
}
asked Mar 8, 2013 at 17:19
8
  • 5
    what a horrible mix of C and C++ Commented Mar 8, 2013 at 17:20
  • 1
    Is there a reason why you're using malloc instead of new? Generally, if you're programming in C++, you want to actually use C++. Commented Mar 8, 2013 at 17:21
  • 2
    Start by deciding whether you're you're writing C or C++. If you're writing C, don't use cin or cout and don't cast the return from malloc. If you're writing C++, don't use malloc at all, and use std::vector instead of your home-rolled imitation. Commented Mar 8, 2013 at 17:21
  • sorry about that I am using c++ but I my problem is with the malloc so I thought people with c or cpp experience can help with it that is why I but both on the tags Commented Mar 8, 2013 at 17:23
  • 2
    I'll repeat: if you're using C++, don't use malloc. When writing C++, it's best to forget that it exists at all. Commented Mar 8, 2013 at 17:24

2 Answers 2

6

The reason you are getting a crash is because string str1; string str2; do not get constructed properly.

And they are not constructed properly because malloc only allocates memory and doesn't call the constructors.

Which is what operator new is for in C++.

Therefore, as highlighted in comments:

  1. Never ever use malloc to allocate non-POD objects.
  2. Even better, never use malloc in c++ at all.
  3. And better still, never ever use manually allocated arrays, use std::vector instead
answered Mar 8, 2013 at 17:25
Sign up to request clarification or add additional context in comments.

5 Comments

+1 for that last paragraph (well, and the whole answer as well).
I replaced the malloc with new in both lines and it worked
@Roola But you do understand that changing those two mallocs to news is only a quick and dirty fix for your code and that there are more and larger issues to it? You'll make it a lot easier for your future self if you learn now how to do things the C++ way instead of continuing with your current style until you hit a project complex enough where it fails.
@us2012 thank you for your comment, could you tell me what exactly are you refereeing to? I don't understand what you mean.
@Roola For example: Your method that produces a B still has signature void GetB (B** b) when it could simply be B GetB() avoiding the pointers entirely. You are still using A* instead of vector<A>. You may want to explore whether your classes should have constructors, or whether their members should be private.
1

Expanding on my comments, this would be an equivalent of your current program using some more idiomatic C++. I have deliberately kept the structure as close to your original as possible, but there are of course other issues to think about, like whether your classes should have constructors or private members.

struct A {
 string str1;
 string str2;
};
struct B {
 int count;
 vector<A> A_vec;
};
B GetB ()
{
 B myB;
 cout << "Enter count";
 cin >> myB.count;
 A a;
 cout << "Enter str1";
 cin >> a.str1;
 cout << "Enter str2";
 cin >> a.str2;
 myB.A_vec.push_back(a);
 return myB;
}
int main(){
 B b(GetB());
}
answered Mar 8, 2013 at 21:16

2 Comments

Thank you for the elaboration. I noticed that you didn't use pointers, do you recommend staying away from pointers in c++
@Roola That would be too general. Pointers are not inherently bad, and some things (implementing trees or linked lists) simply require pointers! In my opinion, a reasonable guideline is: Don't use raw pointers where ownership of an object is implied. But that will likely not mean a lot to you at this point, so really, you need to get a good and modern book on C++ and learn this stuff from scratch.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.