0

I am new to C++ and need some help. I have the following code:

struct Force {
    float X[10];
    float Y[10];
    float Z[10];
};

struct Measurement{
    char serial_number[30];
    struct Force F1;
    struct Force F2;
 };

How should I properly allocate the following?

struct Measurement meas

The problem is that struct Force force works fine; however, when I try to define struct Measurement meas then I get "Unhandled exception" error!

jankos
  • 896
  • 1
  • 11
  • 16
  • First of all it would help us to answer your question better if you show us _how_ you use the `meas` variable when you get the "crash". Secondly, the first thing you always should to when getting a "crash", is to run your program in a debugger. It will help you locate the place of the crash, and also let you examine variables to help you see why it might have crashed. – Some programmer dude Aug 10 '12 at 06:41
  • Why do you insist on changing the tags to C++ if you say you're learning C in the question? – R. Martinho Fernandes Aug 10 '12 at 06:43
  • This piece of the code looks ok. Where is the "Dynamically Allocating" part? – Bo Persson Aug 10 '12 at 06:43

3 Answers3

2

As I saw in your question, you are using C, so here is solution for C.

Wherever you want to have instance of structure Measurement, simply type:

struct Measurement meas;

and you will be able to access your structure elements as:

meas.F1.X and so on...

And if you wish to have dynamic allocation(i.e. at run time) then simply use malloc/calloc as follows

struct Measurement *meas = (struct Measurement *)malloc(sizeof(struct Measurement));

Doing so, you will have to access your structure elements as:

meas->F1.X and so on...
jsist
  • 5,223
  • 3
  • 28
  • 43
1

Technically it works like you wrote it, but struct word is unnecessary on members (actually generates warning but works.)

struct Force {
    float X[10];
    float Y[10];
    float Z[10];
};

struct Measurement {
    char serial_number[30];
    Force F1;
    Force F2;
};

Then in function use like this:

Measurement somevar;
somevar.F1.Y = 999;

Now the proper way to do this (and save stack) is to use pointers.

struct Measurement {
    char serial_number[30];
    Force* F1;
    Force* F2;
};

And then:

Measurement* m = new Measurement;
if (m) {
    m->F1 = new Force;
    m->F2 = new Force;
}

After using you have to delete all pointers to avoid memory leaks:

delete m->F1;
delete m->F2;
delete m;

There is another approach. Using:

struct Force {
    float X[10];
    float Y[10];
    float Z[10];
};

struct Measurement {
    char serial_number[30];
    Force F1;
    Force F2;
};

You can allocate with malloc some amount of memory and treat it as struct (did not have time to test it, but I use that approach many times).

Measurement* m = (Measurement*)malloc(sizeof( size in bytes of both structs ));
// zero memory on m pointer

// after use
free(m);

That's all.

dr4cul4
  • 258
  • 4
  • 14
  • Thanks. This works for me (the first approach). I will now go into details now. Thank you all guy. You have helped me a lot! – jankos Aug 10 '12 at 06:51
  • You should really learn the concept of smart pointers, so you don't have to `delete` by hand, because you will likely forget about it, or forget to catch exceptions and cleanup properly. Most modern C++ code do not contain a single `delete` statement. – BatchyX Aug 10 '12 at 06:56
  • Thank you for the advice on Smart pointers. I would be happy if you could show me the use of smart pointers on the above example. – jankos Aug 10 '12 at 07:05
0

C:

struct Measurement *meas;
meas=(struct Measurement *) malloc(sizeof(Measurement));
              ^                             ^                         
              |                             |                 
              |                             |                
          this is shape                  this is the space allocated

C++:

Measurement *meas;
meas=new Measurement;
huseyin tugrul buyukisik
  • 11,469
  • 4
  • 45
  • 97