3 registered members (NewbieZorro, TipmyPip, 1 invisible),
19,045
guests, and 8
spiders. |
Key:
Admin,
Global Mod,
Mod
|
|
|
Problem with struct array
#187451
03/07/08 12:35
03/07/08 12:35
|
Joined: Jan 2008
Posts: 49 Sweden
Kenchu
OP
Newbie
|
OP
Newbie
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]
#187454
03/07/08 17:25
03/07/08 17:25
|
Joined: Jan 2004
Posts: 2,013 The Netherlands
Excessus
Expert
|
Expert
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
Kenchu
OP
Newbie
|
OP
Newbie
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
Excessus
Expert
|
Expert
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.
|
|
|
|