Home > Software design >  Error while printing structure array contents
Error while printing structure array contents

Time:05-27

Below code is not printing after 1st element of array. After printing first structure content the subsequent values are containing garbage values

#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);
}

I think there is problem while calculating the ptrTemp value.

But I am not able to figure out what is the mistake.

CodePudding user response:

The problem is here:

ptrTemp = (i * sizeof(data_holder_conf_t))   ptrLocal;

in both C and C , pointer arithmetic operates in units of the pointed-to type, but that code seems to be assuming that it operates in units of bytes. When i is 0 that doesn't matter, but for larger values of i you are (way) overrunning the bounds of your array. Correct would be

ptrTemp = i   ptrLocal;

Alternatively, it would be idiomatic and clearer to avoid ptrTemp altogether and just use the indexing operator [] on ptrLocal to access the elements:

        cout << " data_id = " << ptrLocal[i].data_id << endl;
        // ...

CodePudding user response:

I think there is problem while calculating the ptrTemp value.

You presume right: your pointer arithmetic is incorrect: to get a pointer to the i-th entry, just use:

    ptrTemp = ptrLocal   i;

or

    ptrTemp = &ptrLocal[i];
  • Related