473,394 Members | 1,737 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

#define preprocessor

how I can do the following in C#, (the define line below)

c/c++ : #define MAX_LEN 0x23

for (int i=0;i<MAX_LEN;++)
array[i] = 0xFF;
Nov 17 '05 #1
14 3684
[Yosi] wrote:
how I can do the following in C#, (the define line below)

c/c++ : #define MAX_LEN 0x23

for (int i=0;i<MAX_LEN;++)
array[i] = 0xFF;


not *exactly* the same, but you could use

private const int MAX_LEN = 0x23;
(or "public" instead of "private")

--
Hans Kesting
Nov 17 '05 #2
No , this is not what I need , your suggestion allocate memory (sizeof
(int)), In my project there are more than 200 constants (define) , this will
need 200*4 = 800 bytes.
What I need is while running the compiler all the MAX_LEN will changed to be
the number I defined (pre compiler).
"Hans Kesting" wrote:
[Yosi] wrote:
how I can do the following in C#, (the define line below)

c/c++ : #define MAX_LEN 0x23

for (int i=0;i<MAX_LEN;++)
array[i] = 0xFF;


not *exactly* the same, but you could use

private const int MAX_LEN = 0x23;
(or "public" instead of "private")

--
Hans Kesting

Nov 17 '05 #3
In C++ there is a preprocessor that will react to the lines starting with #.
In C# there is no seperate preprocessor, instead you should use the const
keyword to achieve 'almost' the same.

Gabriel Lozano-Morán

"[Yosi]" <Yo**@discussions.microsoft.com> wrote in message
news:1B**********************************@microsof t.com...
No , this is not what I need , your suggestion allocate memory (sizeof
(int)), In my project there are more than 200 constants (define) , this
will
need 200*4 = 800 bytes.
What I need is while running the compiler all the MAX_LEN will changed to
be
the number I defined (pre compiler).
"Hans Kesting" wrote:
[Yosi] wrote:
> how I can do the following in C#, (the define line below)
>
> c/c++ : #define MAX_LEN 0x23
>
> for (int i=0;i<MAX_LEN;++)
> array[i] = 0xFF;


not *exactly* the same, but you could use

private const int MAX_LEN = 0x23;
(or "public" instead of "private")

--
Hans Kesting

Nov 17 '05 #4
You could comment out everything you don't actually need.
But there is another solution. Some of the obfuscator tools out there have
features to automatically delete unused code from an assembly.
Honestly, do you really worry about this small overhead?
"[Yosi]" <Yo**@discussions.microsoft.com> schrieb im Newsbeitrag
news:1B**********************************@microsof t.com...
No , this is not what I need , your suggestion allocate memory (sizeof
(int)), In my project there are more than 200 constants (define) , this will need 200*4 = 800 bytes.
What I need is while running the compiler all the MAX_LEN will changed to be the number I defined (pre compiler).
"Hans Kesting" wrote:
[Yosi] wrote:
how I can do the following in C#, (the define line below)

c/c++ : #define MAX_LEN 0x23

for (int i=0;i<MAX_LEN;++)
array[i] = 0xFF;


not *exactly* the same, but you could use

private const int MAX_LEN = 0x23;
(or "public" instead of "private")

--
Hans Kesting

Nov 17 '05 #5
>No , this is not what I need , your suggestion allocate memory (sizeof
(int)),
No it doesn't.

What I need is while running the compiler all the MAX_LEN will changed to be
the number I defined (pre compiler).


That's what the C# compiler does with constants.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 17 '05 #6
Hello Yosi,
Even in C++/C there will be 800 bytes allocated. These constants still
exist in memory in C++/C. As someone else pointed out, 800 bytes isn't a lot
of overhead to worry about. Unless your program is extremely massive.
Regards,
Mark

"[Yosi]" wrote:
No , this is not what I need , your suggestion allocate memory (sizeof
(int)), In my project there are more than 200 constants (define) , this will
need 200*4 = 800 bytes.
What I need is while running the compiler all the MAX_LEN will changed to be
the number I defined (pre compiler).
"Hans Kesting" wrote:
[Yosi] wrote:
how I can do the following in C#, (the define line below)

c/c++ : #define MAX_LEN 0x23

for (int i=0;i<MAX_LEN;++)
array[i] = 0xFF;


not *exactly* the same, but you could use

private const int MAX_LEN = 0x23;
(or "public" instead of "private")

--
Hans Kesting

Nov 17 '05 #7
Mattias Sjögren <ma********************@mvps.org> wrote:
What I need is while running the compiler all the MAX_LEN will changed to be
the number I defined (pre compiler).


That's what the C# compiler does with constants.


It does - the constants *do* live on in the compiled IL. It's a tiny,
tiny amount of memory to worry about, but it is there.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #8
MarkTheNuke <Ma*********@discussions.microsoft.com> wrote:
Even in C++/C there will be 800 bytes allocated.
Only if the constants are used. In both cases, there'll be the size of
an int in the compiled code wherever it's used. In C#, there'll also be
the static member itself - including the name of it.
These constants still exist in memory in C++/C.
Only where used, in C++/C.
As someone else pointed out, 800 bytes isn't a lot
of overhead to worry about. Unless your program is extremely massive.


I'd say unless your program is absolutely tiny :)

Personally I think C# does it the right way...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #9
"MarkTheNuke" <Ma*********@discussions.microsoft.com> wrote in message
news:05**********************************@microsof t.com...
Even in C++/C there will be 800 bytes allocated. These constants still
exist in memory in C++/C. As someone else pointed out, 800 bytes isn't a lot of overhead to worry about. Unless your program is extremely massive.


Um... Nope. If you say
#define MAX_LEN 0x23
for (int i=0;i<MAX_LEN;++i)

no space is allocated for MAX_LEN, and as far as the compiler is concerned,
you typed it as:
for (int i=0;i < 23;++i)

Now, if you were using C++ and had written:
const int MAX_LEN = 0x23;
for (int i=0;i<MAX_LEN;++)

Then the compiler is not required to allocate space for it, and very few
compilers will.

Only if you'd written something like:
const int MAX_LEN = 0x23;
const int* pMaxLen = &MAX_LEN;

then is the compiler require to allocate space for MAX_LEN.

Nov 17 '05 #10
Interestingly, the compiler does it both ways. Given the code:
class Class1
{
const int Max_len = 23;
[STAThread]
static void Main(string[] args)
{
for (int i = 0; i < Max_len; ++i)
Console.WriteLine("{0}", i);
}
}

The generated IL (as decompiled by Reflector) shows that Max_len *is*
present in the EXE, but the code is compiled as:

private static void Main(string[] args)
{
for (int num1 = 0; num1 < 0x17; num1++)
{
Console.WriteLine("{0}", num1);
}
}

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Mattias Sjögren <ma********************@mvps.org> wrote:
What I need is while running the compiler all the MAX_LEN will changed to bethe number I defined (pre compiler).


That's what the C# compiler does with constants.


It does - the constants *do* live on in the compiled IL. It's a tiny,
tiny amount of memory to worry about, but it is there.
Nov 17 '05 #11
James Curran <ja*********@mvps.org> wrote:
Interestingly, the compiler does it both ways. Given the code:
class Class1
{
const int Max_len = 23;
[STAThread]
static void Main(string[] args)
{
for (int i = 0; i < Max_len; ++i)
Console.WriteLine("{0}", i);
}
}

The generated IL (as decompiled by Reflector) shows that Max_len *is*
present in the EXE, but the code is compiled as:

private static void Main(string[] args)
{
for (int num1 = 0; num1 < 0x17; num1++)
{
Console.WriteLine("{0}", num1);
}
}


Exactly. That's the difference between declaring it as const and
declaring it as static readonly.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #12
>Mattias Sjögren <ma********************@mvps.org> wrote:
>What I need is while running the compiler all the MAX_LEN will changed to be
>the number I defined (pre compiler).
That's what the C# compiler does with constants.


It does


Yeah that's what I said. Or did you mean to quote the other part of my
reply?

the constants *do* live on in the compiled IL. It's a tiny,
tiny amount of memory to worry about, but it is there.


Yes of course the value must exist somewhere. But the CLR doesn't have
to allocate space for the constant field (which I believe was what the
OP claimed would happen) since you're not allowed to use it at
runtime.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 17 '05 #13
Mattias Sjögren <ma********************@mvps.org> wrote:
Mattias Sjögren <ma********************@mvps.org> wrote:
>What I need is while running the compiler all the MAX_LEN will changed to be
>the number I defined (pre compiler).

That's what the C# compiler does with constants.
It does


Yeah that's what I said. Or did you mean to quote the other part of my
reply?


Not entirely sure, looking back at it.
the constants *do* live on in the compiled IL. It's a tiny,
tiny amount of memory to worry about, but it is there.


Yes of course the value must exist somewhere.


Whereas it doesn't using just a #define in C/C++ - an unused #define in
C/C++ would take absolutely no memory at all.
But the CLR doesn't have
to allocate space for the constant field (which I believe was what the
OP claimed would happen) since you're not allowed to use it at
runtime.


Can you not get the value by reflection? Maybe I'm wrong and you can't,
but if you *can*, then the value must have some space allocated for it
somewhere, wherever that is. (It's not once per instance, of course,
but I don't think the OP was saying that.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #14
there is no tiny amount of memory , when I buil ahuge application includes
more than 1000 constant, I will stop being a tiny amount of memory .
And Cody : yes I'm really worry about this small overhead :-(
"Jon Skeet [C# MVP]" wrote:
Mattias Sjögren <ma********************@mvps.org> wrote:
What I need is while running the compiler all the MAX_LEN will changed to be
the number I defined (pre compiler).


That's what the C# compiler does with constants.


It does - the constants *do* live on in the compiled IL. It's a tiny,
tiny amount of memory to worry about, but it is there.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 17 '05 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
by: srinivas reddy | last post by:
I have defined some variables using #define preprocessor instruction. And then later I checked whether I had defined a variable. Sometimes even though a variable have been defined, #if...
97
by: s | last post by:
Can I do this: #define MYSTRING "ABC" .. .. .. char mychar = MYSTRING; .. .. ..
34
by: BQ | last post by:
Hello Is there a way to declare 'FUNCT' via a define so that if its parameter x, a constant, is greater than 35, it returns 56, if not, 20. I would like that at compile time, not at run time. ...
7
by: JustBoo | last post by:
Whilst reading through the C++ FAQ. I came across Section 29.8 and realized I never had fully thought about where a #define resided in a Standard C++ program. I thought they were a compile-time...
11
by: Frank Silvermann | last post by:
#define q p I must admit to a most elementary confusion. I have only ever used #define to, in point of fact, #redefine . The above is standard. Is the following: #define...
3
by: ashwani | last post by:
Hi Can any one tell me the difference between preprocessor macros like #define and enum. If i want to define MAX_LIMIT=100 as preprocessor macro as #define MAX_LIMIT 100 or if i define enum...
4
by: Mohammad Omer Nasir | last post by:
I was read Linux kernel code in which I saw few define macro defines in functions. The snap of the function is following and file name "err_ev6.c". static int ev6_parse_cbox(u64 c_addr, u64...
6
by: anirbid.banerjee | last post by:
Hi, I need to write a macro which would have a lot many conditional #ifdef ... #endif blocks in it. #define _xx_macro (x) { ... \ ... \ /* some code (); */ #ifdef _SOME_STMT \ ... \ ... \
23
by: anon.asdf | last post by:
Hello! In the following code-snippet, is it possible to initialize each element of arr, with STRUCT_INIT? struct mystruct { int a; char b; };
2
by: badc0de | last post by:
Hello.. a header file of one of my project has macro definitions like this (for example) #define PART_A_SORT_1_LABEL_STRING1 100 #define PART_A_SORT_1_LABEL_STRING2 200 #define...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.