打印结构数组内容时出错
Error while printing structure array contents
下面的代码不会在数组的第一个元素之后打印。打印第一个结构内容后,后续值包含垃圾值
#include <iostream>
using namespace std;
#define SMALL_STRING_LEN 20
#define TINY_STRING_LEN 10
enum data_item_type_t {
TYPE_FLOAT,
TYPE_INT,
TYPE_UINT
};
enum agent_type_t {
LOCATION,
};
enum sensor_type_t {
NOT_APPLICABLE,
};
typedef struct data_holder_conf {
int16_t data_id;
char description[SMALL_STRING_LEN];
int16_t num_items;
char unit[TINY_STRING_LEN];
data_item_type_t data_type;
agent_type_t agent;
sensor_type_t sensor;
/* pull frequency in milliseconds*/
uint32_t pull_freq;
} data_holder_conf_t;
data_holder_conf_t data_holder_conf_arr[] = {
{ 101, "altitude", 1, "metres", TYPE_FLOAT, LOCATION, NOT_APPLICABLE, 100 },
{ 102, "latitude", 1, "metres", TYPE_FLOAT, LOCATION, NOT_APPLICABLE, 100 },
{ 103, "longitude", 1, "metres", TYPE_FLOAT, LOCATION, NOT_APPLICABLE, 100 },
{ 104, "velocity", 1, "kmph", TYPE_FLOAT, LOCATION, NOT_APPLICABLE, 100 }
};
int main() {
data_holder_conf_t *ptrLocal = (data_holder_conf_t *)malloc(4 * sizeof(data_holder_conf_t));
memcpy(ptrLocal, data_holder_conf_arr, 4 * sizeof(data_holder_conf_t));
cout << "..........................................\n";
data_holder_conf_t *ptrTemp;
for (int i = 0; i < 4; i++) {
ptrTemp = (i * sizeof(data_holder_conf_t)) + ptrLocal;
cout << " data_id = " << ptrTemp->data_id << endl;
cout << " description = " << ptrTemp->description << endl;
cout << " num_items = " << ptrTemp->num_items << endl;
cout << " unit = " << ptrTemp->unit << endl;
cout << " data_type =" << ptrTemp->data_type << endl;
cout << " agent = " << ptrTemp->agent << endl;
cout << " sensor = " << ptrTemp->sensor << endl;
cout << " pull_freq = " << ptrTemp->pull_freq << endl;
}
free(ptrLocal);
}
我认为计算 ptrTemp
值时出现问题。
但我无法弄清楚是什么错误。
I think there is problem while calculating the ptrTemp
value.
你猜对了:你的指针算法不正确:要获得指向第 i
条目的指针,只需使用:
ptrTemp = ptrLocal + i;
或
ptrTemp = &ptrLocal[i];
问题出在这里:
ptrTemp = (i * sizeof(data_holder_conf_t)) + ptrLocal;
在 C 和 C++ 中,指针运算以 pointed-to 类型为单位进行运算,但该代码似乎假定它以字节为单位进行运算。当 i
为 0 时无关紧要,但对于更大的 i
值,您(方式)超出了数组的边界。正确的是
ptrTemp = i + ptrLocal;
或者,完全避免使用 ptrTemp
并仅在 ptrLocal
上使用索引运算符 []
来访问元素将是惯用的和更清晰的:
cout << " data_id = " << ptrLocal[i].data_id << endl;
// ...
下面的代码不会在数组的第一个元素之后打印。打印第一个结构内容后,后续值包含垃圾值
#include <iostream>
using namespace std;
#define SMALL_STRING_LEN 20
#define TINY_STRING_LEN 10
enum data_item_type_t {
TYPE_FLOAT,
TYPE_INT,
TYPE_UINT
};
enum agent_type_t {
LOCATION,
};
enum sensor_type_t {
NOT_APPLICABLE,
};
typedef struct data_holder_conf {
int16_t data_id;
char description[SMALL_STRING_LEN];
int16_t num_items;
char unit[TINY_STRING_LEN];
data_item_type_t data_type;
agent_type_t agent;
sensor_type_t sensor;
/* pull frequency in milliseconds*/
uint32_t pull_freq;
} data_holder_conf_t;
data_holder_conf_t data_holder_conf_arr[] = {
{ 101, "altitude", 1, "metres", TYPE_FLOAT, LOCATION, NOT_APPLICABLE, 100 },
{ 102, "latitude", 1, "metres", TYPE_FLOAT, LOCATION, NOT_APPLICABLE, 100 },
{ 103, "longitude", 1, "metres", TYPE_FLOAT, LOCATION, NOT_APPLICABLE, 100 },
{ 104, "velocity", 1, "kmph", TYPE_FLOAT, LOCATION, NOT_APPLICABLE, 100 }
};
int main() {
data_holder_conf_t *ptrLocal = (data_holder_conf_t *)malloc(4 * sizeof(data_holder_conf_t));
memcpy(ptrLocal, data_holder_conf_arr, 4 * sizeof(data_holder_conf_t));
cout << "..........................................\n";
data_holder_conf_t *ptrTemp;
for (int i = 0; i < 4; i++) {
ptrTemp = (i * sizeof(data_holder_conf_t)) + ptrLocal;
cout << " data_id = " << ptrTemp->data_id << endl;
cout << " description = " << ptrTemp->description << endl;
cout << " num_items = " << ptrTemp->num_items << endl;
cout << " unit = " << ptrTemp->unit << endl;
cout << " data_type =" << ptrTemp->data_type << endl;
cout << " agent = " << ptrTemp->agent << endl;
cout << " sensor = " << ptrTemp->sensor << endl;
cout << " pull_freq = " << ptrTemp->pull_freq << endl;
}
free(ptrLocal);
}
我认为计算 ptrTemp
值时出现问题。
但我无法弄清楚是什么错误。
I think there is problem while calculating the
ptrTemp
value.
你猜对了:你的指针算法不正确:要获得指向第 i
条目的指针,只需使用:
ptrTemp = ptrLocal + i;
或
ptrTemp = &ptrLocal[i];
问题出在这里:
ptrTemp = (i * sizeof(data_holder_conf_t)) + ptrLocal;
在 C 和 C++ 中,指针运算以 pointed-to 类型为单位进行运算,但该代码似乎假定它以字节为单位进行运算。当 i
为 0 时无关紧要,但对于更大的 i
值,您(方式)超出了数组的边界。正确的是
ptrTemp = i + ptrLocal;
或者,完全避免使用 ptrTemp
并仅在 ptrLocal
上使用索引运算符 []
来访问元素将是惯用的和更清晰的:
cout << " data_id = " << ptrLocal[i].data_id << endl;
// ...