473,770 Members | 6,950 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Does this allocate memory?

If this is the complete program (ie, the address of the const is never
taken, only its value used) is it likely the compiler will allocate ram for
constantA or constantB? Or simply substitute the values in (as would be
required if I used the hideous, evil, much-abused #define :)

-----------

const int constantA = 10;
static const int constantB = 20;

void main()
{
for( int i=0; i<constantA ; i++ );
for( int j=0; j<constantB ; j++ );
}

----------

Thanks in advance :)

-Curt

Jul 19 '05
37 4678

"Curt" <cN****@nSoPrth AarMc.com> wrote in message
news:Xn******** *************** *@63.223.5.254. ..
int main( int argn, char *argv[] )
This isn't wrong but I recommend that you stick to the standard argc versus
argn. On the other hand, you're not using any command line arguments passed
in the first place.
{
for( volatile int i=0; i<constantA ; i++ );
for( volatile int j=0; j<constantB ; j++ );

return 0;
}

Jul 19 '05 #21
Karl Heinz Buchegger <kb******@gasca d.at> wrote in
news:3F******** ******@gascad.a t:


Jingz wrote:

[...]

Thank you for your response.
>
>> const int constantA = 10;
>> static const int constantB = 20;
>>
>> int main( int argn, char *argv[] )
>> {
>> for( volatile int i=0; i<constantA ; i++ ); for( volatile int
>> j=0; j<constantB ; j++ );
>>
>> return 0;
>> }
>> }
> Why are you using volatile for a local variable?
>


Because the original responders wanted to tell me how smart they were
about proper c++ rather than answer the obvious question in a
meaningful way. So I composed an equally trivial example that would
force code generation.


But that loops still does no work other then consuming CPU time.
An optimizer might drop them.


Correct, but using 'volatile' compels the compiler to encode as written.
The point of the example has nothign to do with how the constantA/B is used
other than the fact that their address is never taken.

-Curt

Jul 19 '05 #22
On Tue, 22 Jul 2003 13:49:30 GMT, Curt <cN****@nSoPrth AarMc.com>
wrote:
If this is the complete program (ie, the address of the const is never
taken, only its value used) is it likely the compiler will allocate ram for
constantA or constantB? Or simply substitute the values in (as would be
required if I used the hideous, evil, much-abused #define :)

-----------

const int constantA = 10;
static const int constantB = 20;
static is superfluous above - namespace scope consts are static by
default. Perhaps you want:

extern const int constantC = 30;

void main()
{
for( int i=0; i<constantA ; i++ );
for( int j=0; j<constantB ; j++ );
}


Even a stupid compiler should manage to avoid allocating any static
storage for the constants, unless they are declared extern, in which
case only compilers with whole program optimization will be able to
eliminate the storage.

Tom
Jul 19 '05 #23
"Fao, Sean" <en**********@y ahoo.comI-WANT-NO-SPAM> wrote in
news:hM******** *******@news.ab s.net:

"Curt" <cN****@nSoPrth AarMc.com> wrote in message
news:Xn******** *************** *@63.223.5.254. ..
int main( int argn, char *argv[] )


This isn't wrong but I recommend that you stick to the standard argc
versus argn. On the other hand, you're not using any command line
arguments passed in the first place.


I tend to think of is as "args - number" and "args - values" and have seen
argn and argc (count, I presume) both used.

-Curt

Jul 19 '05 #24
"Curt" <cN****@nSoPrth AarMc.com> wrote...
"Fao, Sean" <en**********@y ahoo.comI-WANT-NO-SPAM> wrote in
news:hM******** *******@news.ab s.net:

"Curt" <cN****@nSoPrth AarMc.com> wrote in message
news:Xn******** *************** *@63.223.5.254. ..
int main( int argn, char *argv[] )


This isn't wrong but I recommend that you stick to the standard argc
versus argn. On the other hand, you're not using any command line
arguments passed in the first place.


I tend to think of is as "args - number" and "args - values" and have seen
argn and argc (count, I presume) both used.


It absolutely doesn't matter how you name them. Convention to
name the first argument 'argc' and the second 'argv' is just that,
a convention.

Victor
Jul 19 '05 #25
"Peter van Merkerk" <me*****@deadsp am.com> wrote in
news:bf******** ****@ID-133164.news.uni-berlin.de:

Microsoft thinks defined like "BOOL" and "WORD" make sense, so I'm
not quite sure invoking problems with their source code is credible.


The Microsoft Windows API is written for C compilers somewhere in the
eighties, they could not use C++ features. The choices they made do


Yes I remember, they were wrong then and are wrong now, but chose not to
bite the bullet and re-architect when 16-bit os's were basically dead-ended
on the desktop. I said so at the time, but no one listened to me then
either ;)

Having said that, for all the bashing, Microsoft does have some truly
brilliant programmers cranking out code, its too bad it get tainted by the
other things they do.
I have
indeed never seen a sane define collide in the manner you suggest
happens "all the time" but if we're going to assume insane
programmers then you can prety much claim anything you choose.


What you say may make sense for small projects, but with large
complicated projects the rules change. Problems that might seem
accademic in one context, may become a very real problems in another
context. For example if you use multiple 3rd party libraries or work
on a large projects, name clashes are not all that uncommon. I'm not
making this up, I'm speaking from personal experience. The usual
workaround for these problems is using a prefix for macro names. But
what if two 3rd party libraries have name clashes? In that case you
are in deep shit I can tell you. Sure you can modify the header files.
But that means maintaining your own version of that header file. Every
time a new version of the library is released you will have to do the
modifications again. This can become quite a headache, and therefore
one should be extremely reluctant to do so.


Having worked on both, I think you are overgeneralizin g. A large project
that has those kinds of collisions is mismanaged and has other problems.
However working with 3rd party libraries I can believe it. I have had some
experience with graphics and XML libraries in this regard, and almost
always wrap that functionality using our established coding standards. I
would be curious to see an example where such a wrapper would not solve the
name collision.

-Curt

Jul 19 '05 #26
Karl Heinz Buchegger <kb******@gasca d.at> wrote in
news:3F******** *******@gascad. at:


Jingz wrote:

On Tue, 22 Jul 2003 17:56:04 -0400, Victor Bazarov wrote:
> "Jingz" <e@spam.com> wrote...
>> [...]
>> In terms of "the tone I've taken" I have been factual [..]
>
> Which part in your "inheritanc e obscures functionality at best"
> tirade is factual?
>


A call to someFunct(); that is not found in the implementation/header
file of the class you are on, is either global or (more probobly, in
a well-formed project, an inherited class)

In order to understand that program you must now track down which
class its in, and in the case of multiple inheritance this can become
quite annoying. Of course in a codebase you are very famliar with, or
perhaps one you wrote yourself, this is not a big deal; in a large
project or in maintaining a new set of code it becomes a very big
deal. It also, I believe, fits a reasonable definition of
"obscuring" .


Right. And?
That's why we are professinoals and get paid for it. If every teeny
weeny newbie could do that we wouldn't earn our money. Building a
rocket technically isn't very distinct from buildind a bicycle. You
use the same tools. And yet building a rocket is incredible more
complex then building a bike and one needs to take care of much more
things. That's why rocket engineers earn much more money, they know
how to do it and more importantly: they can do it.


I do agree that professionals get payed to do the job they are experts
in. Now please do not take this as personal criticism, but I firmly
believe in dumbing everything down as much as possible, so the brain-
cycles can be spent on the really difficult problems. Put too simply- if
its tedious and difficult to cut and paste text, then thats less effort
and time you can spend concentrating on the content of that text.

I prefer structures to be simple and straightforward , so the task they
are performing becomes the focus.

I will be the first to admit this may be the result of the type of work I
do, which is enormous, high-bandwidth databases, bare-metal embedded
work, and gui interfaces to them. In deference to that, I am always
looking to learn new things and not a day goes by that I don't learn
something interesting about the programming art. Its wonderful being part
of such a bottomless industry, in terms of knowledge and new experiences.

-Curt

Jul 19 '05 #27

