2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

C++&Win32 API 現在のスレッドのスタック情報を取得する

Last updated at Posted at 2020-09-10

C/C++では変数はいくつかのメモリ領域に確保されますが、ローカル変数の多くはスタックに確保されます。このスタックの情報、特に現在のスレッドのスタック情報を取得するにはWin32 APIのGetCurrentThreadStackLimits関数が利用できます。

void GetCurrentThreadStackLimits(
  PULONG_PTR LowLimit,
  PULONG_PTR HighLimit
);

GetCurrentThreadStackLimits function (processthreadsapi.h) - Win32 apps | Microsoft Docs

この関数はスタックのアドレス下限LowLimitと上限HighLimitを返すので、その差HighLimit - LowLimitからサイズが得られます。これをC++&STLで実装したコードを以下に示します。

// Win32 APIで現在のスレッドのスタックのアドレス上限・下限とサイズを取得する。
//

# include <iostream>
# include <iomanip>

# define STRICT
# define NOMINMAX
# include <Windows.h>

int main()
{
	// UNICODEの有効化
	// locale::ctypeは数値出力時のカンマ非表示に必要
	std::wcout.imbue(std::locale("", std::locale::ctype));

	ULONG_PTR lowLimit, highLimit;
	GetCurrentThreadStackLimits(&lowLimit, &highLimit);
	auto size = highLimit - lowLimit;
	std::wcout << L"スタックのアドレス下限:0x" << std::setw(16) << std::setfill(L'0') << std::hex << lowLimit << std::endl;
	std::wcout << L"スタックのアドレス上限:0x" << std::setw(16) << std::setfill(L'0') << std::hex << highLimit << std::endl;
	std::wcout << L"スタックのサイズ:0x" << std::setw(16) << std::setfill(L'0') << std::hex << size << L"(" << std::dec << size << L")" << std::endl;

	int a = 0;
	ULONG_PTR pa = reinterpret_cast<ULONG_PTR>(&a);
	std::wcout << L"ローカル変数aのアドレス:0x" << std::setw(16) << std::setfill(L'0') << std::hex << pa << std::endl;
	std::wcout << L"aがスタックに含まれる:" << std::boolalpha << (lowLimit <= pa && pa <= highLimit) << std::endl;

	return 0;
}
2
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?