Thread: RE: [GD-General] not sure where to post this question... bit C++ implementation specific....
Brought to you by:
vexxed72
From: CAVEY G. <GER...@sg...> - 2004-07-07 08:20:59
|
>its a bit C++ specific, but does anyone know of a way of getting the >"meaning" of this to compile... > >const float ropeLength =3D 12.5f; >const float knotSpace =3D 3.3f; >int array[ ropeLength / knotSpace ]; > >... its sorta meaning we have to break our own coding guidlines.... = (no >#defines unless truly necessary) Hi AFAIK the number of tokens in an array must be an integer . An easy solution could be to compute the division in a temp variable then see if it give us a modulo ,if yes increase by one our result=20 and finally use that result to create the array. GC. ************************************************************************= * Ce message et toutes les pieces jointes (ci-apres le "message") sont confidentiels et etablis a l'intention exclusive de ses destinataires. Toute utilisation ou diffusion non autorisee est interdite.=20 Tout message electronique est susceptible d'alteration.=20 SG Asset Management et ses filiales declinent toute responsabilite au = titre de ce message s'il a ete altere, deforme ou falsifie. D=E9couvrez l'offre et les services de SG Asset Management sur le site www.sgam.fr=20 ******** This message and any attachments (the "message") are confidential and intended solely for the addressees. Any unauthorised use or dissemination is prohibited.=20 E-mails are susceptible to alteration. =20 Neither SG Asset Management nor any of its subsidiaries or affiliates = shall be liable for the message if altered, changed or falsified.=20 ************************************************************************= * |
From: CAVEY G. <GER...@sg...> - 2004-07-07 09:11:10
|
>You've got to admit this is a bit of a stinker, and i can't see why = the >c++ langauge cannot cope with this simple premise. > >At the moment, the best compromise we can come up with is to #ifdef >VALUE #error#else#define VALUE 12.5f#endif >... do stuff with the constant ... >#undef VALUE > >we've got a good thing going on about not polluting the namespace, and >#defines can wreak havok on anything that hits them. but why dont you just use some temp integers filled with the abs values to create your array? It will only add few instructions. Instead of trying to destroy the wall ,just jump over :) GC. ************************************************************************= * Ce message et toutes les pieces jointes (ci-apres le "message") sont confidentiels et etablis a l'intention exclusive de ses destinataires. Toute utilisation ou diffusion non autorisee est interdite.=20 Tout message electronique est susceptible d'alteration.=20 SG Asset Management et ses filiales declinent toute responsabilite au = titre de ce message s'il a ete altere, deforme ou falsifie. D=E9couvrez l'offre et les services de SG Asset Management sur le site www.sgam.fr=20 ******** This message and any attachments (the "message") are confidential and intended solely for the addressees. Any unauthorised use or dissemination is prohibited.=20 E-mails are susceptible to alteration. =20 Neither SG Asset Management nor any of its subsidiaries or affiliates = shall be liable for the message if altered, changed or falsified.=20 ************************************************************************= * |
From: Jorrit T. <jor...@uz...> - 2004-07-07 09:35:59
|
CAVEY GERARD wrote: > >but why dont you just use some temp integers filled with the abs values >to create your array? >It will only add few instructions. >Instead of trying to destroy the wall ,just jump over :) > > How would that help? In C++ you simply cannot do this: int a = 6; int array[a]; It is not possible to allocate arrays with variables (const or not const). Greetings, |
From: Crosbie F. <cr...@cy...> - 2004-07-07 10:03:21
|
> From: Jorrit Tyberghein > How would that help? In C++ you simply cannot do this: >=20 > int a =3D 6; > int array[a]; >=20 > It is not possible to allocate arrays with variables (const=20 > or not const). Many apologies for the following hack, but once upon a time it was = needed. #if 0 #define CLASS_INT(N) _SClassInt##N #define CLASS_INT_STRUCT(N) typedef char CLASS_INT(N) [N] CLASS_INT_STRUCT(2); CLASS_INT_STRUCT(3); CLASS_INT_STRUCT(4); =09 #undef CLASS_INT_STRUCT #define NUMBER(NN) CLASS_INT(NN) #else #define NUMBER(NN) char [NN] #endif #define _2 NUMBER(2) #define _3 NUMBER(3) #define _4 NUMBER(4) #define INT_(NUM) (sizeof(NUM)) Maybe it could be used again? e.g. int array[INT_(NUMBER(6))]; But, there MUST be a better answer these days eh? Templates perhaps? |
From: Richard F. <gd...@th...> - 2004-07-07 12:23:27
|
i seem to be able to safely issue declarations for int initialised arrays... e.g. const int ropeLength = 12.5f; const int knotSpace = 3.3f; int array[ (int)( ropeLength / knotSpace ) ]; is perfectly valid. it looks like it is quite literally the "const float"s that are at fault. ------------------ On the other hand I may be talking complete drivel. ------------------ > -----Original Message----- > From: gam...@li... > [mailto:gam...@li...] On > Behalf Of Jorrit Tyberghein > Sent: 07 July 2004 10:36 AM > To: gam...@li... > Subject: Re: [GD-General] not sure where to post this > question... bit C++ implementation specific.... > > > CAVEY GERARD wrote: > > > > >but why dont you just use some temp integers filled with the > abs values > >to create your array? It will only add few instructions. > >Instead of trying to destroy the wall ,just jump over :) > > > > > How would that help? In C++ you simply cannot do this: > > int a = 6; > int array[a]; > > It is not possible to allocate arrays with variables (const > or not const). > > Greetings, > > > > ------------------------------------------------------- > This SF.Net email sponsored by Black Hat Briefings & > Training. Attend Black Hat Briefings & Training, Las Vegas > July 24-29 - > digital self defense, top technical experts, no vendor pitches, > unmatched networking opportunities. Visit www.blackhat.com > _______________________________________________ > Gamedevlists-general mailing list > Gam...@li... > https://lists.sourceforge.net/lists/listinfo/gamedevlists-general > Archives: http://sourceforge.net/mailarchive/forum.php?forum_id=557 > > --- > Incoming mail is certified Virus Free. > Checked by AVG anti-virus system (http://www.grisoft.com). > Version: 6.0.716 / Virus Database: 472 - Release Date: 05/07/2004 > > --- Outgoing mail is certified Virus Free. Checked by AVG anti-virus system (http://www.grisoft.com). Version: 6.0.716 / Virus Database: 472 - Release Date: 05/07/2004 |
From: Noel L. <nl...@co...> - 2004-07-07 13:52:26
|
On Wednesday 07 July 2004 05:23 am, Richard Fabian wrote: > i seem to be able to safely issue declarations for int initialised > arrays... > > e.g. > > const int ropeLength = 12.5f; > const int knotSpace = 3.3f; > int array[ (int)( ropeLength / knotSpace ) ]; > > is perfectly valid. > > it looks like it is quite literally the "const float"s that are at > fault. If I recall correctly, const floats were treated very differently from const ints in VC++. Const floats even generated their own bit of code to be executed when they were initialized, whereas const ints became true consts, so it doesn't surprise me that they can't be used as part of the array size. As some other people pointed out earlier, any reason not to use an std::vector<> or do dynamic allocation? If you don't like having to delete the memory, you can always use boost::scoped_array: boost::scoped_array<int> array (new[ (int)( ropeLength / knotSpace ) ]); --Noel Games from Within http://www.gamesfromwithin.com |
From: Richard F. <gd...@th...> - 2004-07-07 14:06:22
|
> If I recall correctly, const floats were treated very > differently from const > ints in VC++. Const floats even generated their own bit of code to be > executed when they were initialized, whereas const ints > became true consts, > so it doesn't surprise me that they can't be used as part of > the array size. > > As some other people pointed out earlier, any reason not to use an > std::vector<> or do dynamic allocation? > > If you don't like having to delete the memory, you can always use > boost::scoped_array: > boost::scoped_array<int> array (new[ (int)( ropeLength / > knotSpace ) ]); its actually more of a "didn't want to have to dereference everytime i needed to access the array". speed issue... The best fix in our situation will be to use defines as far as i can see. --- Outgoing mail is certified Virus Free. Checked by AVG anti-virus system (http://www.grisoft.com). Version: 6.0.716 / Virus Database: 472 - Release Date: 05/07/2004 |
From: Noel L. <nl...@co...> - 2004-07-07 15:19:20
|
On Wednesday 07 July 2004 07:05 am, Richard Fabian wrote: > its actually more of a "didn't want to have to dereference everytime i > needed to access the array". speed issue... You do realize that, at least on a PC, it's just as fast accessing an array on the stack than one that was dynamically allocated, right? Actually, if you're accessing more than one int at the time, the dynamically-allocated one might be faster because of better memory alignment. And if you're talking about the extra indirection added by boost::scoped_array... I admit I haven't measured, but I doubt very, very much you would notice any difference unless all your program did was run in a for loop accessing array elements. > The best fix in our situation will be to use defines as far as i can > see. Personally, I see nothing wrong with simple defines for things like that. It's when you start doing complicated macros that the pre-processor can become more of a problem than it solves. --Noel Games from Within http://www.gamesfromwithin.com |
From: Richard F. <gd...@th...> - 2004-07-07 16:11:21
|
what about the dereference of the global pointer to get the address of the array? if the array was a global, then surely the address of the array would be in the instructions that access it? Would this not go so far as to compile out any const pointer arithmetic? On the PS2 and other memory issue intense machines, might not such pointer dereferencing be a real issue? ------------------ On the other hand I may be talking complete drivel. ------------------ > -----Original Message----- > From: gam...@li... > [mailto:gam...@li...] On > Behalf Of Noel Llopis > Sent: 07 July 2004 04:19 PM > To: gam...@li... > Subject: Re: [GD-General] not sure where to post this > question... bit C++ implementation specific.... > > > On Wednesday 07 July 2004 07:05 am, Richard Fabian wrote: > > > its actually more of a "didn't want to have to dereference > everytime i > > needed to access the array". speed issue... > > You do realize that, at least on a PC, it's just as fast > accessing an array on > the stack than one that was dynamically allocated, right? > Actually, if you're > accessing more than one int at the time, the > dynamically-allocated one might > be faster because of better memory alignment. > > And if you're talking about the extra indirection added by > boost::scoped_array... I admit I haven't measured, but I > doubt very, very > much you would notice any difference unless all your program > did was run in a > for loop accessing array elements. > > > The best fix in our situation will be to use defines as far > as i can > > see. > > Personally, I see nothing wrong with simple defines for > things like that. It's > when you start doing complicated macros that the > pre-processor can become > more of a problem than it solves. > > > --Noel > Games from Within > http://www.gamesfromwithin.com > > > > ------------------------------------------------------- > This SF.Net email sponsored by Black Hat Briefings & > Training. Attend Black Hat Briefings & Training, Las Vegas > July 24-29 - > digital self defense, top technical experts, no vendor pitches, > unmatched networking opportunities. Visit www.blackhat.com > _______________________________________________ > Gamedevlists-general mailing list > Gam...@li... > https://lists.sourceforge.net/lists/listinfo/gamedevlists-general > Archives: http://sourceforge.net/mailarchive/forum.php?forum_id=557 > > --- > Incoming mail is certified Virus Free. > Checked by AVG anti-virus system (http://www.grisoft.com). > Version: 6.0.716 / Virus Database: 472 - Release Date: 05/07/2004 > > --- Outgoing mail is certified Virus Free. Checked by AVG anti-virus system (http://www.grisoft.com). Version: 6.0.716 / Virus Database: 472 - Release Date: 05/07/2004 |
From: brian s. <pud...@po...> - 2004-07-07 18:03:33
|
AFAIK const floats (and const doubles) are treated differently because any compile-time arithmetic you do with them might not give the same results as runtime arithmetic. The results you get depend on what state you've buggered the FPU into (on both the compiling machine and the target machine). So VC will be cautious and defer any division of const floats until runtime, meaning that you can't use it in an array declaration. --brian Noel Llopis wrote: >On Wednesday 07 July 2004 05:23 am, Richard Fabian wrote: > > >>i seem to be able to safely issue declarations for int initialised >>arrays... >> >>e.g. >> >> const int ropeLength = 12.5f; >> const int knotSpace = 3.3f; >> int array[ (int)( ropeLength / knotSpace ) ]; >> >>is perfectly valid. >> >>it looks like it is quite literally the "const float"s that are at >>fault. >> >> > >If I recall correctly, const floats were treated very differently from const >ints in VC++. Const floats even generated their own bit of code to be >executed when they were initialized, whereas const ints became true consts, >so it doesn't surprise me that they can't be used as part of the array size. > >As some other people pointed out earlier, any reason not to use an >std::vector<> or do dynamic allocation? > >If you don't like having to delete the memory, you can always use >boost::scoped_array: >boost::scoped_array<int> array (new[ (int)( ropeLength / knotSpace ) ]); > > > > |
From: Andras B. <bn...@ma...> - 2004-07-07 15:40:48
|
> How would that help? In C++ you simply cannot do this: > > int a = 6; > int array[a]; > > It is not possible to allocate arrays with variables (const or not const). I dunno, this compiles fine on MSVC++: const int a = 6; int array[a]; This also works fine: const int a = static_cast<const int>(5.4f / 3.6f); int array[a]; But then why it's not possible to do: const float a = 14.5f; const float b = 2.3f; const int c = static_cast<const int>(a / b); int array[c]; ? Andras |
From: <phi...@pl...> - 2004-07-07 16:38:32
|
I'm not convinced that this isn't just a quirk of VC6. Cheers, Phil |
From: CAVEY G. <GER...@sg...> - 2004-07-07 09:52:29
|
> >How would that help? In C++ you simply cannot do this: > > int a =3D 6; > int array[a]; > >It is not possible to allocate arrays with variables (const or not = const). Guys How can you explain the following code compiles under = mingw32(IDE=3Ddevc++ 4987)? Do you use visual studio ? Anyway i ll it a try on solaris. int main(int argc, char *argv[]) { =20 const float ropeLength =3D 12.5f; const float knotSpace =3D 3.3f; =20 int array[ int(ropeLength / knotSpace) ]; =20 int a=3D6; int arraybis[a]; =20 return 0; } ************************************************************************= * Ce message et toutes les pieces jointes (ci-apres le "message") sont confidentiels et etablis a l'intention exclusive de ses destinataires. Toute utilisation ou diffusion non autorisee est interdite.=20 Tout message electronique est susceptible d'alteration.=20 SG Asset Management et ses filiales declinent toute responsabilite au = titre de ce message s'il a ete altere, deforme ou falsifie. D=E9couvrez l'offre et les services de SG Asset Management sur le site www.sgam.fr=20 ******** This message and any attachments (the "message") are confidential and intended solely for the addressees. Any unauthorised use or dissemination is prohibited.=20 E-mails are susceptible to alteration. =20 Neither SG Asset Management nor any of its subsidiaries or affiliates = shall be liable for the message if altered, changed or falsified.=20 ************************************************************************= * |
From: Jorrit T. <jor...@uz...> - 2004-07-07 09:56:36
|
CAVEY GERARD wrote: > >Guys > >How can you explain the following code compiles under mingw32(IDE=devc++ >4987)? >Do you use visual studio ? > > gcc supports this with a C++ extension. If you enable -Wpedantic then gcc will probably give an error or warning for that. Greetings, |
From: Dr A. P. <aj...@eu...> - 2004-07-07 10:03:17
|
I think this comes from C99 complience - not standard yet. Cheers, Andrew > -----Original Message----- > From: gam...@li... > [mailto:gam...@li...]On Behalf Of > CAVEY GERARD > Sent: 07 July 2004 10:54 > To: 'gam...@li...' > Subject: RE: [GD-General] not sure where to post this question... bit > C++ implementation specific.... > > > > > >How would that help? In C++ you simply cannot do this: > > > > int a = 6; > > int array[a]; > > > >It is not possible to allocate arrays with variables (const or > not const). > > Guys > > How can you explain the following code compiles under mingw32(IDE=devc++ > 4987)? > Do you use visual studio ? > Anyway i ll it a try on solaris. > > int main(int argc, char *argv[]) > { > > const float ropeLength = 12.5f; > const float knotSpace = 3.3f; > > int array[ int(ropeLength / knotSpace) ]; > > int a=6; > int arraybis[a]; > > return 0; > } > > > ************************************************************************* > Ce message et toutes les pieces jointes (ci-apres le "message") sont > confidentiels et etablis a l'intention exclusive de ses destinataires. > Toute utilisation ou diffusion non autorisee est interdite. > Tout message electronique est susceptible d'alteration. > SG Asset Management et ses filiales declinent toute > responsabilite au titre > de ce message s'il a ete altere, deforme ou falsifie. > > Découvrez l'offre et les services de SG Asset Management sur le site > www.sgam.fr > > ******** > > This message and any attachments (the "message") are confidential and > intended solely for the addressees. > Any unauthorised use or dissemination is prohibited. > E-mails are susceptible to alteration. > Neither SG Asset Management nor any of its subsidiaries or > affiliates shall > be liable for the message if altered, changed or falsified. > > ************************************************************************* > > > > > > ------------------------------------------------------- > This SF.Net email sponsored by Black Hat Briefings & Training. > Attend Black Hat Briefings & Training, Las Vegas July 24-29 - > digital self defense, top technical experts, no vendor pitches, > unmatched networking opportunities. Visit www.blackhat.com > _______________________________________________ > Gamedevlists-general mailing list > Gam...@li... > https://lists.sourceforge.net/lists/listinfo/gamedevlists-general > Archives: > http://sourceforge.net/mailarchive/forum.php?forum_idU7 > > _____________________________________________________________________ > This e-mail is confidential and may be privileged. It may be > read, copied and used only by the intended recipient. No > communication sent by e-mail to or from Eutechnyx is intended to > give rise to contractual or other legal liability, apart from > liability which cannot be excluded under English law. > > This message has been checked for all known viruses by Star > Internet delivered through the MessageLabs Virus Control Centre. > > www.eutechnyx.com Eutechnyx Limited. Registered in England No: 2172322 _____________________________________________________________________ This e-mail is confidential and may be privileged. It may be read, copied and used only by the intended recipient. No communication sent by e-mail to or from Eutechnyx is intended to give rise to contractual or other legal liability, apart from liability which cannot be excluded under English law. This message has been checked for all known viruses by Star Internet delivered through the MessageLabs Virus Control Centre. www.eutechnyx.com Eutechnyx Limited. Registered in England No: 2172322 |
From: Richard F. <gd...@th...> - 2004-07-07 08:41:22
|
> >its a bit C++ specific, but does anyone know of a way of getting the > >"meaning" of this to compile... > > > >const float ropeLength = 12.5f; > >const float knotSpace = 3.3f; > >int array[ ropeLength / knotSpace ]; > > > >... its sorta meaning we have to break our own coding > guidlines.... (no > >#defines unless truly necessary) > Hi > > AFAIK the number of tokens in an array must be an integer . > An easy solution could be to compute the division in a temp > variable then see if it give us a modulo ,if yes increase by > one our result > and finally use that result to create the array. > > GC. that, my friend is not the problem... i wouldn't be much of a coder if i thought i could have "pi" elements in my array... The problem is that as long as there is a "const float" involved, the compiler will not listen to the fact that it is a const. e.g. if i replace "int array[ ropeLength / knotSpace ];", with "int array[ int(ropeLength / knotSpace) ];", or "int array[ (int)(ropeLength / knotSpace) ];", there is still a compilation error. --- Outgoing mail is certified Virus Free. Checked by AVG anti-virus system (http://www.grisoft.com). Version: 6.0.716 / Virus Database: 472 - Release Date: 05/07/2004 |