Thread: [Dev-C++] #DEFINE versus cont variables in array inicializations
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: <and...@su...> - 2003-05-29 13:08:54
|
Dear Users, For the sake of flexibility, I think we only have two ways to iniciatize a size of an array: 1) via #define: #define MAX 10 ... int vect[MAX]; ... 2) via cont variable: const int size=10; ... int vetc[size]; ... So, in both ways, everytime if I need to change the size of the arrays, it´s only necessary to change 10 to the other value. -->My questions: 1) Is there more ways to to this? 2) For theese two ways presented here, when use one and when use the other? Explaining a little more what I need to know: in a "good programming technique" point of view, what of theese two is the preferred of the programmers (please don´t answer "pointers" ...lol). Regards André |
|
From: <or...@vp...> - 2003-05-29 13:31:00
|
André Macário Barros wrote: > -->My questions: > 1) Is there more ways to to this? To hardcode "magic" values :) But it is not an otpion. > 2) For theese two ways presented here, when use one and when use the other? Well, C++ encourages people to use const variables instead of #define values. But I think there are circumstances, when #define is better. In particular, when you HIGHLY optimize for speed in an algorithm, and you don't want to use magic values, then it is better to use #define values, as the preprocessor replaces them (as if you had typed in the exact value) and the compiler can make various optimizations. For example, when you need to shift with a known value: i = 1<<SHIFT_VAL; This case it is better to #define SHIFT_VAL 3 for example, rather than const int SHIFT_VAL=3, because in the first case the compiler will automatically calculate the value in compile time: i = 1<<3 --> i = 8; While this is not true for using const variables (I think, unless the compiler is smart), and it takes more time to shift with a variable, rather than with a #define. So, another example: int v; v<<SHIFT_VAL; Here v is a variable, and it is faster to shift it if SHIFT_VAL is a #define, rather than if SHIFT_VAL is a variable. (I have tested this extensively!) In your case, I don't think you need this type of low-level optimisation, so I think you can safely use a const variable. Hope I could help. -- Greetings, Balázs |
|
From: Zodiaq <zo...@ac...> - 2003-05-29 14:01:11
|
Hello André and all Dev-C++ users, Thursday, May 29, 2003, 3:08:42 PM, André wrote as follows: AMB> Dear Users, AMB> For the sake of flexibility, I think we only have two ways to iniciatize AMB> a AMB> size of an array: AMB> 1) via #define: AMB> #define MAX 10 AMB> ... AMB> int vect[MAX]; AMB> ... AMB> 2) via cont variable: AMB> const int size=10; AMB> ... AMB> int vetc[size]; AMB> ... AMB> So, in both ways, everytime if I need to change the size of the arrays, it´s AMB> only necessary to change 10 to the other value. -->>My questions: AMB> 1) Is there more ways to to this? enum ?? hehe But what for?? AMB> 2) For theese two ways presented here, when use one and when use the other? AMB> Explaining a little more what I need to know: in a "good programming AMB> technique" AMB> point of view, what of theese two is the preferred of the programmers AMB> (please AMB> don´t answer "pointers" ...lol). AFAIK const is more C++ like, and #define more C like... #define doesn't create 'real' variable, I mean it is only visible in first stage of preprocessing - precompiler raplaces every one of MAX into defined value, program does not see it in runtime... with const it is all different - it is created as variable (which is const), but during runtime you may const_cast it, check for its size, place in memory etc... So I must say I use both ways, but more often the const one... -- regards, Zodiaq |
|
From: <a_g...@ya...> - 2003-05-29 17:30:01
|
Hi Andre,
you are right, there's probably no third way out. Now I prefer to use the first one due to two reasons.
1> Being textual subsitution, the #define macro does not use up memory size whereas an integer uses up 32 bytes, though that is negligent for most purposes.
2> The more important being that apart from compiler systems that support C++ along with C, the interpretation of const by the compilers around is a bit non-standard. Like in the Cygwin gcc complier assigment to const variables result only in a warning at compile time and program runs fine, and prints out 20!!!.
#include <stdio.h>
int main()
{
const int i = 10;
i=20;
printf("%d\n",i);
return 0;
}/*main*/
So well although I cannot remember of a third way to do it right now, obviously the first approach is better.
André_Macário_Barros <and...@su...> wrote:
Dear Users,
For the sake of flexibility, I think we only have two ways to iniciatize
a
size of an array:
1) via #define:
#define MAX 10
...
int vect[MAX];
...
2) via cont variable:
const int size=10;
...
int vetc[size];
...
So, in both ways, everytime if I need to change the size of the arrays, it´s
only necessary to change 10 to the other value.
-->My questions:
1) Is there more ways to to this?
2) For theese two ways presented here, when use one and when use the other?
Explaining a little more what I need to know: in a "good programming
technique"
point of view, what of theese two is the preferred of the programmers
(please
don´t answer "pointers" ...lol).
Regards
André
-------------------------------------------------------
This SF.net email is sponsored by: eBay
Get office equipment for less on eBay!
http://adfarm.mediaplex.com/ad/ck/711-11697-6916-5
_______________________________________________
Dev-cpp-users mailing list
Dev...@li...
TO UNSUBSCRIBE: http://www.noicys.cjb.net/devcpp/ub.htm
https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
Thanx and Regards
Avik Ghose
Catch all the cricket action. Download Yahoo! Score tracker |
|
From: Per W. <pw...@ia...> - 2003-05-29 18:43:35
|
You are using mighty big integers :-)
A 32-byte unsigned integer would store numbers up to 1*10^77
/Per W
On Thu, 29 May 2003, Avik Ghose wrote:
> Hi Andre,
> you are right, there's probably no third way out. Now I prefer to use the=
first one due to two reasons.
> 1> Being textual subsitution, the #define macro does not use up memory si=
ze whereas an integer uses up 32 bytes, though that is negligent for most p=
urposes.
> 2> The more important being that apart from compiler systems that support=
C++ along with C, the interpretation of const by the compilers around is a=
bit non-standard. Like in the Cygwin gcc complier assigment to const varia=
bles result only in a warning at compile time and program runs fine, and pr=
ints out 20!!!.
> #include <stdio.h>
> int main()
> {
> const int i =3D 10;
> i=3D20;
> printf("%d\n",i);
> return 0;
> }/*main*/
> So well although I cannot remember of a third way to do it right now, obv=
iously the first approach is better.
>
>
>
>
> Andr=E9_Mac=E1rio_Barros <and...@su...> wrote:
> Dear Users,
>
> For the sake of flexibility, I think we only have two ways to iniciatize
> a
> size of an array:
> 1) via #define:
> #define MAX 10
> ...
> int vect[MAX];
> ...
>
> 2) via cont variable:
> const int size=3D10;
> ...
> int vetc[size];
> ...
>
> So, in both ways, everytime if I need to change the size of the arrays, i=
t=B4s
> only necessary to change 10 to the other value.
>
> -->My questions:
> 1) Is there more ways to to this?
> 2) For theese two ways presented here, when use one and when use the othe=
r?
> Explaining a little more what I need to know: in a "good programming
> technique"
> point of view, what of theese two is the preferred of the programmers
> (please
> don=B4t answer "pointers" ...lol).
>
> Regards
> Andr=E9
>
>
>
>
> -------------------------------------------------------
> This SF.net email is sponsored by: eBay
> Get office equipment for less on eBay!
> http://adfarm.mediaplex.com/ad/ck/711-11697-6916-5
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> TO UNSUBSCRIBE: http://www.noicys.cjb.net/devcpp/ub.htm
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>
>
> Thanx and Regards
> Avik Ghose
> Catch all the cricket action. Download Yahoo! Score tracker
|