News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

Figuring out C style * and **

Started by sinsi, July 06, 2015, 03:44:57 PM

Previous topic - Next topic

sinsi

I have a prototype in C

int ADL2_Display_DisplayInfo_Get
( ADL_CONTEXT_HANDLE  context,
int  iAdapterIndex,
int *  lpNumDisplays,
ADLDisplayInfo **  lppInfo,
int  iForceDetect
)

So int * is a pointer to a dword holding a value, and ADLDisplayInfo ** is a pointer to a dword holding a pointer to an ADLDisplayInfo structure?


ADL_Display_DisplayInfo_Get (lpAdapterInfo[i].iAdapterIndex, &iNumDisplays, &lpAdlDisplayInfo, 0)

I assume that & is OFFSET?


dispinfo     ADLDisplayInfo <>
numdisplays  dd ?
dispinfoptr  dd offset dispinfo
...
invoke ADL2_Display_DisplayInfo_Get,hctx,1,offset numdisplays,offset dispinfoptr,0


This has left me seeing *s  :dazzled:
Tá fuinneoga a haon déag níos fearr :biggrin:

dedndave

Quote from: sinsi on July 06, 2015, 03:44:57 PM
So int * is a pointer to a dword holding a value, and ADLDisplayInfo ** is a pointer to a dword holding a pointer to an ADLDisplayInfo structure?

sounds exactly right

Quote from: sinsi on July 06, 2015, 03:44:57 PM
I assume that & is OFFSET?

right again (address of)


hutch--

Have a look at this macro in the MASM32 macros file. The code calling macros (rv fn etc ...) use it to support both INDIRECTION and REFERENCE. From memory qword's code calling macros have the same capacity.


  ; -------------
  ; expand prefix
  ; -------------
    expand_prefix MACRO txtitm
      LOCAL prefix1,wrd,nu,varname

      prefix1 SUBSTR <txtitm>,1,1

   ;; usable characters are "&" "*" "@" "#" "?" "^" "~" "`" "/"

        IFIDN prefix1,<&>                   ;; reference operator
          nu SUBSTR <txtitm>,2
          wrd CATSTR <ADDR >,nu
          EXITM <wrd>
        ENDIF

        IFIDN prefix1,<*>                   ;; indirection operator
          nu SUBSTR <txtitm>,2
          .data?
            varname dd ?
          .code
          push ebx
          mov ebx, nu
          mov ebx, [ebx]                    ;; dereference variable in EBX
          mov varname, ebx
          pop ebx
          EXITM <varname>
        ENDIF

      EXITM <txtitm>                        ;; exit with original argument
    ENDM

Gunther

Hi sinsi,

only a short note. int * is a pointer and int ** is a pointer that points to pointers. That's often used by allocating a matrix during run time.

Gunther
You have to know the facts before you can distort them.

jimg

What's the difference between a pointer and an offset?

rrr314159

offset is a term used only by the assembler / compiler / linker, means offset from the beginning of the program in memory. Since that's not known until later in the process (at linking, or even at loading) offset is not a physical address. It can be a small number like, say, 4, which is not a legal address. At some stage prior to actually running that will get added to the program beginning ("origin", "org") and become a real physical address - which can then be used as a pointer.

Pointer, OTOH, refers to a dword (or qword in 64-bit) which contains a real physical address (I suppose it could be virtual in some circumstances) that is used to directly access memory in a running program. The pointer may point to, for instance, an array "Customers", or a procedure "ListCustomers". In the first case you would probably run thru the list with the pointer, adding a "stride" (like "4" or "Size of CustomerStructure") to the pointer to access each customer one by one. In the second case you would call that pointer address, or simply jump to it, to run the procedure.
I am NaN ;)

Tedd

Quote from: sinsi on July 06, 2015, 03:44:57 PM

int ADL2_Display_DisplayInfo_Get
( ADL_CONTEXT_HANDLE  context,
int  iAdapterIndex,
int *  lpNumDisplays,
ADLDisplayInfo **  lppInfo,
int  iForceDetect
)

int is a signed register-sized value (DWORD on 32-bit, QWORD on 64-bit.)
int* is a pointer to an int.
int** is a pointer to a list of pointers, each of which point to an int. (Technically, just a pointer to a pointer to an int, but it's almost always a list of them; done this way because the size of the array isn't static or known in advance.)

Quote
I assume that & is OFFSET?
Yes. It gives the address of the symbol proceeding it, like ADDR or OFFSET.



Quote from: jj2007 on July 07, 2015, 12:01:25 AM
Looks more like a reference, although the difference between pointer, address and reference is remarkably foggy
'&' is used for references as well, but in this case it's the address. When the & follows a type, e.g. "int&", then it creates a reference to that type. A reference is just a pointer, with a magic trick; it will be automatically dereferenced when used -- you're explicitly saying that you'll only ever want to access the thing it points to, not the pointer itself -- so you don't need to keep adding '*' everywhere.
Potato2

redskull

Quote from: Tedd on July 08, 2015, 02:52:08 AM
A reference is just a pointer, with a magic trick

Nitpicks: references are not truly pointers; you can't do arithmetic on them, nor can they be null, nor must they actually even exist.  A pointer is a specific bit of addressable memory with an actual type, whereas a reference is just a "rename" or "alias" of an existing object.  The magic trick is that the compiler figures out when it needs to be implemented using a pointer, and when they can be optimized away.

-r


Tedd

Quote from: redskull on July 08, 2015, 04:41:08 AM
Nitpicks: references are not truly pointers; you can't do arithmetic on them, nor can they be null, nor must they actually even exist.  A pointer is a specific bit of addressable memory with an actual type, whereas a reference is just a "rename" or "alias" of an existing object.  The magic trick is that the compiler figures out when it needs to be implemented using a pointer, and when they can be optimized away.
Omitted details.. It was a purposeful simplification to avoid the confusion that all of the additional detail was apparently causing. (And not even the subject of this thread.)
Potato2

rrr314159

re. confusion ...

I purposely answered the q. re "difference between offset and pointer" as though it were asked about assembler not C++ - I wasn't confused. Just wanted to show how much simpler and more logical (i.e. better) assembler is. The subsequent discussion has amply proved my point
I am NaN ;)

hutch--

Pedantic semantics, an address is a location in memory, a POINTER is that address stored in a variable.


    lea eax, your_variable
    mov pVariable, eax

rrr314159

You're right, assembler is simple and sensible. Try telling a C++'er that pointers are that simple, and you'll get a long response explaining the fine points. Followed by an equally long post from some other C++ expert disagreeing with everything the first guy said
I am NaN ;)