![]() |
![]() |
|
|
型付きバッファのサイズの変更
tpalloc() で割り当てられたバッファのサイズを変更するには、次のように tprealloc(3c) 関数を使用します。
char*
tprealloc(char *ptr, long size)
次の表は、tprealloc() 関数の引数を示しています。
tprealloc() 関数の引数
引数 |
説明 |
---|---|
ptr |
サイズを変更するバッファを指すポインタ。このポインタは、 tpalloc() の呼び出しで設定されます。それ以外の方法で設定されている場合、呼び出しは失敗し、tperrno(5) が TPEINVAL に設定されて、無効な引数がこの関数に渡されたことが示されます。 |
size |
変更後のバッファ・サイズを指定する長精度型 (long)。 |
tprealloc() が返すポインタは、元のバッファと同じタイプのバッファを指します。バッファの場所が変わっている場合があるので、返されたポインタを使用してサイズが変更されたバッファを参照します。 バッファのサイズを増やすために tprealloc() 関数を呼び出すと、バッファに新しい領域が割り当てられます。バッファのサイズを減らすために tprealloc() 関数を呼び出すと、実際にバッファのサイズが変更されるのでなく、指定されたサイズを超える領域が使用不能になります。型付きバッファの内容には影響しません。未使用領域を解放するには、必要なサイズを持つ別のバッファにデータをコピーし、未使用領域を持つバッファを解放します。 tprealloc() 関数は、エラーが発生すると NULL ポインタを返し、tperrno に適切な値を設定します。エラー・コードについては、『BEA Tuxedo C リファレンス』の tpalloc(3c) を参照してください。 警告 tprealloc() 関数が NULL ポインタを返した場合、内容が変更されて有効ではなくなったバッファが渡された可能性があります。 次のコード例は、STRING バッファの領域を再度割り当てる方法を示しています。 バッファのサイズ変更 次の例は前述の例を拡張したもので、発生する可能性があるエラー・コードを確認する方法を示しています。 tprealloc() のエラーの確認 関連項目#include <stdio.h>
#include "atmi.h"
char instr[100]; /* 標準入力からの入力文字列を格納する文字列 */
long s1len, s2len; /* 文字列 1 と文字列 2 の長さ */
char *s1ptr, *s2ptr; /* 文字列 1 と文字列 2 のポインタ */
main()
{
(void)gets(instr); /* 標準入力から行を取得します。 */
s1len = (long)strlen(instr)+1; /* 長さを決定します。 */
join application
if ((s1ptr = tpalloc("STRING", NULL, s1len)) == NULL) {
fprintf(stderr, "tpalloc failed for echo of:%s\n", instr);
アプリケーションを終了
exit(1);
}
(void)strcpy(s1ptr, instr);
make communication call with buffer pointed to by s1ptr
(void)gets(instr); /* 標準入力から別の行を取得します。 */
s2len = (long)strlen(instr)+1; /* その長さを決定します。 */
if ((s2ptr = tprealloc(s1ptr, s2len)) == NULL) {
fprintf(stderr, "tprealloc failed for echo of:%s\n", instr);
free s1ptr's buffer
アプリケーションを終了
exit(1);
}
(void)strcpy(s2ptr, instr);
make communication call with buffer pointed to by s2ptr
. . .. . .
if ((s2ptr=tprealloc(s1ptr, s2len)) == NULL)
switch(tperrno) {
case TPEINVAL:
fprintf(stderr, "given invalid arguments\n");
fprintf(stderr, "will do tpalloc instead\n");
tpfree(s1ptr);
if ((s2ptr=tpalloc("STRING", NULL, s2len)) == NULL) {
fprintf(stderr, "tpalloc failed for echo of:%s\n", instr);
アプリケーションを終了
exit(1);
}
break;
case TPEPROTO:
fprintf(stderr, "tried to tprealloc before tpinit;\n");
fprintf(stderr, "program error; contact product support\n");
アプリケーションを終了
exit(1);
case TPESYSTEM:
fprintf(stderr,
"BEA Tuxedo error occurred; consult today's userlog file\n");
アプリケーションを終了
exit(1);
case TPEOS:
fprintf(stderr, "Operating System error %d occurred\n",Uunixerr);
アプリケーションを終了
exit(1);
default:
fprintf(stderr,
"Error from tpalloc:%s\n", tpstrerror(tperrno));
break;
}
![]() |
![]() |
![]() |
|
Copyright © 2001 BEA Systems, Inc. All rights reserved.
|