Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Structure Pointer Crahses Down The Programm

RIGHT_THENRIGHT_THEN Member Posts: 154
WhenEver I Try To Access A Element Of A StrucTure Through Its
Pointer Causes The Programm TO ShutDown.;-)

This Is What Happened;
[code]
BOOL GetClientRect(
HWND hWnd, // handle to window we want to get the rect off
LPRECT lpRect // pointer to structure for Retrieving
);
[/code]
I Used This Fuction For Obvious Reasons as The Name Suggests
Like THis:-
[code]
BOOL IsItSuccessFull;
LPRECT lpRect;

IsItSuccessFull = GetClientRect( hWnd,lpRect );
[/code]
Now WhatEver is Stored In The lpRect it would have to Retrieved
this way .It Being a Pointer To a Structure.
[code]
lpRect->(Any Member Of The Rect StrucTure); //OR NOT!
[/code]
i THought somewhere something is getting mixed up might be
so i made a entirely new projuct to just test a structure pointer
even there it does the same thing.
[code]
void StructTest()
{
struct Testing{

int a;
int b;
int c;
};
struct Testing *Test1=NULL;
struct Testing Test2;

Test2.a=10;//Produces No Error;

Test1->a=10;//Is a KIller Crashes Down the Programm
// TO 0 ALtitude Killing all Pessengers
// On BOARD.Shuts it Down Telling
// Illegal Operation Performed
// Please Refer Error:-Illegal

}
[/code]
Error:-illegal
===================
[code]
TEMPTEST caused an invalid page fault in
module TEMPTEST.EXE at 0167:004014a9.
Registers:
EAX=00000000 CS=0167 EIP=004014a9 EFLGS=00010206
EBX=0065fc18 SS=016f ESP=0065fa78 EBP=0065fad4
ECX=00000000 DS=016f ESI=0065fadc FS=3b0f
EDX=00000000 ES=016f EDI=0065fad4 GS=0000
Bytes at CS:EIP:
c7 00 0a 00 00 00 5f 5e 5b 8b e5 5d c3 cc cc cc
Stack dump:
0065fbc4 0065fadc 0065fc18 cccccccc cccccccc cccccccc cccccccc cccccccc cccccccc cccccccc cccccccc cccccccc cccccccc cccccccc cccccccc cccccccc
[/code]
================error discription ends=====================


same Above thing done in turbo c (dos mode) .it is happy giving results
same Above thing in void main() mode of vc++6 does it happily
same Above thing in Winmain() mode of vc++6 Produces Error why

[code]
while(1){//Endlessly
Why Why Why;
}
[/code]
why Are Structures Giving THis Problem they are Niether Getting any
thing nor Leting them Retrieved.It is happening with every
fuction which takes a pointer to a structure.EverySIngle One;
[code]
HELP NEDDED EMERGENTLY:-O
[/code]

