user3728181
user3728181

Reputation: 19

assign multi-dimension array to another

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...

Upvotes: 0

Views: 69

Answers (2)

R Sahu
R Sahu

Reputation: 206607

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.

Upvotes: 0

yizzlez
yizzlez

Reputation: 8805

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.

Upvotes: 1

Related Questions