News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Having trouble using a structure

Started by David, January 14, 2010, 06:14:08 PM

Previous topic - Next topic

David

Hi, for my program I made a structure that contains data that I use to read a file.  However, I am getting these errors:

x\FileRead.asm(15) : error A2179: structure improperly initialized
x\FileRead.asm(15) : error A2008: syntax error : in structure

Here is my struct

stream STRUCT
hMemory HANDLE ?
pMemory DWORD  ?
SizeRW DWORD  ?
hFile HANDLE ?
buffer db 65535 dup(0)
stream EndS

initialization:

.data
stream stream <?>


Figured it out

LOCAL xstream:stream

David,

I removed the [solved] notation in the title as this forum functions as a database for answered questions for many.

dedndave

that is a pretty big local, but ok

i may be wrong, but just selecting a different name might fix it

stream stream <?>

you have labeled the structure with a name that is already in use
if you still want it to be global, you might try this...

xstream stream <?>

Ghandi

#2
In the case of your first error, I believe it was due to mixing initialized and uninitialized data. You declare the structure to contain initialized data (the 0), but the structure variable is declared to have ininitialized content (the <?>). ML would have thrown an error trying to process the initialized data member, as the .data? section cannot contain initialized data by definition, it is the uninitialized data section.

I agree with dedndave, using the actual name of the structure as the name of the variable will cause ML to throw an error, if you want a variable which is closely named you could lead it with an underscore (or any legal character) to distuinguish the difference. Some (a lot?) of people will shun such usage, as it can cause confusion later on, better to instead use a name which is distinctly diferent than that which the structure has.

I also thought that when declaring a struct, you would not indicate that it contained initialized data (0), just as the declaration of the structure variable in the uninitialized data section wouldnt require any 0 or ?. If you wish to ensure it is explicitly initialized to 0, apart from its first use where it is already so, you could also add a routine to zero the contents and call it prior to using the structure variable.


stream STRUCT
  hMemory HANDLE ?
  pMemory DWORD ?
  SizeRW DWORD ?
  hFile HANDLE ?
  buffer db 65535 dup (?)
stream EndS

.data?
StreamStruct stream <>

.code
InitializeStreamStruct Proc uses ecx edi lpStreamStruct:DWORD

mov eax,0
mov edi,lpStreamStruct
mov ecx,sizeof stream

.repeat
  mov byte ptr [edi],al
  add edi,1
  sub ecx,1
.until !ecx

ret

InitializeStreamStruct EndP

InitializeStreamStruct2 Proc uses ecx edi lpStreamStruct:DWORD

xor eax,eax
mov edi,lpStreamStruct
mov ecx,sizeof stream
rep stosb
ret

InitializeStreamStruct2 EndP


Above I have shown two types of initialize routines, there are many ways to achieve it though, depending on your needs. You could even inline the initialization every time you want to start with a zero initialized structure variable . This is, of course, assuming that you need the structure variable zero initialized. You may well have different requirements, in which case I personally would zero initialize the structure variable and then fill it with the required information, but some others wouldn't, instead opting to fill each member individually as required before use. My main reason for zero initializing is that if a member is used in one instance, yet not in another, there will still be 'junk' data left from the previous use.

HR,
Ghandi

Finder

Thinks Ghandi

        I have the same trouble,but now,through you,have been resolved.
                                   
                                                      Finder