#
#include <windows.h>
#include <stdlib.h>
#include <time.h>
#include <stdio.h>
struct CRITICAL_REGION
{
public:
CRITICAL_SECTION cs;
int gData;
};
DWORD WINAPI writer(LPVOID cr);
DWORD WINAPI reader(LPVOID cr);
int main(int argc,char* argv[])
{
DWORD targetThreadID;
HANDLE writerThread;
HANDLE readerThread;
CRITICAL_REGION cr;
cr.gData=0;
InitializeCriticalSection(&(cr.cs));
writerThread =CreateThread(NULL,0,writer,&cr,0,&targetThreadID);
CloseHandle(writerThread);
readerThread =CreateThread(NULL,0,reader,&cr,0,&targetThreadID);
CloseHandle(readerThread);
Sleep(10000);
printf("end");
return 0;
}
DWORD WINAPI writer(LPVOID cr)
{
Sleep(1000);
DWORD result = 0;
int n = 1;
ExitProcess(0);
while(n<=10)
{
EnterCriticalSection(&((CRITICAL_REGION*)cr)->cs);
if(((CRITICAL_REGION*)cr)->gData==0)
{
((CRITICAL_REGION*)cr)->gData = n;
printf("gData is %d\n",((CRITICAL_REGION*)cr)->gData);
n++;
}
LeaveCriticalSection(&(((CRITICAL_REGION*)cr)->cs));
Sleep(100);
}
return result;
};
DWORD WINAPI reader(LPVOID cr)
{
DWORD result =0;
char u[6];
int n = 1;
while(n<=10)
{
EnterCriticalSection(&((CRITICAL_REGION*)cr)->cs);
if(((CRITICAL_REGION*)cr)->gData!=0)
{
((CRITICAL_REGION*)cr)->gData = 0;
n++;
printf("gData is taken away\n");
}
LeaveCriticalSection(&(((CRITICAL_REGION*)cr)->cs));
Sleep(50);
}
DeleteCriticalSection( &(((CRITICAL_REGION*)cr)->cs));
gets(u);
return result;
};
摘要: // stdafx.h : 标准系统包含文件的包含文件,// 或是经常使用但不常更改的// 特定于项目的包含文件//#pragma once#define WIN32_LEAN_AND_MEAN // 从 Windo...
阅读全文
摘要: #include "Stack.h"CStack::CStack(void){ this->m_pTopMost = new char[100]; this->m_pBottom = &(this->m_pTopMost[...
阅读全文
/**//*****************************************************************
* ?000 Microsoft Corporation *
* CRT_DBG1 *
* This simple program illustrates the basic debugging features *
* of the C runtime libraries, and the kind of debug output *
* that these features generate. *
*****************************************************************/
#include <stdio.h>
#include <string.h>
#include <malloc.h>
#include <crtdbg.h>
// Disable deprecation warnings. The unsecure version of strcpy is
// used intentionally to show off debugging features.
#pragma warning (disable : 4996)
// This routine place comments at the head of a section of debug output
void OutputHeading( const char * explanation )
{
_RPT1( _CRT_WARN, "\n\n%s:\n**************************************\
************************************\n", explanation );
}
// The following macros set and clear, respectively, given bits
// of the C runtime library debug flag, as specified by a bitmask.
#ifdef _DEBUG
#define SET_CRT_DEBUG_FIELD(a) \
_CrtSetDbgFlag((a) | _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG))
#define CLEAR_CRT_DEBUG_FIELD(a) \
_CrtSetDbgFlag(~(a) & _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG))
#else
#define SET_CRT_DEBUG_FIELD(a) ((void) 0)
#define CLEAR_CRT_DEBUG_FIELD(a) ((void) 0)
#endif
int main( )
{
char *p1, *p2;
_CrtMemState s1, s2, s3;
#ifndef _DEBUG
printf("Skipping this for non-debug mode.\n");
return 2;
#endif
// Send all reports to STDOUT
_CrtSetReportMode( _CRT_WARN, _CRTDBG_MODE_FILE );
_CrtSetReportFile( _CRT_WARN, _CRTDBG_FILE_STDOUT );
_CrtSetReportMode( _CRT_ERROR, _CRTDBG_MODE_FILE );
_CrtSetReportFile( _CRT_ERROR, _CRTDBG_FILE_STDOUT );
_CrtSetReportMode( _CRT_ASSERT, _CRTDBG_MODE_FILE );
_CrtSetReportFile( _CRT_ASSERT, _CRTDBG_FILE_STDOUT );
// Allocate 2 memory blocks and store a string in each
p1 = malloc( 34 );
strcpy( p1, "This is the p1 string (34 bytes)." );
p2 = malloc( 34 );
strcpy( p2, "This is the p2 string (34 bytes)." );
OutputHeading(
"Use _ASSERTE to check that the two strings are identical" );
_ASSERTE( strcmp( p1, p2 ) == 0 );
OutputHeading(
"Use a _RPT macro to report the string contents as a warning" );
_RPT2( _CRT_WARN, "p1 points to '%s' and \np2 points to '%s'\n", p1, p2 );
OutputHeading(
"Use _CRTMemDumpAllObjectsSince to check the p1 and p2 allocations" );
_CrtMemDumpAllObjectsSince( NULL );
free( p2 );
OutputHeading(
"Having freed p2, dump allocation information about p1 only" );
_CrtMemDumpAllObjectsSince( NULL );
// Store a memory checkpoint in the s1 memory-state structure
_CrtMemCheckpoint( &s1 );
// Allocate another block, pointed to by p2
p2 = malloc( 38 );
strcpy( p2, "This new p2 string occupies 38 bytes.");
// Store a 2nd memory checkpoint in s2
_CrtMemCheckpoint( &s2 );
OutputHeading(
"Dump the changes that occurred between two memory checkpoints" );
if ( _CrtMemDifference( &s3, &s1, &s2 ) )
_CrtMemDumpStatistics( &s3 );
// Free p2 again and store a new memory checkpoint in s2
free( p2 );
_CrtMemCheckpoint( &s2 );
OutputHeading(
"Now the memory state at the two checkpoints is the same" );
if ( _CrtMemDifference( &s3, &s1, &s2 ) )
_CrtMemDumpStatistics( &s3 );
strcpy( p1, "This new p1 string is over 34 bytes" );
OutputHeading( "Free p1 after overwriting the end of the allocation" );
free( p1 );
// Set the debug-heap flag so that freed blocks are kept on the
// linked list, to catch any inadvertent use of freed memory
SET_CRT_DEBUG_FIELD( _CRTDBG_DELAY_FREE_MEM_DF );
p1 = malloc( 10 );
free( p1 );
strcpy( p1, "Oops" );
OutputHeading( "Perform a memory check after corrupting freed memory" );
_CrtCheckMemory( );
// Use explicit calls to _malloc_dbg to save file name and line number
// information, and also to allocate Client type blocks for tracking
p1 = _malloc_dbg( 40, _NORMAL_BLOCK, __FILE__, __LINE__ );
p2 = _malloc_dbg( 40, _CLIENT_BLOCK, __FILE__, __LINE__ );
strcpy( p1, "p1 points to a Normal allocation block" );
strcpy( p2, "p2 points to a Client allocation block" );
// You must use _free_dbg to free a Client block
OutputHeading(
"Using free( ) to free a Client block causes an assertion failure" );
free( p1 );
free( p2 );
p1 = malloc( 10 );
OutputHeading( "Examine outstanding allocations (dump memory leaks)" );
_CrtDumpMemoryLeaks( );
// Set the debug-heap flag so that memory leaks are reported when
// the process terminates. Then, exit.
OutputHeading( "Program exits without freeing a memory block" );
SET_CRT_DEBUG_FIELD( _CRTDBG_LEAK_CHECK_DF );
}
大概解释一下,这里的函数都是什么作用:
1、首先是刚开始的六个函数,这六个函数的作用就是让接下来的内存调试函数输出调试信息到控制台。后面的函数都是实际调试内存的,如果之前调用了这六个函数,那么调试信息会输出到控制台。
2、_CrtMemDumpAllObjectsSince( NULL )函数的作用是输出目前为止,在堆中申请的空间的所有信息,包括地址,大小,内容。
3、_CrtDumpMemoryLeaks( )函数的作用是检测到目前为止,还有哪些堆中申请的内存没有释放,包括地址,大小等
4、SET_CRT_DEBUG_FIELD( _CRTDBG_LEAK_CHECK_DF )宏的作用是在程序结束的时候检测堆内存是否还有泄露,作用同CrtDumpMemoryLeaks( )一样,只不过是在所有该释放的都结束之后,进行最后的检查。
(未完,以后补充)
//这是C++文件打开的部分实现fiopen.cpp文件的一个函数,看了就明白了
_CRTIMP2_PURE FILE *__CLRCALL_PURE_OR_CDECL _Fiopen(const _Sysch_t *filename,
ios_base::openmode mode, int prot)
{ // open a file with native name
static const _Sysch_t *mods[] =
{ // fopen mode strings corresponding to valid[i]
_SYSCH("r"), _SYSCH("w"), _SYSCH("w"), _SYSCH("a"),
_SYSCH("rb"), _SYSCH("wb"), _SYSCH("wb"), _SYSCH("ab"),
_SYSCH("r+"), _SYSCH("w+"), _SYSCH("a+"),
_SYSCH("r+b"), _SYSCH("w+b"), _SYSCH("a+b"),
0};
static const int valid[] =
{ // valid combinations of open flags
ios_base::in,
ios_base::out,
ios_base::out | ios_base::trunc,
ios_base::out | ios_base::app,
ios_base::in | ios_base::binary,
ios_base::out | ios_base::binary,
ios_base::out | ios_base::trunc | ios_base::binary,
ios_base::out | ios_base::app | ios_base::binary,
ios_base::in | ios_base::out,
ios_base::in | ios_base::out | ios_base::trunc,
ios_base::in | ios_base::out | ios_base::app,
ios_base::in | ios_base::out | ios_base::binary,
ios_base::in | ios_base::out | ios_base::trunc
| ios_base::binary,
ios_base::in | ios_base::out | ios_base::app
| ios_base::binary,
0};
FILE *fp = 0;
int n;
ios_base::openmode atendflag = mode & ios_base::ate;
ios_base::openmode norepflag = mode & ios_base::_Noreplace;
if (mode & ios_base::_Nocreate)
mode |= ios_base::in; // file must exist
if (mode & ios_base::app)
mode |= ios_base::out; // extension -- app implies out
mode &= ~(ios_base::ate | ios_base::_Nocreate | ios_base::_Noreplace);
for (n = 0; valid[n] != 0 && valid[n] != mode; ++n)
; // look for a valid mode
if (valid[n] == 0)
return (0); // no valid mode
else if (norepflag && mode & (ios_base::out || ios_base::app)
&& (fp = _Xfsopen(filename, _SYSCH("r"), prot)) != 0)
{ // file must not exist, close and fail
fclose(fp);
return (0);
}
else if (fp != 0 && fclose(fp) != 0)
return (0); // can't close after test open
else if ((fp = _Xfsopen(filename, mods[n], prot)) == 0)
return (0); // open failed
if (!atendflag || fseek(fp, 0, SEEK_END) == 0)
return (fp); // no need to seek to end, or seek succeeded
fclose(fp); // can't position at end
return (0);
}
摘要: #include <stdio.h>#include <tchar.h>#include<iostream>#include<exdisp.h>#include <atlbase.h> #include <atlcom.h>#include<mshtml.h&...
阅读全文
摘要: //Computer.h#pragma once#include"CPU.h"#include"Memory.h"#include"MainBoard.h"#include"Monitor.h"class CComputer{private: CCPU m_cpu; CMemo...
阅读全文
C++编程练习1
实现一个计算机的Class的层次结构
l 类CPU
Method :设定厂商名称
:取得厂商名称
:取得价格
:设定主频
:取得价格实现方法:根据主频的范围和厂商确定(自己自由发挥)
l 类Memory
Method :设定厂商名称
:取得厂商名称
:取得价格(自己自由发挥)
:设定大小
:取得价格实现方法:根据内存大小的范围和厂商确定
l 类MainBoard
Method :设定厂商名称
:取得厂商名称
:取得价格(自己自由发挥)
:取得价格实现方法:根据厂商确定
:Plug(CCPU*,CMemory* )
:SelfCheck() 检察是否plug过正确的CPU, Memory
l 类Monitor
Method :设定厂商名称
:取得厂商名称
:取得价格
:设定大小
:设定类型:一般。液晶
:取得价格实现方法:根据大小,是否液晶和厂商确定(自己自由发挥)
类计算机:
包含以上几个类的成员
Method :设定CPU主频
:设定CPU厂商(A,B,C)
:设定Memory大小(128,256,512)
:设定Memory厂商(A,B,C)
:设定显示器大小(14,15,17,19),类型(一般,液晶)
:设定显示器厂商(A,B,C);
:设定主版的厂商(A,B,C);
:察看整机价格(打印到屏幕)
:察看配置(打印到屏幕)
:Init() (调用MainBoard.Plug())
:Start() (调用MainBoard.SelfCheck() )
l Computer类:包含上面几个类的对象。
写一个小程序,动态创建一个10个Computer的数组,设定察看每一台的配置,计算总价格等。
要求正确的释放对象的数组。