- 변환 STRUCT을 문자의 배열.
- char의 테이블을 int 배열로 변환합니다.
- int 배열을 char 배열로 변환합니다.
- char 배열을 STRUCT로 변환합니다. 실행시
는하지만 그 erreur 있습니다
이 내 코드입니다 : 당신이 날 실행하는 경우이 문제의 원인을 찾기 위해 기쁘게 도와 줄 수
void rServer::convertStruct_to_char (PCryptDATA p) {
// ***********convert struct to Array of char ************
char* frame= new char[p.size];
cout << p.size <<endl;
cout << endl;
memcpy(frame, &p, sizeof(p));
//***********convert Array of char to array of int ************
int taille= p.size;
int* out = new int[taille];
for (int i=0; i<taille+1;i++)
{
out[i]=frame[i];
}
delete [] frame;
//***********convert Array of int to Array of char ************
char* int2char = new char[taille];
for (int i=0; i<taille+1;i++)
{
int2char[i]=out[i];
}
//delete [] int2char;
//***********convert Array of char to STRUCT ************
PCryptDATA t; //Re-make the struct
memcpy(&t, int2char, sizeof(t));
}
. 루프의 마지막 반복에서 당신이 out[taille]
에 값을 할당하지만, out
배열 0
저점 taille-1
에서 색인 taille
요소에 할당 된 -
"out"과 "int2char"의 끝을지나 한 문자 씩 쓰고 있습니다 : 크기는'taille + 1'이거나 루프 바운드는'taille'이어야합니다. 또한,'p.size'와'sizeof (p) '사이에 혼란이 있습니다. –
C++의 C 스타일 함수는 심각하게 frowned합니다. 당신은 STL과 iterator를 가지고 있습니다. 왜 손으로 루프를 작성하고 포인터로 놀고 있습니까? –
또한'char'는 일반적으로 1 바이트이고'int'는 2 바이트입니다. int를 char 배열에 복사하면 데이터를 조작 할 수 있습니다. –