ポインタ割り当ての直後に関数が呼び出されたときに、セグメンテーション違反が発生しました。ポインタ割り当ての後にセグメンテーションエラーが発生するのはなぜですか?
typedef struct
{
DMINT field1;
DMINT field2;
DMINT field3;
} MSG1;
typedef struct
{
....
} MSG;
/* MSG is size of 1040 byte, bigger than MSG1 in size */
int main()
{
MSG Msg;
MSG1 *pMsg1;
int mid;
pthread_t tid;
...
Recv_msg(mid, &Msg); /* this function does a memcpy to &Msg */
pMsg1 = (MSG1 *)&Msg;
//ret = pthread_join(pMsg1->.....); /* Got Segmentation fault here by GDB*/
/* even the first argument has nothing to do with pMsg1, SEGV is still received */
ret = pthread_creat(&tid, NULL, thread_function, NULL); /* Got Segmentation fault here by GDB*/
pMsg1 = (MSG1 *)&Msg
を削除しても問題ありません。 2つのポインタのサイズが異なるためですか?
ありがとうございました。
もう少し情報を提供する必要があります。例: 'Recv_msg'では何をしていますか?ソースコードをお願いします。 – Constantinius
はもっと多くのコードを表示します。逆参照されると、構造体内のポインタを含むすべてのポインタが初期化され、確実にNULLにならないようにしてください。 'valgrind'を使ってメモリリークのバグを追跡することも考えてください。 –
これを診断し始めても、他の人にはもっと多くの情報が必要です。 – tbert