Gamestudio Links
Zorro Links
Newest Posts
Zorro 2.70
by jcl. 09/29/25 09:24
optimize global parameters SOLVED
by dBc. 09/27/25 17:07
ZorroGPT
by TipmyPip. 09/27/25 10:05
assetHistory one candle shift
by jcl. 09/21/25 11:36
Plugins update
by Grant. 09/17/25 16:28
AUM Magazine
Latest Screens
Rocker`s Revenge
Stug 3 Stormartillery
Iljuschin 2
Galactic Strike X
Who's Online Now
1 registered members (TipmyPip), 18,633 guests, and 5 spiders.
Key: Admin, Global Mod, Mod
Newest Members
krishna, DrissB, James168, Ed_Love, xtns
19168 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Problem with struct array #187451
03/07/08 12:35
03/07/08 12:35
Joined: Jan 2008
Posts: 49
Sweden
K
Kenchu Offline OP
Newbie
Kenchu  Offline OP
Newbie
K

Joined: Jan 2008
Posts: 49
Sweden
This will generate an error.

Code:
#include <default.c>

typedef struct
{
var member1;
var member2;
} astruct;

astruct array[10];

function main()
{
astruct tmp;
tmp.member1 = 10;
tmp.member2 = 20;

array[0] = tmp;
}



Quote:

Error in main line 17: machine code generator: can not translate EQU:STRUCT@17::STRUCT@17

< array[0] = tmp; >




Why wont it work? I tried using pointers and allocation on heap and that worked, but id rather do it this way. Just that it doesnt work...

Last edited by Kenchu; 03/07/08 12:41.
Re: Problem with struct array [Re: Kenchu] #187452
03/07/08 13:10
03/07/08 13:10
Joined: Jul 2001
Posts: 6,904
H
HeelX Offline
Senior Expert
HeelX  Offline
Senior Expert
H

Joined: Jul 2001
Posts: 6,904
astruct array[10]; will already allocate 10 instances of astruct. You should have to use memcpy to copy the contents of your local created struct instance. You should use "=" only when you work with pointers.

Re: Problem with struct array [Re: HeelX] #187453
03/07/08 15:57
03/07/08 15:57
Joined: Jan 2008
Posts: 49
Sweden
K
Kenchu Offline OP
Newbie
Kenchu  Offline OP
Newbie
K

Joined: Jan 2008
Posts: 49
Sweden
Hm thats weird, since you dont have to do that in either C or C++. memcpy is for memory management on the heap. Besides, memcpy is not even mentioned in the manual.

Anyway, I tried using memcpy for this and it didnt work. :/ I think it, just like in C and C++, doesnt operate on the stack.

What Ive written above does work in regular C btw.

Re: Problem with struct array [Re: Kenchu] #187454
03/07/08 17:25
03/07/08 17:25
Joined: Jan 2004
Posts: 2,013
The Netherlands
E
Excessus Offline
Expert
Excessus  Offline
Expert
E

Joined: Jan 2004
Posts: 2,013
The Netherlands
What you wrote in your original post doesn't work in C or C++ either (not without overloading the assignment operator for your struct, anyway).

Think about the implications of what you expect this code to do. If using assignment on an arbitrary struct instance would copy it over to the assigned-to struct, it would copy pointers too. What if your struct contains a pointer to some variable that should be allocated per-struct. When you copy that struct, it would copy the pointer, NOT the data pointed-to, so now you have two struct instances sharing the same pointee. Sometimes this is what you want, often it's not. In any case, you must specify what you want to do, so assignment of structs is not implemented in C.

You should copy the elements member by member, or with memcpy as HeelX pointed out.

Could you show the code where you tried to use memcpy?

BTW, memcpy works on the heap as well as on the stack in C++ (haven't tested with Lite-C, but would be strange if it behaved differently).

Re: Problem with struct array [Re: Excessus] #187455
03/07/08 17:57
03/07/08 17:57
Joined: Jan 2008
Posts: 49
Sweden
K
Kenchu Offline OP
Newbie
Kenchu  Offline OP
Newbie
K

Joined: Jan 2008
Posts: 49
Sweden
Quote:

What you wrote in your original post doesn't work in C or C++ either (not without overloading the assignment operator for your struct, anyway).




It does work. The default behavior for = when it comes to structs and classes is to copy its members. I made a small program and checked it out (even though I was 99% sure before, lite-c confused me ). You can find it here:
http://web.telia.com/~u87309837/forum/C.zip

Quote:

Think about the implications of what you expect this code to do. If using assignment on an arbitrary struct instance would copy it over to the assigned-to struct, it would copy pointers too. What if your struct contains a pointer to some variable that should be allocated per-struct. When you copy that struct, it would copy the pointer, NOT the data pointed-to, so now you have two struct instances sharing the same pointee. Sometimes this is what you want, often it's not. In any case, you must specify what you want to do, so assignment of structs is not implemented in C.

You should copy the elements member by member, or with memcpy as HeelX pointed out.




In this case I want the members to be copied.
And btw, memcpy would cause the same problem with pointers as when using =, wouldnt it? Its not like it magically would know when the user wants it to really copy a pointer or whats stored at the pointed space.

Quote:

Could you show the code where you tried to use memcpy?

BTW, memcpy works on the heap as well as on the stack in C++ (haven't tested with Lite-C, but would be strange if it behaved differently).




Code:
#include <default.c>

typedef struct
{
int member1;
int member2;
} astruct;

//astruct* array[10];
astruct array[10];

function main()
{
// astruct* tmp = malloc(sizeof(astruct));
astruct tmp;
tmp.member1 = 10;
tmp.member2 = 20;

// memcpy(array[0], tmp);
memcpy(array[0], tmp, sizeof(astruct));
// memcpy(&array[0], &tmp);?
}



EDIT:
Found the problem with memcpy. Turns out it had a third argument.


Last edited by Kenchu; 03/07/08 18:12.
Re: Problem with struct array [Re: Kenchu] #187456
03/07/08 18:11
03/07/08 18:11
Joined: Jan 2004
Posts: 2,013
The Netherlands
E
Excessus Offline
Expert
Excessus  Offline
Expert
E

Joined: Jan 2004
Posts: 2,013
The Netherlands
Ah I see, you're right about struct assignment in C, I wasn't aware of this. I guess Lite-C is not standards conformant here then.

The correct use of memcpy would be: memcpy(&array[0], &tmp, sizeof(astruct));

Quote:

And btw, memcpy would cause the same problem with pointers as when using =, wouldnt it? Its not like it magically would know when the user wants it to really copy a pointer or whats stored at the pointed space.



Yes that's right, I didn't want to imply otherwise. But personally I think that since there is no "better" choice for the = behaviour, it should not be defined. Memcpy can then be used explicitly for a shallow copy, while a programmer defined function can be used for a deep copy. But hey, I'm not in the C standards commision, so who cares Thanks for clarifying this.

Re: Problem with struct array [Re: Excessus] #187457
03/07/08 18:13
03/07/08 18:13
Joined: Jan 2008
Posts: 49
Sweden
K
Kenchu Offline OP
Newbie
Kenchu  Offline OP
Newbie
K

Joined: Jan 2008
Posts: 49
Sweden
Yeah I took a closer look at a C reference i found.
As you say, theres a third argument...

Still puzzled that it doesnt work to use =. It should work imo.


Moderated by  HeelX, Lukas, rayp, Rei_Ayanami, Superku, Tobias, TWO, VeT 

Gamestudio download | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1