Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS345 – Event-driven Programming

Similar presentations


Presentation on theme: "CS345 – Event-driven Programming"— Presentation transcript:

1 CS345 – Event-driven Programming
Dr. Randy Ribler

2 Who’s in charge here? The Computer The User
Many text-based console applications Computer says: “Enter username: “ Choices Enter a username Stop the program The User Event-driven/Graphical applications Computer responds to what the user does (events): Click a button Type a command Drag an icon

3 Event-driven Programming
Desktop Programming Windows Programming X Window Programming (linux) Mobile Programming Android Programming iPhone Programming Web Programming ASP.NET JavaScript

4 Events (Messages) Mouse Clicks Mouse Movements
Keyboard events (a character is pressed) Timer events (timer expires) Windows Events (e.g., a window gets/loses focus) System events (e.g., system shutdown)

5 Events are Described in Messages
Messages have a unique identifier (message number) Message can carry parameters – for example: Cursor coordinates on a mouse click The object that caused the message to be sent The character that was typed The mouse button that was pressed

6 Messages Enter a Message Queue
Third Message Second Message First Message Are Processed by a Message Loop

7 Message Loop While the program is still running (no quit message)
Read a message from the message queue Decode the message Invoke the functions that have been associated with that message

8 Associating Functions with Messages
C/C++ Function Pointers Point to functions called “callbacks” C# Delegates Safer types of function pointers

9 Function Pointers in C++
Functions that return pointers A function that returns a char* char* functionThatReturnsAPointer(int x); Pointers to functions A pointer to a function that returns a char* char* (*ptrName)(int x);

10 Memory Management in C++
Automatic variables are allocated on the run-time stack class Car { private: int year_; float mileage_; }; int x = 10; Car myCar; myCar.mileage_ 112000 myCar.year_ 2001 x 10 Return Address 0xabcdef04

11 Dynamically Allocated Memory in C++
Dynamically allocated memory in C++ is allocated from the “heap.” int x = 10; Car myCar; Car* ptr = new Car(); ptr 0x myCar.mileage_ 112000 myCar.year_ 2001 x 10 Return Address 0xabcdef04 The memory that stores the data member for the dynamically allocated car comes from the “heap,” and must be explicitly deallocated.

12 The Stack and the HeaP <allocated> <unallocated> ptr
0x myCar.mileage_ 112000 myCar.year_ 2001 x 10 Return Address 0xabcdef04

13 .Net Introduces “Managed Memory”
Managed Memory is memory that is “Garbage Collected” Garbage Collected Memory is allocated with new, but does not need to be explicitly deallocated. A separate heap is maintained for garbage collected memory. A special kind of pointer is used to point to garbage collected memory. These pointers are called “handles.” The Garbage Collection Process Periodically, mark all the memory that is referenced by handles. All other memory on the garbage collected heap is deallocated. Memory in the garbage collected heap might be “compacted” to make all the allocated memory contiguous. This might require changing handle values.

14 Handles in C++/CLI There are two class types in C++/CLI Conventional Classes Can be automatically allocated on the stack Can be dynamically allocated from the standard heap using “new” Reference Classes Can only be allocated with gcnew Use handles to point to them Use garbage-collected memory to store them.

15 Conventional class in C++/CLI
class Car { // : public: int myPublicDataMember_; }; int main() Car myCar; // myCar.myPublicDataMember is on the stack. Car* ptr = new Car(); // ptr->myPublicDataMember is on the heap. }

16 Reference class in C++/CLI
Ref class RCar { // : public: int myPublicDataMember_; }; int main() Rcar^ myCar; // myCar.myPublicDataMember does not exist! myCar= gcnew Car(); // myCar->myPublicDataMember is on the gc heap. }


Download ppt "CS345 – Event-driven Programming"

Similar presentations


Ads by Google