系统级编程Lab 14. Exception and Process/Thread

这篇博客介绍了系统级编程的实践内容,包括在鼠标点击位置显示时间的实现,通过GetLocalTime()和wsprintf()函数处理时间显示。作者还解决了右键点击导致无限画画死循环的问题,通过创建线程避免影响主线程。此外,提到了多线程同步的实践,并给出了相关的代码示例。

系统级编程最后一周的实验,一共5个Practice,因为网上没有相关的解答,我给出自己的解答,有兴趣的可以参考参考,有误的可以发送我的邮箱doglikejie@163.com交流



这是ppt的题目要求

我分析之后给出我的源代码

Practice1

第一题是在鼠标点击位置显示时间

我使用了以下重要函数来实现这一功能

GetLocalTime(),这个函数的使用,我先定义了一个SYSTEMTIME类型的变量systime,然后GetLocalTime(&systime);这一行代码把本地时间储存在systime变量里。

然后很重要的一个函数wsprintf(),为什么我这里使用wsprintf函数而不是老师给的sprintf函数,因为我在vs2017环境下编译意外的发现sprintf函数无法被识别,具体原因我也不知道,所以就用了wsprintf函数代替。

 wsprintf(str, "[%d年%d月%d日%d时%d分%d秒]   ",

 systime.wYear, systime.wMonth, systime.wDay,

 systime.wHour, systime.wMinute, systime.wSecond);

因为windows编程没有printf这种函数,所以我用wsprintf函数的目的是为了将时间保存为字符串便于打印。我们利用systime.wYear等进一步确定精确的年月日时分秒。

 x = LOWORD(lParam);

 y = HIWORD(lParam);

这两行分别保存横坐标和纵坐标,关于如何保存鼠标的问题,我查阅网上发现不同的解法,我这里偷懒直接int两个变量x,y保存,网上的通解应该是POINT 一个变量,然后通过变量.x,变量.来储存横纵坐标,这两种方法的优劣我还没思考过。

 hdc = GetDC(hwnd);

 //输出字符串

 TextOut(hdc, x, y, str, strlen(str));

ReleaseDC(hwnd, hdc);

GetDC()和ReleaseDC()必须成对出现,GetDC之后必须释放掉。TextOut负责打印字符串,参数的用法可以查阅msdn。

#include <windows.h>

 

LRESULT CALLBACK WndProc (HWND,UINT,WPARAM,LPARAM) ;

 

int WINAPI WinMain (HINSTANCE hInstance,HINSTANCE hPrevInstance,

                    PSTR szCmdLine,int iCmdShow)

{

     static TCHAR szAppName[] =TEXT ("HelloWin") ;

     HWND         hwnd ;

     MSG          msg ;

     WNDCLASS     wndclass ;

 

     wndclass.style         = CS_HREDRAW |CS_VREDRAW ;

     wndclass.lpfnWndProc   = WndProc ;

     wndclass.cbClsExtra    = 0 ;

     wndclass.cbWndExtra    = 0 ;

     wndclass.hInstance     = hInstance ;

     wndclass.hIcon         = LoadIcon (NULL,IDI_APPLICATION) ;

     wndclass.hCursor       = LoadCursor (NULL,IDC_ARROW) ;

     wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;

     wndclass.lpszMenuName  = NULL ;

     wndclass.lpszClassName = szAppName ;

 

     if (!RegisterClass (&wndclass))

     {

          MessageBox (NULL,TEXT ("This program requires Windows NT!"),

                      szAppName, MB_ICONERROR) ;

          return 0 ;

     }

     hwnd = CreateWindow (szAppName,                  // window class name

                          TEXT ("The Hello Program"),// window caption

                          WS_OVERLAPPEDWINDOW,        // window style

                          CW_USEDEFAULT,              // initial x position

                          CW_USEDEFAULT,              // initial y position

                          CW_USEDEFAULT,              // initial x size

                          CW_USEDEFAULT,              // initial y size

                          NULL,                       // parent window handle

                          NULL,                       // window menu handle

                          hInstance,                  // program instance handle

                          NULL) ;                     // creation parameters

     

     ShowWindow (hwnd, iCmdShow) ;

     UpdateWindow (hwnd) ;

     

     while (GetMessage (&msg,NULL, 0, 0))

     {

          TranslateMessage (&msg) ;

          DispatchMessage (&msg) ;

     }

     return msg.wParam ;

}

 

LRESULT CALLBACK WndProc (HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam)

{

     HDC         hdc ;

     PAINTSTRUCT ps ;

     RECT        rect ;

 int         x, y;//分别储存横纵坐标

 CHAR        str[100];//储存转换成字符串的时间

 SYSTEMTIME systime;//负责储存系统时间

     

     switch (message)

     {

     case WM_CREATE:

          //PlaySound (TEXT ("hellowin.wav"), NULL, SND_FILENAME | SND_ASYNC) ;

          return 0 ;

 

 case WM_LBUTTONDOWN:

 x = LOWORD(lParam);

 y = HIWORD(lParam);

 GetLocalTime(&systime);

 wsprintf(str, "[%d年%d月%d日%d时%d分%d秒]   ",

 systime.wYear, systime.wMonth, systime.wDay,

 systime.wHour, systime.wMinute, systime.wSecond);

 hdc = GetDC(hwnd);

 //输出字符串

 TextOut(hdc, x, y, str, strlen(str));

 ReleaseDC(hwnd, hdc);

 break;

 return 0;

 

     case WM_PAINT:

          hdc = BeginPaint (hwnd, &ps) ;

          

          GetClientRect (hwnd, &rect) ;

          

          DrawText (hdc, TEXT ("Hello, world"), -1, &rect,

                    DT_SINGLELINE | DT_CENTER | DT_VCENTER) ;

          EndPaint (hwnd, &ps) ;

          return 0 ;

          

     case WM_DESTROY:

          PostQuitMessage (0) ;

          return 0 ;

     }

     return DefWindowProc (hwnd,message,wParam,lParam) ;

}

 

上面是我修改后的源代码

 

截图明显看出已经正确的实现了功能。当然我也思考过如何打印一次时间把上一次的使时间消除掉。因为老师没有要求,所以我也没有花时间实现这个功能了。感兴趣的可以自己尝试尝试。

Practice2 Add thread to DrawRect

Add thread to solve the problem in the program

题目源代码运行后,主要问题是,当我们左击鼠标一切正常,然而我们右击鼠标时,程序会进入一个无限画画的死循环,题目要求的是添加线程以解决问题。现在给出分析和源代码。

这里我用的是CreateThread()函数来建立新线程,下面是CreateThread()函数的定义,我们接下来将根据这个定义建立线程。

HANDLE CreateThread(    

  LPSECURITY_ATTRIBUTES lpThreadAttributes, // 安全属性,一般为NULL  

  DWORD dwStackSize,                        // 初始的栈大小,给NULL即可(系统会设置默认值)  

  LPTHREAD_START_ROUTINE lpStartAddress,    // 线程函数地址  

  LPVOID lpParameter,                       // 线程参数,传递给线程函数的参数  

  DWORD dwCreationFlags,                    // 创建的操作,如挂起等操作,为NULL则立即运行  

  LPDWORD lpThreadId                        // 线程的ID,一般不需要,给NULL  

);  

同时我们将while(1)那整个循环放入到线程函数中

DWORD WINAPI printThread(LPVOID lpParameter)

{

HBRUSH hBrush;

HDC    hdc;

int    xLeft, xRight, yTop, yBottom, iRed, iGreen, iBlue;

while (1)

{

if (cxClient != 0 || cyClient != 0)

{

xLeft = rand() % cxClient;

xRight = rand() % cxClient;

yTop = rand() % cyClient;

yBottom = rand() % cyClient;

iRed = rand() & 255;

iGreen = rand() & 255;

iBlue = rand() & 255;

 

hdc = GetDC(hwnd);

hBrush = CreateSolidBrush(RGB(iRed, iGreen, iBlue));

SelectObject(hdc, hBrush);

 

Rectangle(hdc, min(xLeft, xRight), min(yTop, yBottom),

max(xLeft, xRight), max(yTop, yBottom));

 

ReleaseDC(hwnd, hdc);

DeleteObject(hBrush);

}

}

return 0;

}

接下来每点击右键一次,新建一个线程,然而那个线程并行运行,因此并不影响我们“看到”的线程的操作。


现在是左键情况,我们现在点击右键观察


 这里注意右边诊断绘画里进行内存的变化,很明显看出进行内存使用变多,意思也就是那些线程被成功创建了!

#include <windows.h>

#include <process.h>

 

LRESULT CALLBACK WndProc (HWND,UINT,WPARAM, LPARAM) ;

 

HWND hwnd ;

int  cxClient, cyClient ;

 

int WINAPI WinMain (HINSTANCE hInstance,HINSTANCE hPrevInstance,

                    PSTR szCmdLine,int iCmdShow)

{

     static TCHAR szAppName[] =TEXT ("RndRctMT") ;

     MSG          msg ;

     WNDCLASS     wndclass ;

     

     wndclass.style         = CS_HREDRAW |CS_VREDRAW ;

     wndclass.lpfnWndProc   = WndProc ;

     wndclass.cbClsExtra    = 0 ;

     wndclass.cbWndExtra    = 0 ;

     wndclass.hInstance     = hInstance ;

     wndclass.hIcon         = LoadIcon (NULL,IDI_APPLICATION) ;

     wndclass.hCursor       = LoadCursor (NULL,IDC_ARROW) ;

     wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;

     wndclass.lpszMenuName  = NULL ;

     wndclass.lpszClassName = szAppName ;

     

     if (!RegisterClass (&wndclass))

     {

          MessageBox (NULL,TEXT ("This program requires Windows NT!"),

                      szAppName, MB_ICONERROR) ;

          return 0 ;

     }

     

     hwnd = CreateWindow (szAppName,TEXT ("Random Rectangles"),

                          WS_OVERLAPPEDWINDOW,

                          CW_USEDEFAULT,CW_USEDEFAULT,

                          CW_USEDEFAULT,CW_USEDEFAULT,

                          NULL, NULL, hInstance, NULL) ;

     

     ShowWindow (hwnd, iCmdShow) ;

     UpdateWindow (hwnd) ;

     

     while (GetMessage (&msg,NULL, 0, 0))

     {

          TranslateMessage (&msg) ;

          DispatchMessage (&msg) ;

     }

     return msg.wParam ;

}

 

DWORD WINAPI printThread(LPVOID lpParameter)

{

HBRUSH hBrush;

HDC    hdc;

int    xLeft, xRight, yTop, yBottom, iRed, iGreen, iBlue;

while (1)

{

if (cxClient != 0 || cyClient != 0)

{

xLeft = rand() % cxClient;

xRight = rand() % cxClient;

yTop = rand() % cyClient;

yBottom = rand() % cyClient;

iRed = rand() & 255;

iGreen = rand() & 255;

iBlue = rand() & 255;

 

hdc = GetDC(hwnd);

hBrush = CreateSolidBrush(RGB(iRed, iGreen, iBlue));

SelectObject(hdc, hBrush);

 

Rectangle(hdc, min(xLeft, xRight), min(yTop, yBottom),

max(xLeft, xRight), max(yTop, yBottom));

 

ReleaseDC(hwnd, hdc);

DeleteObject(hBrush);

}

}

return 0;

}

LRESULT CALLBACK WndProc (HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam)

{

 

 

     switch (message)

     {

     case WM_CREATE:

          return 0;

     case WM_RBUTTONDOWN:

 HANDLE hThread = CreateThread(0, 0, printThread, (LPVOID)10,CREATE_SUSPENDED, 0);

 return 0 ;

 case WM_LBUTTONDOWN:

 MessageBox(hwnd,"Left button down!","点击左键",MB_OK);

 return 0;

     case WM_SIZE:

          cxClient = LOWORD (lParam) ;

          cyClient = HIWORD (lParam) ;

          return 0 ;

          

     case WM_DESTROY:

          PostQuitMessage (0) ;

          return 0 ;

     }

     return DefWindowProc (hwnd,message,wParam, lParam) ;

}

这是源代码

注意在vs下编译

 HANDLE hThread = CreateThread(0, 0, printThread, (LPVOID)10, CREATE_SUSPENDED, 0);会报错,但是却不影响使用,所以我也没深究了。

 

P3

新建两个线程然后让他们在主函数里运行之后用WaitForMultipleObjects函数等待线程结束,P3是SSD6 Exercise6的原题,我在这里也就直接给出简单分析和标准答案了。

/*

ThreadedClient.cpp

 

A threaded database client.

 */

 

#include <iostream>

 

#include "servers.h"

 

#ifdef _MSC_VER

#include <windows.h>

#include <winbase.h>

#include <process.h>

typedef __int64 INT64_T;

typedef unsigned __int64 UINT64_T;

 

typedef LONGLONG time_ms_t;

 

time_ms_t getTimeInMilliseconds() {

SYSTEMTIME stime;

GetSystemTime( &stime );

 

FILETIME ftime;

LARGE_INTEGER time;

 

SystemTimeToFileTime( &stime, &ftime ); /* if this fails... */

 

time.HighPart = ftime.dwHighDateTime;

time.LowPart = ftime.dwLowDateTime;

 

/* FileTime is in 100ns intervals since 1/1/1601 */

return time.QuadPart / 10000;

}

#endif

 

/*

ostream &operator <<( ostream &out, string *str ) {

 

Send a string to an output stream.

 */

ostream &operator<<(ostream &out,string *str) {

if (str)

return out << str->data();

else

return out;

}

 

/*

    ostream & operator<< ( ostream &out, INT64_T num )

 

Print a 64-bit unsigned integer to the given output stream.

  */

ostream &operator<<(ostream &out,INT64_T snum ) {

#define _OSTR_LL_LEN 21

if (snum) {

char buffer[_OSTR_LL_LEN];

int i = _OSTR_LL_LEN - 1;

        UINT64_T num;

if (snum < 0) num = -snum;else num = snum;

 

buffer[i] = '\0';

while (num) {

buffer[--i] = ('0' + (int) (num % 10));

num /= 10;

}

if (snum < 0) buffer[--i] ='-';

return out << buffer + i;

}else return out << '0';

}

 

/*

  These are the two thread routines, each of which will run as

  as a separate thread

*/

 

Personal *global_personal = NULL;

void __cdecl personal_thread(void *arg ) {

int acct = *( (int *)arg );

global_personal = GetPersonalInformation( acct );

}

 

AccountInfo *global_account = NULL;

void __cdecl account_thread(void *arg ) {

int acct = *( (int *)arg );

global_account = GetAccountInformation( acct );

}

 

/*

int main( int argc, char *argv[]

 

You should modify this function to use threads.

 */

int main( int argc, char *argv[] ) {

  HANDLE handles[2];

  if (argc != 2) {

    cerr << "usage: " << argv[0]<< " [account_number]" << endl;

    exit(1);

  }

  int account = atoi( argv[1] );

  

  time_ms_t start = getTimeInMilliseconds();

  cout << "Retrieving...";

  cout.flush();

  

  handles[0] = (HANDLE) _beginthread( personal_thread, 0, &account );

  if (handles[0] == (HANDLE) -1L) {

    cout << "Couldn't create thread!" << endl;

    exit(1);

  }

  

  handles[1] = (HANDLE) _beginthread( account_thread, 0, &account );

  if (handles[1] == (HANDLE) -1L) {

    cout << "Couldn't create thread!" << endl;

    exit(1);

  }

  

  WaitForMultipleObjects( 2, handles, TRUE,INFINITE );

  cout << "done" << endl;

  

  time_ms_t end = getTimeInMilliseconds();

  cout << "done (" << end - start <<"ms)" << endl;

  

  if (global_personal) {

    cout << account << ": " << global_personal->FirstName<< " "

 << global_personal->LastName << endl;

    cout << global_personal->Address<< endl;

    cout << "Balance: " << global_account->Balance<<  ", " 

 << global_account->Pending

 << " pending, " << global_account->Share<< " share" << endl;

    

  }

  delete global_personal;

  delete global_account;

 

  return 0;

}

P4

程序问题

 

打印出的count的值随机,

现象名称:(终止)异常

发生原因:_sleep()函数提前终止了线程运行,将_sleep()函数删去,发现运行结果正常为0

#include <stdio.h>

#include <process.h>

#include <windows.h>

 

 

int count=0;

 

void thread1(void* pvoid)

{

while(count<1000000000)

{

       count++;

}

}

 

void thread2(void* pvoid)

{

while(count>-1000000000)

{

       count--;

}

}

 

int main()

{

_beginthread(thread1,0,NULL);

_beginthread(thread2,0,NULL);

 

//_sleep(100);

printf("%d\n",count);

getchar();

return 0;

}


P5单选题,SSD6有现成的答案,我写这篇博客的原因是因为之前的lab'中参阅了不少前辈们的答案,在做lab14发现暂无答案,希望自己的思路能帮助到后面的同学。


Introduction The usual implementation of malloc and free are unforgiving to errors in their callers' code, including cases where the programmer overflows an array, forgets to free memory, or frees a memory block twice. This often does not affect the program immediately, waiting until the corrupted memory is used later (in the case of overwrites) or gradually accumulating allocated but unused blocks. Thus, debugging can be extremely difficult. In this assignment, you will write a wrapper for the malloc package that will catch errors in the code that calls malloc and free. The skills you will have learned upon the completion of this exercise are pointer arithmetic and a greater understanding of the consequences of subtle memory mistakes. Logistics Unzip debugging_malloc.zip into an empty directory. The files contained are as follows: File(s): Function: debugmalloc.c Contains the implementation of the three functions you will be writing. This is the one file you will be editing and handing in. debugmalloc.h Contains the declaration of the functions, as well as the macros that will call them. driver.c Contains main procedure and the code that will be calling the functions in the malloc package dmhelper.c, dmhelper.h Contain the helper functions and macros that you will be calling from your code grader.pl Perl script that runs your code for the various test cases and gives you feedback based on your current code debugmalloc.dsp Exercise 3 project file debugmalloc.dsw Exercise 3 workspace file tailor.h, getopt.c, getopt.h Tools that are used only by the driver program for I/O purposes. You will not need to know what the code in these files do. Others Required by Visual C++. You do not need to understand their purpose Specification Programs that use this package will call the macros MALLOC and FREE. MALLOC and FREE are used exactly the same way as the malloc() and free() functions in the standard C malloc package. That is, the line void *ptr = MALLOC ( n ) ;will allocate a payload of at least n bytes, and ptr will point to the front of this block. The line FREE(ptr);will cause the payload pointed to by ptr to be deallocated and become available for later use. The macros are defined as follows: #define MALLOC(s) MyMalloc(s, __FILE__, __LINE__) #define FREE(p) MyFree(p, __FILE__, __LINE__) The __FILE__ macro resolves to the filename and __LINE__ resolves to the current line number. The debugmalloc.c file contains three functions that you are required to implement, as shown: void *MyMalloc(size_t size, char *filename, int linenumber); void MyFree(void *ptr, char *filename, int linenumber); int AllocatedSize(); Using the macros above allow MyMalloc and MyFree to be called with the filename and line number of the actual MALLOC and FREE calls, while retaining the same form as the usual malloc package. By default, MyMalloc and MyFree() simply call malloc() and free(), respectively, and return immediately. AllocatedSize() should return the number of bytes currently allocated by the user: the sum of the requested bytes through MALLOC minus the bytes freed using FREE. By default, it simply returns 0 and thus is unimplemented. The definitions are shown below: void *MyMalloc(size_t size, char *filename, int linenumber) { return (malloc(size)); } void MyFree(void *ptr, char *filename, int linenumber) { free(ptr); } int AllocatedSize() { return 0; } Your job is to modify these functions so that they will catch a number of errors that will be described in the next section. There are also two optional functions in the debugmalloc.c file that you can implement: void PrintAllocatedBlocks(); int HeapCheck(); PrintAllocatedBlocks should print out information about all currently allocated blocks. HeapCheck should check all the blocks for possible memory overwrites. Implementation Details To catch the errors, you will allocate a slightly larger amount of space and insert a header and a footer around the "requested payload". MyMalloc() will insert information into this area, and MyFree() will check to see if the information has not changed. The organization of the complete memory block is as shown below: Header Checksum ... Fence Payload Footer Fence Note:MyMalloc() returns a pointer to the payload, not the beginning of the whole block. Also, the ptr parameter passed into MyFree(void *ptr) will point to the payload, not the beginning of the block. Information that you might want to store in this extra (header, footer) area include: a "fence" immediately around the requested payload with a known value like 0xCCDEADCC, so that you can check if it has been changed when the block is freed. the size of the block a checksum for the header to ensure that it has not been corrupted (A checksum of a sequence of bits is calculated by counting the number of "1" bits in the stream. For example, the checksum for "1000100010001000" is 4. It is a simple error detection mechanism.) the filename and line number of the MALLOC() call The errors that can occur are: Error #1: Writing past the beginning of the user's block (through the fence) Error #2: Writing past the end of the user's block (through the fence) Error #3: Corrupting the header information Error #4: Attempting to free an unallocated or already-freed block Error #5: Memory leak detection (user can use ALLOCATEDSIZE to check for leaks at the end of the program) To report the first four errors, call one of these two functions: void error(int errorcode, char *filename, int linenumber); errorcode is the number assigned to the error as stated above. filename and linenumber contain the filename and line number of the line (the free call) in which the error is invoked. For example, call error(2, filename, linenumber) if you come across a situation where the footer fence has been changed. void errorfl(int errorcode, char *filename_malloc, int linenumber_malloc, char *filename_free, int linenumber_free); This is the same as the error(), except there are two sets of filenames and line numbers, one for the statement in which the block was malloc'd, and the other for the statement in which the block was free'd (and the error was invoked). The fact that MyMalloc() and MyFree() are given the filename and line number of the MALLOC() and FREE() call can prove to be very useful when you are reporting errors. The more information you print out, the easier it will be for the programmer to locate the error. Use errorfl() instead of error() whenever possible. errorfl() obviously cannot be used on situations where FREE() is called on an unallocated block, since it was not ever MALLOC'd. Note: You will only be reporting errors from MyFree(). None of the errors can be caught in MyMalloc() In the case of memory leaks, the driver program will call AllocatedSize(), and the grader will look at its return value and possible output. AllocatedSize() should return the number of bytes currently allocated from MALLOC and FREE calls. For example, the code segment: void *ptr1 = MALLOC(10), *ptr2 = MALLOC(8); FREE(ptr2); printf("%d\n", AllocatedSize()); should print out "10". Once you have gotten to the point where you can catch all of the errors, you can go an optional step further and create a global list of allocated blocks. This will allow you to perform analysis of memory leaks and currently allocated memory. You can implement the void PrintAllocatedBlocks() function, which prints out the filename and line number where all currently allocated blocks were MALLOC()'d. A macro is provided for you to use to print out information about a single block in a readable and gradeable format: PRINTBLOCK(int size, char *filename, int linenumber) Also, you can implement the int HeapCheck() function. This should check all of the currently allocated blocks and return -1 if there is an error and 0 if all blocks are valid. In addition, it should print out the information about all of the corrupted blocks, using the macro #define PRINTERROR(int errorcode, char *filename, int linenumber), with errorcode equal to the error number (according to the list described earlier) the block has gone through. You may find that this global list can also allow you to be more specific in your error messages, as it is otherwise difficult to determine the difference between an overwrite of a non-payload area and an attempted FREE() of an unallocated block. Evaluation You are given 7 test cases to work with, plus 1 extra for testing a global list. You can type "debugmalloc -t n" to run the n-th test. You can see the code that is being run in driver.c. If you have Perl installed on your machine, use grader.pl to run all the tests and print out a table of results. There are a total of 100 possible points. Here is a rundown of the test cases and desired output (do not worry about the path of the filename): Test case #1 Code char *str = (char *) MALLOC(12); strcpy(str, "123456789"); FREE(str); printf("Size: %d\n", AllocatedSize()); PrintAllocatedBlocks(); Error # None Correct Output Size: 0 Points worth 10 Details 10 points for not reporting an error and returning 0 in AllocatedSize() Test case #2 Code char *str = (char *) MALLOC(8); strcpy(str, "12345678"); FREE(str); Error # 2 Correct Output Error: Ending edge of the payload has been overwritten. in block allocated at driver.c, line 21 and freed at driver.c, line 23 Points worth 15 Details 6 pts for catching error 3 pts for printing the filename/line numbers 6 pts for correct error message Test case #3 Code char *str = (char *) MALLOC(2); strcpy(str, "12"); FREE(str); Error # 2 Correct Output Error: Ending edge of the payload has been overwritten. in block allocated at driver.c, line 28 and freed at driver.c, line 30 Points worth 15 Details 6 pts for catching error 3 pts for printing the filename/line numbers 6 pts for correct error message Test case #4 Code void *ptr = MALLOC(4); *ptr2 = MALLOC(6); FREE(ptr); printf("Size: %d\n", AllocatedSize()); PrintAllocatedBlocks(); Error # None Correct Output Size: 6 Currently allocated blocks: 6 bytes, created at driver.c, line 34 Points worth 15 Details 15 pts for not reporting an error and returning 6 from AllocatedSize Extra for printing out the extra block Test case #5 Code void *ptr = MALLOC(4); FREE(ptr); FREE(ptr); Error # 4 Correct Output Error: Attempting to free an unallocated block. in block freed at driver.c, line 43 Points worth 15 Details 15 pts for catching error Extra for correct error message Test case #6 Code char *ptr = (char *) MALLOC(4); *((int *) (ptr - 8)) = 8 + (1 << 31); FREE(ptr); Error # 1 or 3 Correct Output Error: Header has been corrupted.or Error: Starting edge of the payload has been overwritten. in block allocated at driver.c, line 47 and freed at driver.c, line 49 Points worth 15 Details 9 pts for catching error 6 pts for a correct error message Test case #7 Code char ptr[5]; FREE(ptr); Error # 4 Correct Output Error: Attempting to free an unallocated block. in block freed at driver.c, line 54 Points worth 15 Details 15 pts for recognizing error Extra for printing correct error message Test case #8 (Optional) Code int i; int *intptr = (int *) MALLOC(6); char *str = (char *) MALLOC(12); for(i = 0; i < 6; i++) { intptr[i] = i; } if (HeapCheck() == -1) { printf("\nCaught Errors\n"); } Error # None Correct Output Error: Ending edge of the payload has been overwritten. Invalid block created at driver.c, line 59 Caught Errors Points worth Extra Details "Caught Errors" indicates that the HeapCheck() function worked correctly. Extra points possible. Your instructor may give you extra credit for implementing a global list and the PrintAllocatedBlocks() and HeapCheck() functions.
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值