Below you will find pages that utilize the taxonomy term “C++”
Posts
[C++]使用Global Flags偵測記憶體越界錯誤
int _tmain(int argc, _TCHAR* argv[]) { int m_len = 5; char *m_p = (char *)HeapAlloc (GetProcessHeap (), HEAP_ZERO_MEMORY, m_len);
m_p[m_len] = 0;
HeapFree (GetProcessHeap (),0, m_p);
return 0; }
read morePosts
[C++]使用Visual Leak Detector for Visual C++ 2008/2010輔助偵測程式中記憶體洩漏的問題
#include “stdafx.h” #include “vld.h”
int _tmain(int argc, _TCHAR* argv[]) { char* buffer = new char[512]; return 0; }
read morePosts
[C++]event_source attribute的功用
void BindingEvent(){ __hook(&TestObj::Executed,this,&TestObj::OnExecuted); } void UnBindingEvent(){ __hook(&TestObj::Executed,this,&TestObj::OnExecuted); } };
read morePosts
[C++]使用nsiqcppstyle輔助檢查C/C++的Coding Style
[Example] nsiqcppstyle . nsiqcppstyle targetdir nsiqcppstyle -f filefilterpath targetfilepath
[Options] -h Show this help -v Show detail ouput(verbose mode) -r Show rule list -o path Set the output path. It’s only applied when the output is csv or xml. -f path Set the filefilter path. If not provided, it uses the default fi lterpath (target/filefilter.txt) If you provide the file path(not folder path) for the target, -f option should be provided.
read morePosts
[C++]使用TinyXml讀寫Xml
TiXmlNode* rootElement = xmlDoc.InsertEndChild(TiXmlElement("Person")); rootElement ->InsertEndChild(TiXmlElement("Name")) ->InsertEndChild(TiXmlText(person->m_sName.c_str())); rootElement ->InsertEndChild(TiXmlElement("NickName")) ->InsertEndChild(TiXmlText(person->m_sNickName.c_str())); char buffer[256]; _itoa(person->m_nAge, buffer,10); rootElement ->InsertEndChild(TiXmlElement("Age")) ->InsertEndChild(TiXmlText(buffer)); xmlDoc.SaveFile(file.c_str()); }
xmlDoc.LoadFile(); if(xmlDoc.ErrorId() > 0) return; TiXmlElement* pRootElement = xmlDoc.RootElement(); if(!pRootElement) return; TiXmlElement* pNode = NULL; pNode = pRootElement->FirstChildElement("Name"); if(pNode) { person->m_sName = pNode->GetText(); } pNode = pRootElement->FirstChildElement("NickName"); if(pNode) { person->m_sNickName = pNode->GetText(); } pNode = pRootElement->FirstChildElement("Age"); if(pNode) { person->m_nAge = atoi(pNode->GetText()); } }
#include “stdafx.h” #include <string> #include “tinyxml.h”
#include “tinystr.
read morePosts
[C++]使用Pageheap偵測記憶體越界錯誤
pageheap Displays pageheap enabled programs
pageheap /enable PROGRAM Enables page heap for PROGRAM pageheap /enable PROGRAM FLAGS Enables page heap for PROGRAM using
FLAGS (hex number) for heap flags.
pageheap /disable PROGRAM Disables page heap for PROGRAM pageheap PROGRAM Displays current setting for PROGRAM
Example: pageheap /enable notepad pageheap /disable pbrush
Note. Enabling page heap does not affect currently running processes. If you need to use page heap for processes that are already running and cannot be restarted (csrss.
read morePosts
[C++]使用ReadDirectoryChangesW API監控檔案系統的改變
hDirectoryHandle = ::CreateFileA( file, FILE_LIST_DIRECTORY, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OVERLAPPED, NULL);
if(hDirectoryHandle == INVALID_HANDLE_VALUE) return;
memset(buffer, 0, nBufferSize);
if(!::ReadDirectoryChangesW( hDirectoryHandle, buffer, nBufferSize, bIncludeSubdirectories, FILE_NOTIFY_CHANGE_LAST_WRITE | FILE_NOTIFY_CHANGE_CREATION | FILE_NOTIFY_CHANGE_FILE_NAME, &dwBytes, NULL, NULL) || GetLastError() == ERROR_INVALID_HANDLE) { break; }
if(!dwBytes) { printf(“Buffer overflow~~ “); }
do { switch (record->Action) { case FILE_ACTION_ADDED: printf(“FILE_ACTION_ADDED:”); break; case FILE_ACTION_REMOVED: printf(“FILE_ACTION_REMOVED:”); break; case FILE_ACTION_MODIFIED: printf(“FILE_ACTION_MODIFIED:”); break; case FILE_ACTION_RENAMED_OLD_NAME: printf(“FILE_ACTION_RENAMED_OLD_NAME:”); break;
read morePosts
[C++]Simple nativated timer class
};
#pragma region Constructor & DeConstructor EventArgs::EventArgs(void) { }
EventArgs::~EventArgs(void) { }
#pragma endregion
[event_source] [event_receiver] class Timer { #pragma region Var private: int _nTimerID; int _nInterval; bool _bIsEnabled; #pragma endregion
#pragma region Private Property private: __declspec(property(get=Get_nTimerID,put=Set_nTimerID)) int m_nTimerID; #pragma endregion
#pragma region Public Property public: __declspec(property(get=Get_nInterval,put=Set_nInterval)) int m_nInterval;
__declspec(property(get=Get_bIsEnabled,put=Set_bIsEnabled)) bool m_bIsEnabled; #pragma endregion
#pragma region Event public: __event void IntervalChanging(void* sender, EventArgs* e); __event void IntervalChanged(void* sender, EventArgs* e); __event void IsEnabledChanging(void* sender, EventArgs* e); __event void IsEnabledChanged(void* sender, EventArgs* e); __event void Tick(void* sender, EventArgs* e); #pragma endregion
read morePosts
[C++]使用Cppcheck靜態分析工具輔助檢查C++程式潛在問題
Syntax: cppcheck [OPTIONS] [files or paths]
If a directory is given instead of a filename, *.cpp, *.cxx, *.cc, *.c++, *.c, *.tpp, and *.txx files are checked recursively from the given directory.
Options: –append=<file> This allows you to provide information about functions by providing an implementation for them. –check-config Check cppcheck configuration. The normal code analysis is disabled by this flag. -D<ID> By default Cppcheck checks all configurations. Use -D to limit the checking.
read morePosts
[C++]C++ Nativated Property With Event Code Snippet
#pragma region Public Property public: declspec(property(get=Get$field$,put=Set$field$)) $type$ m_$field$; #pragma endregion
#pragma region Event public: __event void $field$Changing(void* sender, $eventArgs$* e); __event void $field$Changed(void* sender, $eventArgs$* e); #pragma endregion
#pragma region Property Process Method public: inline $type$ Get_$field$() { return _$field$; }
inline void Set_$field$($type$ value) { if(_$field$ == value) return; $eventArgs$ e; On$field$Changing(&e); _$field$ = value; On$field$Changed(&e); } #pragma endregion
#pragma region Protected Method protected: void On$field$Changing($eventArgs$* e) { __raise $field$Changing(this, e); }
read morePosts
[C++]C++ Nativated Property Code Snippet
#pragma region Public Property public: declspec(property(get=Get$field$,put=Set$field$)) $type$ m_$field$; #pragma endregion
#pragma region Property Process Method public: inline $type$ Get_$field$() { return _$field$; }
inline void Set_$field$($type$ value) { if(_$field$ == value) return; _$field$ = value; } #pragma endregion]]></Code> </Snippet> </CodeSnippet> </CodeSnippets>
read morePosts
[C++]C++ Create GUID
GUID *pguid = new GUID; CoCreateGuid(pguid); // Convert the GUID to a string UuidToString(pguid, (RPC_WSTR*)&guidStr); delete pguid; return wstring(guidStr); }
#include “stdafx.h” #include <objbase.h> #include <string>
using namespace std;
wstring GetGUID() { _TUCHAR *guidStr = NULL;
GUID *pguid = new GUID; CoCreateGuid(pguid); // Convert the GUID to a string UuidToString(pguid, (RPC_WSTR*)&guidStr); delete pguid; return wstring(guidStr); }
int _tmain(int argc, _TCHAR* argv[]) { wstring guid = GetGUID(); wprintf(guid.c_str()); return 0; }
read morePosts
[C++]C++ Simple Lazy class
template<class T> class Lazy { #pragma region Var private: std::function<T (void)> _func; T _result; bool _bIsValueCreated; #pragma endregion
#pragma region Private Property private: __declspec(property(get=Get_func,put=Set_func)) std::function<T (void)> m_func; #pragma endregion
#pragma region Public Property public: __declspec(property(get=Get_result)) T m_result;
__declspec(property(get=Get_bIsValueCreated,put=Set_bIsValueCreated)) bool m_bIsValueCreated; #pragma endregion
#pragma region Constructor & DeConstructor public: Lazy(std::function<T (void)> func) { Reset(); m_func = func; }
~Lazy(void) { Reset(); } #pragma endregion
#pragma region Property Process Method private: inline std::function<T (void)> Get_func() { return _func; }
read morePosts
[C++][Visual Studio]Visual studio 2010 C++0x new feature: lambda
auto result = lambda(1,2);
auto result = lambda(1,2);
auto lambda = = -> int { return val1 + val2; };
result = lambda();
lambda(1,2);
lambda(1,2);
auto lambda = =, &result { result = val1 + val2; };
lambda();
read morePosts
[C++][Visual Studio]Visual studio 2010 C++0x new feature: nullptr
void Test(int value) { cout << “void Test(int value)”; }
int _tmain(int argc, _TCHAR* argv[]) { Test(NULL); return 0; }
#include “stdafx.h” #include <iostream>
using namespace std;
void Test(int* value) { cout << “void Test(int* value) “; }
void Test(int value) { cout << “void Test(int value) “; }
int _tmain(int argc, _TCHAR* argv[]) { Test(NULL); Test((int*)NULL); Test(nullptr); return 0; }
read morePosts
[C++][Visual Studio]Visual studio 2010 C++0x new feature: auto
#include “stdafx.h” #include <string> #include <list> #include <map>
using namespace std;
int _tmain(int argc, _TCHAR* argv[]) { auto x = 1, *y = &x, **z = &y; // Resolves to int. auto a(2.01), *b (&a); // Resolves to double. auto c = ‘a’, *d(&c); // Resolves to char. auto m = 1, &n = m; // Resolves to int.
map<int,list<string>> mapObj; map<int,list<string>>::iterator i1 = mapObj.begin(); auto i2 = mapObj.begin(); auto lambda = []()->int { return 0; }; return 0; }
read morePosts
[C++][Visual Studio]Visual studio 2010 C++0x new feature: static_assert
#include “stdafx.h”
#define DEFAULT_VALUE 0 #define MAX_VALUE 100
const int VALUE = 10;
struct MyStruct { char data[1024]; };
template < class T, int Size > class Vector { static_assert(Size > 0, “Vector size must be bigger than zero!”);
T m_values[Size]; };
int _tmain(int argc, _TCHAR* argv[]) { static_assert( sizeof(void ) == 4, “64-bit code generation is not supported.”); static_assert( MAX_VALUE > DEFAULT_VALUE, “DEFAULT_VALUE must be smaller than MAX_VALUE” ); static_assert( MAX_VALUE > VALUE, “VALUE must be smaller than MAX_VALUE” ); static_assert( sizeof( MyStruct ) < 10241024, “The structure size exceeds stack size” ); Vector<int, 10> intArray; return 0; }
read morePosts
[C++][Visual Studio]Natived C++使用Visual Studio做單元測試
//Act actual = myObj.Add(x, y); //Assert Assert::AreEqual(actual, expected); }; }; }
read morePosts
[C++/CLI]Nativated物件處理Managed物件的事件
void OnAutoRunTimerTick(System::Object^ sender, System::EventArgs^ e) { _handler->AutoRunTimer_Tick(sender, e); } };
read morePosts
[VC.NET] 如何修復 quot;C2039: lsquo;GetCurrentDirectoryA()rsquo; : is Not a Member of lsquo;System::IO::Directoryrsquo;quot;問題
int main(array<System::String ^> ^args) { Console::WriteLine(L"Dir: “+System::IO::Directory::GetCurrentDirectory()); // Error! return 0; } #undef GetCurrentDirectory
int main(array<System::String ^> ^args) { Console::WriteLine(L"Dir: “+System::IO::Directory::GetCurrentDirectory()); return 0; }
read morePosts
VC.NET Natived Property
int _tmain(int argc, _TCHAR* argv[]) { TestObj obj; obj.m_bIsRunning = true; return 0; }
read morePosts
C++/CLI索引子
int _tmain(int argc, _TCHAR* argv[]) { ArrayClass a; for (int i=0;i<1024;++i){ a[i]=i; } return 0; }
read more