Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Using #define wrong way ?

Using #define wrong way ?

Scheduled Pinned Locked Moved C / C++ / MFC
helpdatabasequestion
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    Vaclav_
    wrote on last edited by
    #1

    I am trying to build SAM3x8E registers database. I though I could use serial of #define(s) to do that but run into a problem In this sample I can build PIO_SODR but I cannot add another (port) offset to it - see PIOA_SODR. I think the problem is my defines are not really int (32 bits) , but only symbols. I did compare the (symbol) PIO_SODR to its true hex value and it failed. BUT - I can add the first offset of 0x30 no problem. Ether plain add (+) or (|) or works. Any help would be as always appreciated.

    /* MSD upper 16 bits byte base*/
    #define SAM_MSB 0x400E //MSD upper 16 bits byte base
    /* LSD lower 16 bits byte base */
    #define SAM_LSB 0xE00
    /* PIO offset from PIO A and up */
    #define PIO_OFFSET 0x200
    /* status ouput data register */
    #define PIO_SODR SAM_MSB << 16 | SAM_LSB + 0x30 // base plus reg offset OK
    #define PIOA_SODR PIO_SODR + 0x200 // add in another offset fails

    K CPalliniC 2 Replies Last reply
    0
    • V Vaclav_

      I am trying to build SAM3x8E registers database. I though I could use serial of #define(s) to do that but run into a problem In this sample I can build PIO_SODR but I cannot add another (port) offset to it - see PIOA_SODR. I think the problem is my defines are not really int (32 bits) , but only symbols. I did compare the (symbol) PIO_SODR to its true hex value and it failed. BUT - I can add the first offset of 0x30 no problem. Ether plain add (+) or (|) or works. Any help would be as always appreciated.

      /* MSD upper 16 bits byte base*/
      #define SAM_MSB 0x400E //MSD upper 16 bits byte base
      /* LSD lower 16 bits byte base */
      #define SAM_LSB 0xE00
      /* PIO offset from PIO A and up */
      #define PIO_OFFSET 0x200
      /* status ouput data register */
      #define PIO_SODR SAM_MSB << 16 | SAM_LSB + 0x30 // base plus reg offset OK
      #define PIOA_SODR PIO_SODR + 0x200 // add in another offset fails

      K Offline
      K Offline
      k5054
      wrote on last edited by
      #2

      #defines are not even symbols - when the pre-processor runs it replaces the #define "tag" with the associated text. You probably want to add brackets to the PIO_SODR and PIOA_SODR definitions e.g. #define PIO_SODR (SAM_MSB<<16|SAM_LSB+0x30) #define PIOA_SODR (PIO_SODR + 0x200)

      V 1 Reply Last reply
      0
      • K k5054

        #defines are not even symbols - when the pre-processor runs it replaces the #define "tag" with the associated text. You probably want to add brackets to the PIO_SODR and PIOA_SODR definitions e.g. #define PIO_SODR (SAM_MSB<<16|SAM_LSB+0x30) #define PIOA_SODR (PIO_SODR + 0x200)

        V Offline
        V Offline
        Vaclav_
        wrote on last edited by
        #3

        I think I got part of the answer - I am mixing logical operations with math so that is why the first #define works ( compare to value) AFTER I added brackets. #define PIO_SODR (SAM_MSB << 16 | SAM_LSB + PIO_OFFSET) now passes the value test OK #define PIOA_SODR (PIO_SODR + PIO_OFFSET) But the additional offset does not work, but I am getting closer. Thank for your help. And it also helps to check if my #define(s) are not duplicated somewhere else !!! Thanks

        1 Reply Last reply
        0
        • V Vaclav_

          I am trying to build SAM3x8E registers database. I though I could use serial of #define(s) to do that but run into a problem In this sample I can build PIO_SODR but I cannot add another (port) offset to it - see PIOA_SODR. I think the problem is my defines are not really int (32 bits) , but only symbols. I did compare the (symbol) PIO_SODR to its true hex value and it failed. BUT - I can add the first offset of 0x30 no problem. Ether plain add (+) or (|) or works. Any help would be as always appreciated.

          /* MSD upper 16 bits byte base*/
          #define SAM_MSB 0x400E //MSD upper 16 bits byte base
          /* LSD lower 16 bits byte base */
          #define SAM_LSB 0xE00
          /* PIO offset from PIO A and up */
          #define PIO_OFFSET 0x200
          /* status ouput data register */
          #define PIO_SODR SAM_MSB << 16 | SAM_LSB + 0x30 // base plus reg offset OK
          #define PIOA_SODR PIO_SODR + 0x200 // add in another offset fails

          CPalliniC Offline
          CPalliniC Offline
          CPallini
          wrote on last edited by
          #4

          Why don't you use enum (see, for instance "Avoiding #defines for constant data and using enums instead"[^])?

          In testa che avete, signor di Ceprano?

          V 1 Reply Last reply
          0
          • CPalliniC CPallini

            Why don't you use enum (see, for instance "Avoiding #defines for constant data and using enums instead"[^])?

            V Offline
            V Offline
            Vaclav_
            wrote on last edited by
            #5

            Mainly because the "core" program is build using defines and I am still not too comfortable using enum. BTW after I "solved" this problem I have decided to "improve" on on it a got stuck again. Life is fun.

            S 1 Reply Last reply
            0
            • V Vaclav_

              Mainly because the "core" program is build using defines and I am still not too comfortable using enum. BTW after I "solved" this problem I have decided to "improve" on on it a got stuck again. Life is fun.

              S Offline
              S Offline
              Stefan_Lang
              wrote on last edited by
              #6

              I can only agree with Carlo on this. More generally, whenever there is a suitable way to use standard language features instead of #define, use that standard language feature. Even if it means you need to type more! Modern editors offer autocompletion that makes typing effort a non-argument! Moreover, code riddled with #define macros is much harder for editors to interpret, preventing it from offering correct and complete autocompletion lists. Therefore the use of #define has the very real potential to actually increase the typing effort by hamstringing the editors capabilities! Besides, as you've already stated, #defines will just keep tripping you up. Most of the time it is due to their entire lack of context-related syntax checking at the time of writing. And sometimes it is because your macro name conflicts with another non-macro symbol in some totally unrelated part of the code - that latter issue will lead to obscure errors that can keep you busy for days. I am speaking from my own experience. Therefore: save time - do not use #define unless there is no other way to achieve the intended results. And if you do, don't place these macros in a header that gets included in unrelated source files - either put them right into the source file that needs them, or, if there are multiple source files that need them, put the macros in a separate header just for that purpose.

              GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups