LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-30-2012, 03:19 PM   #31
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454

Quote:
Originally Posted by tuxdev View Post
That's a (not-that-great) solution to a problem that wouldn't have existed in the first place if you used something with decent scoping.

If you use a static const variable, and the compiler can determine you never ever try to take the address of it (so there's no pointer shenanigans to to try to ninja-modify the value), the compiler is legally allowed to treat it as a true constant (and almost all compilers do, when optimizations are turned on).
The scope of my problem was primarily Verilog; "C" defines were automatically generated from Verilog ones, and the names were preserved, so both HW developers and "C" programmers could use the same names for the same things.

...

I notice that, for example, highly skilled C++ programmers are often weak in other languages/approaches. E.g. using an advanced script to autogenerate things often sounds outlandish to them. Luckily, in VLSI design world we were not constrained by "pure programmers" taboos ...
 
Old 06-30-2012, 03:33 PM   #32
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
Quote:
I notice that, for example, highly skilled C++ programmers are often weak in other languages/approaches. E.g. using an advanced script to autogenerate things often sounds outlandish to them. Luckily, in VLSI design world we were not constrained by "pure programmers" taboos ...
It's a matter of which techniques you use by default. Unless you have a specific reason to use a #define over a static const (which in this case you *do*), then you should generally use a static const, as it's the generally superior technique. Just because it's *generally* better doesn't make it better in *all* cases, as your example proves.
 
Old 06-30-2012, 03:45 PM   #33
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by tuxdev View Post
It's a matter of which techniques you use by default. Unless you have a specific reason to use a #define over a static const (which in this case you *do*), then you should generally use a static const, as it's the generally superior technique. Just because it's *generally* better doesn't make it better in *all* cases, as your example proves.
You might be still missing my point. In a multi-language environment I do something like this (using, for example, Perl code/data as the primary spec) :

Code:
my %chip_spec =
  (
  FOO_ADDR => 0x98761234,
  FOO_OFFSET => 0x678,
  ...
  TWICE_FOO_OFFSET => '(2 * FOO_OFFSET)',
  ...
  );
and then from the above I can generate whatever I like - Verilog defines, "C" defines, "C" static const members, C++ public static const class members, etc.

Of course, the above can be hierarchical:

Code:
my %chip_spec =
  (
  DOO_BLOCK =>
    {
    FOO_ADDR => 0x98761234,
    FOO_OFFSET => 0x678,
    ...
    TWICE_FOO_OFFSET => '(2 * FOO_OFFSET)',
    ...
    },

  HOO_BLOCK =>
    {
    ...
    },
  );
- which in reality it is.

And I still stick to "single point of change" paradigm.
 
Old 07-01-2012, 02:55 AM   #34
worzel1968
Member
 
Registered: Mar 2011
Location: Melbourne, Australia
Distribution: fedora17_64, ubuntu
Posts: 44

Original Poster
Rep: Reputation: 2
Hi ntubski,

Quote:
But again, what is the upside to Hungarian notation?
I like my little notation system (it really only shows the type of the variable) because knowing the type can be important to avoid these errors:

1. Incorrect casting - and I always do explicit casts.
2. Problems with pointers - can be a big source of errors.

Here's another question: I have seen people do static_cast to convert an int to double, wondering if there is any advantage in doing this as opposed to doing an ordinary (double) cast, there are no classes involved?

Last edited by worzel1968; 07-01-2012 at 02:58 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Is this bad practice? jonnytabpni Linux - Security 8 05-26-2010 03:48 PM
Nesting while loops, bad practice? General Programming 17 05-05-2010 03:30 PM
iptables good practice - 2 questions ddaas Linux - Security 1 05-31-2005 07:09 AM
Is this good iptables practice ? michaelsanford Linux - Security 1 05-21-2005 09:32 PM
A good practice for compiling? Micro420 Mandriva 29 08-09-2004 03:36 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:57 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration