Since pstTask is a global variable, this line.
Up vote 0 down vote favorite share g+ share fb share tw.
Include "stdafx. H" #include #include #define MAX_ET_TABLES 10 typedef struct{ unsigned char ucVersion; unsigned char enMonitor; unsigned short uPid; }SData_t; typedef struct{ unsigned char ucVersion; unsigned short uID; unsigned int uiCollected; SData_t stMAX_ET_TABLES; }STData_t; typedef struct{ STData_t stData; }psTask; psTask *pstTask; int main() { printf("\npstTask->stData = %u\n",&pstTask->stData); memset(&(pstTask->stData), 0xFF, sizeof(pstTask->stData)); return 0; } I need to set the values for the structure variables as 0xFF. The above program is throwing an unhandled exception as "Unhandled exception at 0x61e1f5cf (msvcr90d.
Dll) in start. Exe: 0xC0000005: Access violation writing location 0x00000000. " please help me understand why it is.
Page ,132 title memset - set sections of memory all to one byte ;*** ;memset. Asm - set a section of memory to all one byte ; ; Copyright (c) Microsoft Corporation. All rights reserved.
; ;Purpose: ; contains the memset() routine ; ;******************************************************************************* . Xlist include cruntime. Inc .
List page ;*** ;char *memset(dst, value, count) - sets "count" bytes at "dst" to "value" ; ;Purpose: ; Sets the first "count" bytes of the memory starting ; at "dst" to the character value "value". ; ; Algorithm: ; char * ; memset (dst, value, count) ; char *dst; ; char value; ; unsigned int count; ; { ; char *start = dst; ; ; while (count--) ; *dst++ = value; ; return(start); ; } ; ;Entry: ; char *dst - pointer to memory to fill with value ; char value - value to put in dst bytes ; int count - number of bytes of dst to fill ; ;Exit: ; returns dst, with filled bytes ; ;Uses: ; ;Exceptions: ; ;******************************************************************************* CODESEG extrn _VEC_memzero:near extrn __sse2_available:dword public memset memset proc \ dst:ptr byte, \ value:byte, \ count:dword OPTION PROLOGUE:NONE, EPILOGUE:NONE . FPO ( 0, 3, 0, 0, 0, 0 ) mov edx,esp + 0ch ; edx = "count" mov ecx,esp + 4 ; ecx points to "dst" test edx,edx ; 0?
Jz short toend ; if so, nothing to do xor eax,eax mov al,esp + 8 ; the byte "value" to be stored ; Special case large block zeroing using SSE2 support test al,al ; memset using zero initializer? Jne dword_align cmp edx,0100h ; block size exceeds size threshold? Jb dword_align cmp DWORD PTR __sse2_available,0 ; SSE2 supported?
Je dword_align jmp _VEC_memzero ; use fast zero SSE2 implementation ; no return ; Align address on dword boundary dword_align: push edi ; preserve edi mov edi,ecx ; edi = dest pointer cmp edx,4 ; if it's less then 4 bytes jb tail ; tail needs edi and edx to be initialized neg ecx and ecx,3 ; ecx = # bytes before dword boundary jz short dwords ; jump if address already aligned sub edx,ecx ; edx = adjusted count (for later) adjust_loop: mov edi,al add edi,1 sub ecx,1 jnz adjust_loop dwords: ; set all 4 bytes of eax to value mov ecx,eax ; ecx=0/0/0/value shl eax,8 ; eax=0/0/value/0 add eax,ecx ; eax=0/0val/val mov ecx,eax ; ecx=0/0/val/val shl eax,10h ; eax=val/val/0/0 add eax,ecx ; eax = all 4 bytes = value ; Set dword-sized blocks mov ecx,edx ; move original count to ecx and edx,3 ; prepare in edx byte count (for tail loop) shr ecx,2 ; adjust ecx to be dword count jz tail ; jump if it was less then 4 bytes > rep stosd // the arrow comes here when that exception is thrown main_loop_tail: test edx,edx ; if there is no tail bytes, jz finish ; we finish, and it's time to leave ; Set remaining bytes tail: mov edi,al ; set remaining bytes add edi,1 sub edx,1 ; if there is some more bytes jnz tail ; continue to fill them ; Done finish: mov eax,esp + 8 ; return dest pointer pop edi ; restore edi ret toend: mov eax,esp + 4 ; return dest pointer ret memset endp end EDITED: int main() { psTask *pstTask; pstTask = (psTask*)malloc(sizeof(psTask)); pstTask = NULL; printf("\npstTask->stData = %u\n",&pstTask->stData); memset(&(pstTask->stData), 0xFF, sizeof(pstTask->stData)); return 0; } I tried like this bu still getting the exception. Please help. C link|improve this question edited Jan 5 at 2:22 asked Jan 5 at 1:44Angus995313 85% accept rate.
Since pstTask is a global variable, this line: psTask *pstTask; is equivalent to this: psTask *pstTask = 0x00000000; and since you never later change it to point anywhere else (e.g. , to properly allocated memory), this line: memset(&(pstTask->stData), 0xFF, sizeof(pstTask->stData)); is equivalent to this: memset(0x00000000, 0xFF, sizeof(pstTask->stData)); (since stData is the first element of the psTask structure, so has an offset of zero). This means that you're trying to write 0xFF into memory location 0x00000000 (as well as the subsequent 47 bytes or so), rather than to memory that you actually have permission to write to (e.g. Stack space, or space returned by malloc). Update for updated question: This bit: pstTask = (psTask*)malloc(sizeof(psTask)); pstTask = NULL; allocates memory for a psTask and points pstTask at it — so far so good — but then it sets pstTask right back to NULL (i.e.
, not pointing anywhere valid). You need to remove that second line. Also, after calling malloc you should always examine its return value; if it returns NULL, that means you're out of memory.
So, you should write something like this: pstTask = (psTask*)malloc(sizeof(psTask)); if(pstTask == NULL) { fprintf(stderr, "Out of memory. \n"); return 1; } printf("\npstTask->stData = %u\n",&pstTask->stData); (In this specific case, I'm sure that malloc will successfully return allocated memory, but you should really always check its return value. ).
1. Nice answer. – Mitch Wheat Jan 5 at 1:55 @ruakh : thank you for explainiing me clearly.
I have done some modifications but still i'm getting the exception dude. – Angus Jan 5 at 2:01 The first equivalence is true in general only because 0x00000000 is a null pointer constant. It stores a null pointer value in pstTask, which is not necessarily all-bits-zero (though as it happens it is on Windows, which apparently is the environment in which the program is running).
I find it clearer to think of it in terms of null pointers rather than 0x00000000. – Keith Thompson Jan 5 at 2:02 @KeithThompson: Well, yes. I chose 0x00000000 as my null-pointer notation because that's what was in the OP's error-message.
I suppose I should have explicitly used the phrase "null pointer" somewhere in my answer, but it didn't seem essential, because the problem was really that pstTask wasn't pointing anywhere, rather than specifically that it was null. (And if you're going to complain about a potential false implication that the null pointer is necessarily all-bits-zero, then why not complain about a potential false implication that a pointer is necessarily 32 bits long? ) – ruakh Jan 5 at 22:38 1 @Angus: O.K., I've updated my answer.
:-) – ruakh Jan 57 at 14:07.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.