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

  • 1
    Why don't you think it's right? What do you expect to happen, and what actually happened that you didn't want? – user3386109 Jun 11 '14 at 01:22
  • by simply using the = operator, does it create the Frame2.stimulues array, or it's allocating the values in another adress? – user3728181 Jun 11 '14 at 01: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`. – aschepler Jun 11 '14 at 01: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? – user3728181 Jun 11 '14 at 01:29
  • I don't see any multidimensional arrays in this code – M.M Jun 11 '14 at 02:08
  • i didnt want to post too much code, but i have another 2 structures, each is an array of another – user3728181 Jun 11 '14 at 02:10

2 Answers2

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.

Community
  • 1
  • 1
yizzlez
  • 8,757
  • 4
  • 29
  • 44
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.

R Sahu
  • 204,454
  • 14
  • 159
  • 270