Comments

  • stoberstober Member Posts: 9,765 ✭✭✭
    [b][red]This message was edited by stober at 2005-8-20 4:16:0[/red][/b][hr]
    you are supposed to pass a pointer to an object. lpRect is only an uninitialized pointer that points to god-only-knows-what! Instead of creating pointers, create actual RECT objects and pass a pointer to it.
    : [code]
    : BOOL IsItSuccessFull;
    : [red]RECT Rect;[/red]
    IsItSuccessFull = GetClientRect( hWnd,[red]&Rect[/red] );
    : [/code]


  • RIGHT_THENRIGHT_THEN Member Posts: 154
    : WhenEver I Try To Access A Element Of A StrucTure Through Its
    : Pointer Causes The Programm TO ShutDown.;-)
    :
    : This Is What Happened;
    : [code]
    : BOOL GetClientRect(
    : HWND hWnd, // handle to window we want to get the rect off
    : LPRECT lpRect // pointer to structure for Retrieving
    : );
    : [/code]
    : I Used This Fuction For Obvious Reasons as The Name Suggests
    : Like THis:-
    : [code]
    : BOOL IsItSuccessFull;
    : LPRECT lpRect;
    :
    : IsItSuccessFull = GetClientRect( hWnd,lpRect );
    : [/code]
    : Now WhatEver is Stored In The lpRect it would have to Retrieved
    : this way .It Being a Pointer To a Structure.
    : [code]
    : lpRect->(Any Member Of The Rect StrucTure); //OR NOT!
    : [/code]
    : i THought somewhere something is getting mixed up might be
    : so i made a entirely new projuct to just test a structure pointer
    : even there it does the same thing.
    : [code]
    : void StructTest()
    : {
    : struct Testing{
    :
    : int a;
    : int b;
    : int c;
    : };
    : struct Testing *Test1=NULL;
    : struct Testing Test2;
    :
    : Test2.a=10;//Produces No Error;
    :
    : Test1->a=10;//Is a KIller Crashes Down the Programm
    : // TO 0 ALtitude Killing all Pessengers
    : // On BOARD.Shuts it Down Telling
    : // Illegal Operation Performed
    : // Please Refer Error:-Illegal
    :
    : }
    : [/code]
    : Error:-illegal
    : ===================
    : [code]
    : TEMPTEST caused an invalid page fault in
    : module TEMPTEST.EXE at 0167:004014a9.
    : Registers:
    : EAX=00000000 CS=0167 EIP=004014a9 EFLGS=00010206
    : EBX=0065fc18 SS=016f ESP=0065fa78 EBP=0065fad4
    : ECX=00000000 DS=016f ESI=0065fadc FS=3b0f
    : EDX=00000000 ES=016f EDI=0065fad4 GS=0000
    : Bytes at CS:EIP:
    : c7 00 0a 00 00 00 5f 5e 5b 8b e5 5d c3 cc cc cc
    : Stack dump:
    : 0065fbc4 0065fadc 0065fc18 cccccccc cccccccc cccccccc cccccccc cccccccc cccccccc cccccccc cccccccc cccccccc cccccccc cccccccc cccccccc cccccccc
    : [/code]
    : ================error discription ends=====================
    :
    :
    : same Above thing done in turbo c (dos mode) .it is happy giving results
    : same Above thing in void main() mode of vc++6 does it happily
    : same Above thing in Winmain() mode of vc++6 Produces Error why
    :
    : [code]
    : while(1){//Endlessly
    : Why Why Why;
    : }
    : [/code]
    : why Are Structures Giving THis Problem they are Niether Getting any
    : thing nor Leting them Retrieved.It is happening with every
    : fuction which takes a pointer to a structure.EverySIngle One;
    : [code]
    : HELP NEDDED EMERGENTLY:-O
    : [/code]
    :


    WELL i have found the answer to it myself
    it took almost 6 hours to figure it out
    by revisiting structures in turbo c dos mode.

    SO I AM POSTING THIS FOR HELP TO ANY OTHER BEGINNER LIKE ME
    WHEN FINDS THIS SAME PROBLEM HERE IS THE ANSWER :-)
    SHALL WE!

    [CODE]
    NEVER EVER USE A POINTER TO A STRUCTURE WITHOUT FIRST MALLOCING IT
    do anything but first allocate some REAL ESTATE inside Memory
    to the STRUCTURE POINTER.which should have exclusive rights over it.

    I FOUND IT OUT BY LOT OF BRAIN HAMPERING.IT JUST OCCURED TO ME
    LET US SEE IF PROBLEM IS SOLVED BY ALLOCATING some memory to it
    first.AND THANK GOD the ALLMIGHT 89 MILLION times that yes it
    indeed did.PROBLEM solved.


    WHY IT HAPPENS so is because when ever we define a pointer
    it is pointing to something allready some garbage in it.
    it always has something in it.but that location inside it
    might or might not belong to some other data inside the same
    programm.SO if it does not then well and good no problems
    BUT IF IT DOES
    IF IT DOES.......
    P R O B L E M!!!!!!!!
    let me say it one more time
    P R O B L EE AA EE MMM!!!!!!

    twist it a little more

    P O L L L
    R B EE
    M M M M M M
    LALALALA M M M M .

    NOW IT IS OKAY.

    when we try to access that data that may not be what could
    be held in the variable we are trying to put it into.
    or it might be something unassesable.which would result in ILLEGAL
    OPERATION shutdown PROGRAMM problem.because we are encroaching
    on somebodies property it is declared illegal.Therefore always
    always assing some memory to a pointer first and then use it.
    secondly if that data was assesscible even then we are
    currupting the data which could be of use to some other
    aspect of the programm.and our data could be curropted by that
    very thing when it updates something new to that location.
    therefore pointer is a wild horse need MALLOCING TO TAME IT.

    so how is mallocing or allocating some memory to some
    variable done.simple
    let us take the above described case.the problem could have been
    avoided if we have only if we had done this.

    LPRECT lpRect;//pointer to structure RECT defined
    lpRect=(struct tagRECT *)malloc(sizeof(struct tagRECT));//memory
    //allocated
    now it has the predefined space inside memory of size
    exactly that is required to store information for
    structure RECT.

    another example

    struct Some *PointerToSome;//pointer to structure named some;
    PointerToSome=(struct Some *)malloc(sizeof(struct Some));

    this will do well with what ever variables Some holds;


    BUT I COULD BE WRONG TECHNICALLY ABOUT IT
    BUT NOT GENERALLY THIS IS THE CAUSE BEHIND IT ALTHOU
    MEMORY AND STUFF MIGHT BE SOMETHING DIFFRENT IT IS MY PERCEPTION

    BUT I HAVE THE RIGHT TO BE WRONG BECAUSE I AM A BEGINNNER ;-)





    [CODE]



  • RIGHT_THENRIGHT_THEN Member Posts: 154
    : [b][red]This message was edited by stober at 2005-8-20 4:16:0[/red][/b][hr]
    : you are supposed to pass a pointer to an object. lpRect is only an uninitialized pointer that points to god-only-knows-what! Instead of creating pointers, create actual RECT objects and pass a pointer to it.
    : : [code]
    : : BOOL IsItSuccessFull;
    : : [red]RECT Rect;[/red]
    : IsItSuccessFull = GetClientRect( hWnd,[red]&Rect[/red] );
    : : [/code]
    :
    :
    :
    WELL THANKYOU SIR STOBER
    you posted this answer while i might be typing
    MY PROBLEM SOLVED THREAD OTHERWISE I WOULD NOT HAVE
    posted it.i Posted it and found you have replied TO IT.
    you are right when you say where is it pointing! so now i am
    mallocing it.Please Go Through My Problem SOlved Thread and
    see it is any good;
    THANKS A MILLION !!!!!!



  • stoberstober Member Posts: 9,765 ✭✭✭
    why are you mallocing it? The structure is so small that mallocing probably takes more memory than just simply allocating on the stack as I suggested in my previous post.
  • stoberstober Member Posts: 9,765 ✭✭✭
    :
    : WELL i have found the answer to it myself
    : it took almost 6 hours to figure it out
    : by revisiting structures in turbo c dos mode.
    :

    good work :-) That's a really good feelng of satisfaction when you labor for hours and resolve the problem yourself. I'll bet you won't have that problem again.
Sign In or Register to comment.