内存读取以及内存对齐

 C++ Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
TravelInfo.h文件 
/*
 * TravelInfo.h
 *
 *  Created on: 2011-11-30
 */

#ifndef TRAVELINFO_H_
#define TRAVELINFO_H_
typedef  unsigned  char u_char;
typedef  unsigned  short u_short;
struct CoordinateInfo
{
  char latChange;
  short latitude;
  char lonChange;
  short longitude;
  short direction;
 u_short speed;
  char additionalInfo;
 u_char counter;
 u_char routeType;
 u_char matchingLevel;
};
struct Head
{
 u_char year;
 u_char month;
 u_char day;
 u_char hour;
 u_char minute;
 u_char second;
 u_char latDegree;
 u_char latPoint;
 u_char lonDegree;
 u_char lonPoint;
 u_short coordinateNum;
 CoordinateInfo *coordinateInfoList;
};
struct Management
{
 u_short measureinfoNum;
 u_char interval;
 u_char mapVersion;
 u_char additionalInfoFlag;
 u_char vehicleType;
  short nothing1;
  short nothing2;
  short nothing3;
 Head *headList;
};
class TravelInfo
{
private:
  static  char *buffer;
public:
 TravelInfo();
 ~TravelInfo();
  char * buildData();
  void parserData();
  void print();
};
#endif  /* TRAVELINFO_H_ */
TravelInfo.cpp文件
//============================================================================
// Name        : TravelInfo.cpp
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================
#ifndef TRAVELINFO_H_
#include  "TravelInfo.h"
#endif
#include <iostream>
using  namespace std;
char *TravelInfo::buffer =  NULL;
TravelInfo::TravelInfo( void)
{
 buffer =  new  char[ 10* 1024];
}
TravelInfo::~TravelInfo( void)
{
}
char*
TravelInfo::buildData( void)
{
  int offset =  0;
 Management m;
 m.measureinfoNum =  2;
 m.interval =  60;
 m.mapVersion =  1;
 m.additionalInfoFlag =  1;
 m.vehicleType =  1;
 m.nothing1 =  0;
 m.nothing2 =  0;
 m.nothing3 =  0;
 m.headList =  new Head[ 2];
 memcpy(buffer, &m,  sizeof(Management) -  sizeof(m.headList));   // headerList is a point
 offset =  sizeof(Management) -  sizeof(m.headList);
  for( int i =  0; i <  2; i++)
 {
  Head head;
  head.year =  2;
  head.month =  7;
  head.day =  19;
  head.hour =  14;
  head.minute =  25 + i;
  head.second =  30;
  head.latDegree =  35+i;
  head.latPoint =  13+i;
  head.lonDegree =  65+i;
  head.lonPoint =  24+i;
  head.coordinateNum =  2;
  head.coordinateInfoList =  new CoordinateInfo[ 2];
  memcpy(buffer+offset, &head,  sizeof(Head) -  sizeof(head.coordinateInfoList));
  offset = offset +  sizeof(Head) -  sizeof(head.coordinateInfoList);
   for( int j =  0; j< 2; j++)
  {
   CoordinateInfo c;
   memset(&c,  0sizeof(CoordinateInfo));
   c.latChange = - 1;
   c.latitude =  3456+j;
   c.lonChange = - 1;
   c.longitude =  4567+j;
   c.direction =  0+j;
   c.speed =  60+j;
   c.additionalInfo =  'P';
   c.counter =  10+j;
   c.routeType =  1+j;
   c.matchingLevel =  1+j;
   memcpy(buffer+offset, &c,  sizeof(CoordinateInfo));
   offset = offset +  sizeof(CoordinateInfo);
   head.coordinateInfoList[j] = c;
  }
  m.headList[i] = head;
 }
  return buffer;
}
void
TravelInfo::parserData( void)
{
  char * dataBuffer = buildData();
  int offset =  0;
 Management m;
  // Copy ManagePart from dataBuffer
 memcpy(&m, dataBuffer,  sizeof(Management)- sizeof(m.headList));
 offset = offset +  sizeof(Management)- sizeof(m.headList);
  // Build the list of HeaderPart
 m.headList =  new Head[m.measureinfoNum];
  for( int i =  0;i < m.measureinfoNum;i++)
 {
   // Copy HeaderParts from dataBuffer
  memcpy(m.headList+i, dataBuffer+offset,  sizeof(Head)- sizeof(m.headList[i].coordinateInfoList));
  offset = offset +  sizeof(Head)- sizeof(m.headList[i].coordinateInfoList);
   // Build the coordinateInfoList of every HeaderPart
  m.headList[i].coordinateInfoList =  new CoordinateInfo[m.headList[i].coordinateNum];
   for( int j= 0; j < m.headList[i].coordinateNum; j++)
  {
    // Copy CoordinateInfos from dataBuffer
   memcpy(m.headList[i].coordinateInfoList+j, dataBuffer+offset,  sizeof(CoordinateInfo));
   offset = offset +  sizeof(CoordinateInfo);
  }
 }
 cout<< "-----------------------管理部(Start)------------------------"<<endl;
  int measureinfoNum = m.measureinfoNum;
 cout<< "measureinfoNum(測定情報数) : "<<( int)measureinfoNum<<endl;
 cout<< "interval(座標間隔): "<<( int)m.interval<<endl;
 cout<< "mapVersion(地図Ver): "<<( int)m.mapVersion<<endl;
 cout<< "additionalInfoFlag(付加情報flag): "<<( int)m.additionalInfoFlag<<endl;
 cout<< "vehicleType(車載機種別): "<<( int)m.vehicleType<<endl;
 cout<< "nothing1(空領域): "<<( int)m.nothing1<<endl;
 cout<< "nothing2(空領域): "<<( int)m.nothing2<<endl;
 cout<< "nothing3(空領域): "<<( int)m.nothing3<<endl;
 cout<< "-----------------------管理部(End)------------------------"<<endl;
  for( int i =  0; i <measureinfoNum; i++)
 {
  cout<< "--------------------------Header部"<<i+ 1<< "(Start)---------------------------"<<endl;
  cout<< "year(年): "<<( int)m.headList[i].year<<endl;
  cout<< "month(月): "<<( int)m.headList[i].month<<endl;
  cout<< "day(日): "<<( int)m.headList[i].day<<endl;
  cout<< "hour(时): "<<( int)m.headList[i].hour<<endl;
  cout<< "minute(分): "<<( int)m.headList[i].minute<<endl;
  cout<< "second(秒): "<<( int)m.headList[i].second<<endl;
  cout<< "latDegree(緯度(度)): "<<( int)m.headList[i].latDegree<<endl;
  cout<< "latPoint(緯度(分)): "<<( int)m.headList[i].latPoint<<endl;
  cout<< "lonPoint(経度(度)): "<<( int)m.headList[i].lonDegree<<endl;
  cout<< "lonPoint(経度(分)): "<<( int)m.headList[i].lonPoint<<endl;
   int coordinateNum = m.headList[i].coordinateNum;
  cout<< "coordinateNum(座標件数): "<<coordinateNum<<endl;
   for( int j =  0; j < coordinateNum; j++)
  {
   cout<< "----------------座標情報"<<j+ 1<< "(Start)----------------"<<endl;
   cout<< "latChange(経変): "<<( int)m.headList[i].coordinateInfoList[j].latChange<<endl;
   cout<< "latitude(経度): "<<( int)m.headList[i].coordinateInfoList[j].latitude<<endl;
   cout<< "lonChange(緯変): "<<( int)m.headList[i].coordinateInfoList[j].lonChange<<endl;
   cout<< "longitude(緯度): "<<( int)m.headList[i].coordinateInfoList[j].longitude<<endl;
   cout<< "direction(方位): "<<( int)m.headList[i].coordinateInfoList[j].direction<<endl;
   cout<< "speed(速度): "<<( int)m.headList[i].coordinateInfoList[j].speed<<endl;
   cout<< "additionalInfo(付加情報): "<<( char)m.headList[i].coordinateInfoList[j].additionalInfo<<endl;
   cout<< "counter: "<<( int)m.headList[i].coordinateInfoList[j].counter<<endl;
   cout<< "routeType(道路種別): "<<( int)m.headList[i].coordinateInfoList[j].routeType<<endl;
   cout<< "matchingLevel(Matching): "<<( int)m.headList[i].coordinateInfoList[j].matchingLevel<<endl;
   cout<< "----------------座標情報"<<j+ 1<< "(End)----------------"<<endl;
  }
  cout<< "--------------------------Header部"<<i+ 1<< "(End)---------------------------"<<endl;
 }
}
int main() {
 TravelInfo travelInfo;
 travelInfo.parserData();
  unsigned  int a= 1;
  int b=- 2;
  int c=- 2;
 cout<<b<<endl;
  if(a+c> 0)
  cout<<a+b<<endl;
/* cout<<"-----------------------管理部(Start)------------------------"<<endl;
 int measureinfoNum = *(u_short*)dataBuffer;
 cout<<"measureinfoNum(測定情報数) : "<<measureinfoNum<<endl;
 dataBuffer =dataBuffer+sizeof(u_short);
 cout<<"interval(座標間隔): "<<(int)(*(u_char*)(dataBuffer))<<endl;
 dataBuffer =dataBuffer+sizeof(u_char);
 cout<<"mapVersion(地図Ver): "<<(int)(*(u_char*)(dataBuffer))<<endl;
 dataBuffer =dataBuffer+sizeof(u_char);
 cout<<"additionalInfoFlag(付加情報flag): "<<(int)(*(u_char*)(dataBuffer))<<endl;
 dataBuffer =dataBuffer+sizeof(u_char);
 cout<<"vehicleType(車載機種別): "<<(int)(*(u_char*)(dataBuffer))<<endl;
 dataBuffer = dataBuffer+sizeof(u_char);
 cout<<"nothing1(空領域): "<<*(u_short*)(dataBuffer)<<endl;
 dataBuffer = dataBuffer+sizeof(u_short);
 cout<<"nothing2(空領域): "<<*(u_short*)(dataBuffer)<<endl;
 dataBuffer = dataBuffer+sizeof(u_short);
 cout<<"nothing3(空領域): "<<*(u_short*)(dataBuffer)<<endl;
 dataBuffer = dataBuffer+sizeof(u_short);
 cout<<"-----------------------管理部(End)------------------------"<<endl;
 for(int i = 0; i <measureinfoNum; i++)
 {
  cout<<"--------------------------Header部"<<i+1<<"(Start)---------------------------"<<endl;
  cout<<"year(年): "<<(int)(*(u_char*)(dataBuffer))<<endl;
  dataBuffer = dataBuffer+sizeof(u_char);
  cout<<"month(月): "<<(int)(*(u_char*)(dataBuffer))<<endl;
  dataBuffer = dataBuffer+sizeof(u_char);
  cout<<"day(日): "<<(int)(*(u_char*)(dataBuffer))<<endl;
  dataBuffer = dataBuffer+sizeof(u_char);
  cout<<"hour(时): "<<(int)(*(u_char*)(dataBuffer))<<endl;
  dataBuffer = dataBuffer+sizeof(u_char);
  cout<<"minute(分): "<<(int)(*(u_char*)(dataBuffer))<<endl;
  dataBuffer = dataBuffer+sizeof(u_char);
  cout<<"second(秒): "<<(int)(*(u_char*)(dataBuffer))<<endl;
  dataBuffer = dataBuffer+sizeof(u_char);
  cout<<"latDegree(緯度(度)): "<<(int)(*(u_char*)(dataBuffer))<<endl;
  dataBuffer = dataBuffer+sizeof(u_char);
  cout<<"latPoint(緯度(分)): "<<(int)(*(u_char*)(dataBuffer))<<endl;
  dataBuffer = dataBuffer+sizeof(u_char);
  cout<<"lonPoint(経度(度)): "<<(int)(*(u_char*)(dataBuffer))<<endl;
  dataBuffer = dataBuffer+sizeof(u_char);
  cout<<"lonPoint(経度(分)): "<<(int)(*(u_char*)(dataBuffer))<<endl;
  dataBuffer = dataBuffer+sizeof(u_char);
  int coordinateNum = *(u_short*)(dataBuffer);
  cout<<"coordinateNum(座標件数): "<<coordinateNum<<endl;
  dataBuffer = dataBuffer+sizeof(u_short);
  for(int j = 0; j < 2; j++)
  {
   cout<<"----------------座標情報"<<j+1<<"(Start)----------------"<<endl;
   cout<<"latChange(経変): "<<(int)(*(char*)(dataBuffer))<<endl;
   dataBuffer = dataBuffer+sizeof(char);
   cout<<"latitude(経度): "<<*(short*)(dataBuffer)<<endl;
   dataBuffer = dataBuffer+sizeof(short);
   cout<<"lonChange(緯変): "<<(int)(*(char*)(dataBuffer))<<endl;
   dataBuffer = dataBuffer+sizeof(char);
   cout<<"longitude(緯度): "<<*(short*)(dataBuffer)<<endl;
   dataBuffer = dataBuffer+sizeof(short);
   cout<<"direction(方位): "<<*(short*)(dataBuffer)<<endl;
   dataBuffer = dataBuffer+sizeof(short);
   cout<<"speed(速度): "<<*(u_short*)(dataBuffer)<<endl;
   dataBuffer = dataBuffer+sizeof(u_short);
   cout<<"additionalInfo(付加情報): "<<*(char*)(dataBuffer)<<endl;
   dataBuffer = dataBuffer+sizeof(char);
   cout<<"counter: "<<(int)(*(u_char*)(dataBuffer))<<endl;
   dataBuffer = dataBuffer+sizeof(u_char);
   cout<<"routeType(道路種別): "<<(int)(*(u_char*)(dataBuffer))<<endl;
   dataBuffer = dataBuffer+sizeof(u_char);
   cout<<"matchingLevel(Matching): "<<(int)(*(u_char*)(dataBuffer))<<endl;
   dataBuffer = dataBuffer+sizeof(u_char);
   cout<<"----------------座標情報"<<j+1<<"(End)----------------"<<endl;
  }
  cout<<"--------------------------Header部"<<i+1<<"(End)---------------------------"<<endl;
 }*/

  return  0;
}
  

你可能感兴趣的:(内存读取以及内存对齐)