"Curt" <cN****@nSoPrth AarMc.com> wrote in message news:Xn******** *************** *@63.223.5.254. ..
I should have mentioned this in the original post, but the application is
that the const is being defined in a header file that is included in many
translation units, and I want to make sure a new copy is not created for
each time it is used (as would be the case using a #define) but prefer to
use the namespace/typsafety of 'const int'.


Even in header files, consts at namespace scope are static.
Jul 19 '05 #28
On Wed, 23 Jul 2003 18:21:28 GMT, Curt <cN****@nSoPrth AarMc.com>
wrote:
const int constantA = 10;
static const int constantB = 20;


static is superfluous above - namespace scope consts are static by
default. Perhaps you want:

extern const int constantC = 30;


Thank you for your response.

I should have mentioned this in the original post, but the application is
that the const is being defined in a header file that is included in many
translation units, and I want to make sure a new copy is not created for
each time it is used (as would be the case using a #define) but prefer to
use the namespace/typsafety of 'const int'.


In a header it is still static - each translation unit sees a
"different" const variable (that happens to have the same name and
value) and each translation unit can be considered separately. The
advantage of static is that the compiler doesn't have to work out that
you aren't taking the address of the const in any translation unit,
but only in the current translation unit.

Finally, one thing to be aware of is that some compilers may allocate
storage for a const if you pass it by reference in that translation
unit (to a non-inline function perhaps?) in addition to if you take
its address.

Tom
Jul 19 '05 #29


Karl Heinz Buchegger wrote:

???
There seems to be a misconception of what volatile really does.
In the above, volatile doesn't do, what you seem to think it does.


Apologies.
It was my fault. A few seconds after hitting 'send', I realized what you
are aming at. And it will work. You are right and I was wrong.

One more time: Apologies.

--
Karl Heinz Buchegger
kb******@gascad .at
Jul 19 '05 #30

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

Similar topics

4
3672
by: Alan Gifford | last post by:
I wrote a program to make sure that new would throw a bad_alloc exception if more memory was requested than was available. On my system, new allocates up to 2931 MBs of memory (I don't have that much, not even with swap) before throwing an exception. When the program tries to fill in the allocated memory with data, it is killed after using up the available memory. Does anyone know why the exception is not being thrown until well after...
4
2591
by: Franklin Lee | last post by:
Hi All, I use new to allocate some memory,even I doesn't use delete to release them. When my Application exit, OS will release them. Am I right? If I'm right, how about Thread especally on Solaries OS? This means that I use new to allocate memory in one Thread and doesn't use delete to release them.
9
3735
by: R.Z. | last post by:
i was wondering whether it pays off in terms of memory use to maintain lots of empty deques (it would be convenient for my algorithms but memory use is more important). and does the size of a deque depends on the size of its members even if the deque is empty? is there at all a way to check out how much memory my deque occupies? i've read that the sizeof operator cannot be used with dynamically allocated arrays so i figured it wouldn't give...
74
4053
by: Suyog_Linux | last post by:
I wish to know how the free()function knows how much memory to be freed as we only give pointer to allocated memory as an argument to free(). Does system use an internal variable to store allocated memory when we use malloc(). Plz help......
6
2316
by: lovecreatesbeauty | last post by:
Hello experts, 1. Does C guarantee the data layout of the memory allocated by malloc function on the heap. I mean, for example, if I allocate a array of 100 elements of structure, can I always reference a correct/valid structure member upon that allocated memory? If I allocate memory, for example like this way:
3
1797
by: Nadav | last post by:
Hi, I am writing a mixed mode application, I have a mixed mode Assembly manipulating a managed byte array, to access this array in unmanaged code I '__pin' the array, As I understand, pining an object guarantee that it will not be collected by the GC ( by increasing it's refcount or so ), Taking that in mind, looking at the code generated by the compiler I can't see anything taking care of the GC refcount... following is the pinned variable...
5
4716
by: Allen | last post by:
I wrote many classes. In a class, there is a member variable which is declared as char szMTreeBuf. On both Windows XP and VxWorks, it cannot work. Then I try to declare the member variable as static char szMTreeBuf, it can work again. But when add some other function codes, it cannot run normally. I modify the class delearation, and change szMTreeBuf member variable to be local variable. Again it can run now. Why does C++ program run...
5
8092
by: mkaushik | last post by:
Hi everyone, Im just starting out with C++, and am curious to know how "delete <pointer>", knows about the number of memory locations to free. I read somewhere that delete frees up space assigned to <pointerby "new". Does "new" create a list of pointer names and the size of the memory array they point to? I also read that some compilers may store the number of consec mem locations a pointer points to, just before the first data...
9
8179
by: rohits123 | last post by:
I have an overload delete operator as below ////////////////////////////////// void operator delete(void* mem,int head_type) { mmHead local_Head = CPRMemory::GetMemoryHead(head_type); mmFree(&local_Head,(char *)mem); CPRMemory::SetMemoryHeadAs(local_Head,head_type); } ///////////////////// void* operator new(size_t sz, int head_Type) {
0
9591
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9425
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10225
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10053
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9867
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7415
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5312
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3573
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2816
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.