次の方法で共有


_malloca

更新 : 2007 年 11 月

スタックにメモリを割り当てます。この関数は、「CRT のセキュリティ強化」に説明されているように、_alloca のセキュリティが強化されたバージョンです。

void *_malloca( 
   size_t size 
);

パラメータ

  • size
    スタックから割り当てられるバイト。

戻り値

_malloca ルーチンは、割り当てられたメモリ領域への void ポインタを返します。戻り値が指すメモリ領域は、どの型のオブジェクトを格納する場合でも、適切にアラインメントされます。size が 0 の場合、_malloca 関数は長さが 0 のアイテムを割り当て、そのアイテムへの有効なポインタを返します。

領域の割り当てが実行できない場合は、スタック オーバーフロー例外が発生します。スタック オーバーフロー例外は C++ の例外ではなく、構造化例外です。C++ の例外処理機能ではなく、構造化例外処理 (SEH) を使用する必要があります。

解説

_malloca は、要求が _ALLOCA_S_THRESHOLD に指定されているバイト数を超える場合に、プログラム スタックまたはヒープから size バイトを割り当てます。_malloca_alloca の違いは、_alloca がサイズに関係なく常にスタックを割り当てることです。_alloca で割り当てたメモリは free を呼び出して解放することが必要なく、また許可もされていないのに対し、_malloca では _freea を使用してメモリを開放する必要があります。デバッグ モードでは、_malloca は常にヒープからメモリを割り当てます。

例外ハンドラ (EH) で _malloca を明示的に呼び出す場合は制限があります。x86 クラスのプロセッサで動作する EH ルーチンは、自身のメモリ フレーム内で処理されるため、外側の関数のスタック ポインタが示す現在位置を基にしたメモリ領域ではタスクを実行しません。一般的な実装には、Windows NT 構造化例外処理 (SEH: structured exception handling) や C++ catch 節の式などがあります。このため、次のようなシナリオで _malloca を明示的に呼び出すと、呼び出した EH ルーチンへ戻る時点でプログラム エラーとなります。

  • Windows NT SEH 例外フィルタ式 : __except (_malloca () )

  • Windows NT SEH 最終例外ハンドラ : __finally(_malloca () )

  • C++ EH catch 句式

ただし、_malloca は、EH ルーチン内から直接呼び出すか、または事前にリストされた EH シナリオのいずれかで呼び出されるアプリケーション供給のコールバックから、直接呼び出すことができます。

5471dc8s.alert_security(ja-jp,VS.90).gifセキュリティに関するメモ :

Windows XP では、try/catch ブロック内で _malloca を呼び出した場合、その catch ブロック内で _resetstkoflw を呼び出す必要があります。

上記の制限に加え、/clr (共通言語ランタイムのコンパイル) オプションを使用する場合は __except ブロックで _malloca を使用できません。詳細については、「/clr の制約」を参照してください。

必要条件

ルーチン

必須ヘッダー

_malloca

<malloc.h>

使用例

// crt_malloca_simple.c
#include <stdio.h>
#include <malloc.h>

void Fn()
{
   char * buf = _malloca( 100 );
   // do something with buf
   _freea( buf );
}

int main()
{
   Fn();
}

// crt_malloca_exception.c
// This program demonstrates the use of
// _malloca and trapping any exceptions
// that may occur.

#include <windows.h>
#include <stdio.h>
#include <malloc.h>

int main()
{
    int     size;
    int     numberRead = 0;
    int     errcode = 0;
    void    *p = NULL;
    void    *pMarker = NULL;

    while (numberRead == 0)
    {
        printf_s("Enter the number of bytes to allocate "
                 "using _malloca: ");
        numberRead = scanf_s("%d", &size);
    }

    // Do not use try/catch for _malloca,
    // use __try/__except, since _malloca throws
    // Structured Exceptions, not C++ exceptions.

    __try
    {
        if (size > 0)
        {
            p =  _malloca( size );
        }
        else
        {
            printf_s("Size must be a positive number.");
        }
        _freea( p );
    }

    // Catch any exceptions that may occur.
    __except( GetExceptionCode() == STATUS_STACK_OVERFLOW )
    {
        printf_s("_malloca failed!\n");

        // If the stack overflows, use this function to restore.
        errcode = _resetstkoflw();
        if (errcode)
        {
            printf("Could not reset the stack!");
            _exit(1);
        }
    };
}

1000

Enter the number of bytes to allocate using _malloca: 1000
Allocated 1000 bytes of stack at 0x0012FAC4

.NET Framework の相当するアイテム

適用できません。標準 C 関数を呼び出すには、PInvoke を使用します。詳細については、「プラットフォーム呼び出しの例」を参照してください。

参照

参照

メモリ割り当て

calloc

malloc

realloc

_resetstkoflw