Author Topic: arrays - what the heck?  (Read 8508 times)

Balazs

  • Guest
arrays - what the heck?
« on: August 14, 2006, 12:57:49 pm »
Consider this code:

Code
struct Test {
int a[4][4];
};

int main()
{
Test t1, t2;
t1 = t2;
return 0;
}

It compiles fine. I ask: what the heck? It happily assigns the array, and not as pointer but it actually COPIES the whole thing correctly (I checked it)! But if I do this:

Code
int main()
{
int a1[4][4], a2[4][4];
a1 = a2;
return 0;
}

I get this error:

error: ISO C++ forbids assignment of arrays

So, can someone clarify me here what the hell is happening? If ISO C++ forbids array assignment, then why does it allow it, if it is inside a struct (or class)? Is it only GCC's behaviour or is it valid C++?

Thanks, and sorry for being a bit off-forum.
--
Greets,
B.

EDIT: I just read, that this is not a general programming board. Well, I already posted, so I apologize, but I promise to keep myself from posting things like this in the future.... IF i get the answer :)
« Last Edit: August 14, 2006, 01:09:54 pm by Balazs »

Offline MortenMacFly

  • Administrator
  • Lives here!
  • *****
  • Posts: 9694
Re: arrays - what the heck?
« Reply #1 on: August 14, 2006, 01:08:24 pm »
error: ISO C++ forbids assignment of arrays
In the first case you assign a struct in the second you assign an array. It isn't suprising at all.

Thanks, and sorry for being a bit off-forum.
Allthough you are sorry for this you could have also asked in a C/C++ related forum. Why didn't you?

With regards, Morten.
Compiler logging: Settings->Compiler & Debugger->tab "Other"->Compiler logging="Full command line"
C::B Manual: https://www.codeblocks.org/docs/main_codeblocks_en.html
C::B FAQ: https://wiki.codeblocks.org/index.php?title=FAQ

Balazs

  • Guest
Re: arrays - what the heck?
« Reply #2 on: August 14, 2006, 01:11:55 pm »
In the first case you assign a struct in the second you assign an array. It isn't suprising at all.
Yeah, but if you look closely, you'll see, that there is an array INSIDE that struct, and it gets assigned, as if you did a memcpy. Why is that? Is it supposed to happen like that?

Offline MortenMacFly

  • Administrator
  • Lives here!
  • *****
  • Posts: 9694
Re: arrays - what the heck?
« Reply #3 on: August 14, 2006, 01:22:55 pm »
Yeah, but if you look closely, you'll see, that there is an array INSIDE that struct, and it gets assigned, as if you did a memcpy. Why is that? Is it supposed to happen like that?
I have looked closely and I still insist of what I said.
Another hint from me: Think of the struct as class.
And another hint: If you want more detailed answers ask in a C/C++ only related forum. ;-)
Compiler logging: Settings->Compiler & Debugger->tab "Other"->Compiler logging="Full command line"
C::B Manual: https://www.codeblocks.org/docs/main_codeblocks_en.html
C::B FAQ: https://wiki.codeblocks.org/index.php?title=FAQ

Balazs

  • Guest
Re: arrays - what the heck?
« Reply #4 on: August 14, 2006, 01:27:35 pm »
Another hint from me: Think of the struct as class.
And another hint: If you want more detailed answers ask in a C/C++ only related forum. ;-)
Cmon, what's good in holding back the answer??? :roll:
I know that the struct is a class (or vice versa, how we look at it), but that still doesn't explain why the default assignment operator generated by the compiler for that class allows assigning an array, when it is generally not allowed.

Offline MortenMacFly

  • Administrator
  • Lives here!
  • *****
  • Posts: 9694
Re: arrays - what the heck?
« Reply #5 on: August 14, 2006, 01:47:51 pm »
Well I tried to be as polite as possible but it seems you don't understand:
The forum description says: "This is NOT a general programming board.".

Why do we act so strict? Because if we shall answer all question of any beginner in any language and/or SDK that C::B supports we can finish working on C::B and only anser general programming questions. Please understand:
1.) We don't have time for that.
2.) Truly C::B related question might get lost or at least are answered in delay which is not what we want.
3.) And (more important) there are a *lot* other forums dedicated to questions like yours.

Try starting in the languages.c and/or languages.c++ newsgroup.

With best regards, Morten.
Compiler logging: Settings->Compiler & Debugger->tab "Other"->Compiler logging="Full command line"
C::B Manual: https://www.codeblocks.org/docs/main_codeblocks_en.html
C::B FAQ: https://wiki.codeblocks.org/index.php?title=FAQ

Balazs

  • Guest
Re: arrays - what the heck?
« Reply #6 on: August 14, 2006, 01:56:59 pm »
All these lots of posts could have been avoided with a simple 2-line straight answer for my question, shish... :roll:
Allright, but please consider my PM, and tell me what you think.
« Last Edit: August 14, 2006, 02:04:37 pm by Balazs »

Offline MortenMacFly

  • Administrator
  • Lives here!
  • *****
  • Posts: 9694
Re: arrays - what the heck?
« Reply #7 on: August 14, 2006, 02:10:45 pm »
All these lots of posts could have been avoided with a simple 2-line straight answer for my question, shish... :roll:
I would have answered in a short way if the anser would have been a two-liner. But: To answer your question in a *good* way a whole paragraph is required with references to programming literatur. You would need to understand how instantiaten works and what a compiler actually builds out of a struct and/or variable. For now just believe what the compile tells you: You cannot assign arrays but structs.
(I'll read the PM in a minute.)
Compiler logging: Settings->Compiler & Debugger->tab "Other"->Compiler logging="Full command line"
C::B Manual: https://www.codeblocks.org/docs/main_codeblocks_en.html
C::B FAQ: https://wiki.codeblocks.org/index.php?title=FAQ

Offline killerbot

  • Administrator
  • Lives here!
  • *****
  • Posts: 5491
Re: arrays - what the heck?
« Reply #8 on: August 14, 2006, 02:16:02 pm »
you struct or class does not have :
a) copy constructor
b) assignment operator

--> compiler will make a bitwise copy/assignment.

It seems the compiler is smart enough to know is a 2-dim array, and not just a pointer. But if the standard does guarantee this ,I am not sure.
On first sight I would say it is dangerous and bad code.

Offline mandrav

  • Project Leader
  • Administrator
  • Lives here!
  • *****
  • Posts: 4315
    • Code::Blocks IDE
Re: arrays - what the heck?
« Reply #9 on: August 14, 2006, 02:16:30 pm »
If you don't explicitely define a copy constructor for your class/struct, then the built-in one will be used which makes a byte-to-byte copy of the original. Happy now?

What's so hard to understand about "This is NOT a general programming board."?
Nothing to discuss about it: our rules and you have to respect them...
Be patient!
This bug will be fixed soon...

Balazs

  • Guest
Re: arrays - what the heck?
« Reply #10 on: August 14, 2006, 02:28:02 pm »
Thanks for your answers! And yes, I'm happy, as always :) (you should try that too!)
BTW, I promise not to bother you again. I understood why this doesn't belong here.

Have a nice day!