473,396 Members | 1,678 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,396 software developers and data experts.

Memory occupied by a constant

If I declare a constant like
Public Const myConst as Byte =1

or I declare

Public Const myConst = 1
there is any difference?

--
Ceers,
Crirus

------------------------------
If work were a good thing, the boss would take it all from you

------------------------------
Nov 20 '05 #1
5 1584
* "Crirus" <Cr****@datagroup.ro> scripsit:
If I declare a constant like
Public Const myConst as Byte =1

or I declare

Public Const myConst = 1
there is any difference?


The 2nd constant will be stored as 'Int32'.

--
Herfried K. Wagner
MVP · VB Classic, VB.NET
<http://www.mvps.org/dotnet>

<http://www.plig.net/nnq/nquote.html>
Nov 20 '05 #2
OK... I'm just wandering why dont they store the const in minimum space
required it that const is unchangeable any way once declared

--
Ceers,
Crirus

------------------------------
If work were a good thing, the boss would take it all from you

------------------------------

"Crirus" <Cr****@datagroup.ro> wrote in message
news:Ok****************@TK2MSFTNGP11.phx.gbl...
If I declare a constant like
Public Const myConst as Byte =1

or I declare

Public Const myConst = 1
there is any difference?

--
Ceers,
Crirus

------------------------------
If work were a good thing, the boss would take it all from you

------------------------------

Nov 20 '05 #3
Hi Crirus,

Minimum space storage is a bit of a myth. A single byte value may take
up 1, 2 or 4 bytes depending on how values are aligned and what other values
it is with. If you want control over these things you have to specify
packing attributes. Mostly, of course, we leave it to the compiler and rely
on multi-MBs of memory making it immaterial.

In the case of the Const, it won't be minimised but will be given the
default type, in this case Int32. It is assumed that if a Byte is required
then a Byte will be specified. The majority of constants are probably ints
and in the code, any byte assigned to an int variable or using in an int
expression will have to be expanded to an int first. So, you could save a
byte or two in storage of the constant but spend both bytes and time
converting it when actually using it.

Regards,
Fergus
Nov 20 '05 #4
That's right I just was curious about that

--
Ceers,
Crirus

------------------------------
If work were a good thing, the boss would take it all from you

------------------------------

"Fergus Cooney" <fi****@post.com> wrote in message
news:u%****************@TK2MSFTNGP12.phx.gbl...
Hi Crirus,

Minimum space storage is a bit of a myth. A single byte value may take
up 1, 2 or 4 bytes depending on how values are aligned and what other values it is with. If you want control over these things you have to specify
packing attributes. Mostly, of course, we leave it to the compiler and rely on multi-MBs of memory making it immaterial.

In the case of the Const, it won't be minimised but will be given the
default type, in this case Int32. It is assumed that if a Byte is required
then a Byte will be specified. The majority of constants are probably ints
and in the code, any byte assigned to an int variable or using in an int
expression will have to be expanded to an int first. So, you could save a
byte or two in storage of the constant but spend both bytes and time
converting it when actually using it.

Regards,
Fergus

Nov 20 '05 #5
Crirus,
In addition to Fergus & Herfried's comments, remember a constant doesn't
really occupy any space as a variable would, the value itself is used in the
IL instructions when ever it is referenced.

To expand on Fergus's comments:

Public Const myByte As Byte = 1
Public Const myInteger As Integer = 1

Produce the same IL code when used, the 'size' (Byte verses Integer) is used
by the compiler to allow or disallow values on the constant itself.
Interesting enough both of the above can be assigned to a Byte variable!

Dim i As Byte = myByte
IL_0001: ldc.i4.1
IL_0002: stloc.2

Dim j As Integer = myInteger
IL_0003: ldc.i4.2
IL_0004: stloc.3

The "ldc" instruction is load constant, the ".i4" says Int32, the ".1" says
load the value 1. The "stloc" is the store location instruction, the ".2" &
".3" are location 2 & location 3 which is the third & fourth variable in
this routine (variable locations start with zero).
Public Const myConst = 1 This causes a compile error if you are using Option Strict On.

Hope this helps
Jay

"Crirus" <Cr****@datagroup.ro> wrote in message
news:Ok****************@TK2MSFTNGP11.phx.gbl... If I declare a constant like
Public Const myConst as Byte =1

or I declare

Public Const myConst = 1
there is any difference?

--
Ceers,
Crirus

------------------------------
If work were a good thing, the boss would take it all from you

------------------------------

Nov 20 '05 #6

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

Similar topics

6
by: Jani Yusef | last post by:
I have a HW problem stated as shown at the top of the solution. The thing is is that I am not 100% sure wtf constant memory means. Well, I think I do but I am confused. Does my solution use contant...
9
by: Birgit Rahm | last post by:
Hello newsgroup, I am a beginner, so I am asking maybe immoderate questions. I want to delete a variable, after filling it with complex objects. How should I do it? e.g. AAA = AAA = Can I...
11
by: Viktor | last post by:
Which GUI is the most stable one? I don't need any fancy looking widgets (look and feel doesn't realy matter to me), I "just" need it to be rock stable and fast...
2
by: anand | last post by:
hi, i am fetching data from database using dataadaptor and filling up a dataset. i checked memory usage of the app, and it consumes 50 Mb of RAM for the given amount of data. that is, i checked...
1
by: trialproduct2004 | last post by:
Hi all, I am having slight confusion regarding memory management in .net. Say suppose i have two application one is in C# and other is in MFC(VC++). Both of this application are using lots...
18
by: Jan | last post by:
Hi there, i've got an STL map with something like this ( map<string, Object*> xyz; ) What happens when I call xyz.clear()? Is only the map cleared or the map and the Objects, so that the memory...
0
by: somaskarthic | last post by:
Hi I need to get the system memory (not harddisk storage space) occupied by my c++ application ? Is there any readily available methods to find the memory currently occupied by the application ? ...
39
by: Ravi | last post by:
Can you all please suggest a program which tell us the range of memry addresses occupied by the given c program?
1
by: Lambda | last post by:
I'm trying to develop several interesting components of a simple search engine follow some text books. A book introduces some ways to compress dictionary, allow it to stay on main memory. In...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: 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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.