0

i have these structures:

struct tcFrameConfig {
    int           NameF; 
    int           NameP;
    int           NameH;
    unsigned __int8 Length;
    unsigned int  Duration; };

struct tcFrameStimulus {
    short Key; };

struct tcFrame {
    tcFrameConfig      Config;
    tcFrameStimulus* Stimulus; };

int main() {
    tcFrame Frame1;
    tcFrame Frame2;

    Frame1.Config.NameF = 0;
    Frame1.Config.NameP = 0;
    Frame1.Config.NameH = 0;
    Frame1.Config.Length = 4;
    Frame1.Config.Duration = 100;

    Frame1.Stimulus = new tcFrameStimulus[Frame1.Config.Length];
    Frame1.Stimulus[0] = 0;
    Frame1.Stimulus[1] = 1;
    Frame1.Stimulus[2] = 40;
    Frame1.Stimulus[3] = 43;

ok i initialized Frame1; now... if i do:

Frame2 = Frame1;
return 0; }

it works, but i dont think its right, what is the proper way?, and if Frame1 was actually an dynamic array of tcFrame?

update....

from

struct tcFrame {
    tcFrameConfig      Config;
    tcFrameStimulus* Stimulus; };

i went to

struct tcFrame {
    tcFrameConfig      Config;
    std::vector<tcFrameStimulus> Stimulus;  };

compiler says

error: 'struct tcFrame' has no member named 'Stimulus'|

had some tests on this code

int main() {
tcFrame Frame1;
tcFrame Frame2;

Frame1.Config.NameF = 0;
Frame1.Config.NameH = 0;
Frame1.Config.NameP = 0;
Frame1.Config.Length = 4;
Frame1.Config.Duration = 100;

Frame1.Stimulus = new tcFrameStimulus[Frame1.Config.Length];
Frame1.Stimulus[0].Key = 0;
Frame1.Stimulus[1].Key = 1;
Frame1.Stimulus[2].Key = 2;
Frame1.Stimulus[3].Key = 3;

Frame2 = Frame1;

Frame1.Config.NameF = 1;
Frame1.Config.NameH = 1;
Frame1.Config.NameP = 1;
Frame1.Config.Length = 4;
Frame1.Config.Duration = 200;

Frame1.Stimulus = new tcFrameStimulus[Frame1.Config.Length];
Frame1.Stimulus[0].Key = 1;
Frame1.Stimulus[1].Key = 2;
Frame1.Stimulus[2].Key = 3;
Frame1.Stimulus[3].Key = 4;

cout << Frame1.Config.NameF  << endl;
cout << Frame1.Config.NameH  << endl;
cout << Frame1.Config.NameP  << endl;
cout << Frame1.Config.Length  << endl;
cout << Frame1.Config.Duration  << endl;
cout << Frame1.Stimulus[0].Key  << endl;
cout << Frame1.Stimulus[1].Key  << endl;
cout << Frame1.Stimulus[2].Key  << endl;
cout << Frame1.Stimulus[3].Key  << endl;

cout << Frame2.Config.NameF  << endl;
cout << Frame2.Config.NameH  << endl;
cout << Frame2.Config.NameP  << endl;
cout << Frame2.Config.Length  << endl;
cout << Frame2.Config.Duration  << endl;
cout << Frame2.Stimulus[0].Key  << endl;
cout << Frame2.Stimulus[1].Key  << endl;
cout << Frame2.Stimulus[2].Key  << endl;
cout << Frame2.Stimulus[3].Key  << endl;

return 0; }

and the Frames had different values...

6
  • 1
    Why don't you think it's right? What do you expect to happen, and what actually happened that you didn't want? Commented Jun 11, 2014 at 1:22
  • by simply using the = operator, does it create the Frame2.stimulues array, or it's allocating the values in another adress? Commented Jun 11, 2014 at 1:25
  • This is not C code, so the [c] tag is inappropriate. main may not be declared to return void; it always has return type int. Commented Jun 11, 2014 at 1:25
  • sorry, i just quicly typed the code, im a delphi programer and im learning c++ now, my question is, can i assign any array by simply using the = operator? Commented Jun 11, 2014 at 1:29
  • I don't see any multidimensional arrays in this code Commented Jun 11, 2014 at 2:08

2 Answers 2

1

Heard of the Rule of Three?

It looks like you should take advantage of the object oriented part of C++ and make tcFrame a full fledged object (hint class). After this you can create appropriate constructors and destructors, along with a copy constructor and a overloaded copy assignment.

...But why are you using dynamic memory anyways? C++ provides std::vector dynamic arrays with no headache.

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

Comments

0

You have to allocate memory for Frame2.Stimulus and copy the contents of Frame1.Stimulus to it.

Frame2.Stimulus = new tcFrameStimulus[Frame1.Config.Length];
Frame2.Stimulus[0] = Frame1.Stimulus[0];
Frame2.Stimulus[1] = Frame1.Stimulus[1];
Frame2.Stimulus[2] = Frame1.Stimulus[2];
Frame2.Stimulus[3] = Frame1.Stimulus[3];

Otherwise, Frame2 and Frame1 point to the same memory. If you modify one, it will affect the other.

Since you are going to use c++, it will be a lot better to use the containers from the standard library, such as std::vector, std::array. Otherwise, you will be dealing with memory related problems.

For your case, you can use:

struct tcFrame 
{
   tcFrameConfig      Config;
   std::vector<tcFrameStimulus> Stimulus;
};

That way, you can just use:

Frame2 = Frame1;

without worrying about memory allocation, memory deallocation, assignment of data from one list to another, etc.

6 Comments

i tried struct tcFrame { tcFrameConfig Config; std::vector<tcFrameStimulus> Stimulus; }; it says the struct has no member names Stimulus
If you can posted an update, with the updated code and copy and paste the compiler error message, it will be helpful.
I think you should leave your original code. Add the new code and the compiler error messages at the end of the post.
like that? its actually my 1st post in stackoverflow :s
Sounds like you forgot to #include <vector> in your code.
|

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.