1

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.

chqrlie
  • 131,814
  • 10
  • 121
  • 189
kernel
  • 326
  • 1
  • 3
  • 18
  • Did you try using a debugger to see just where/how things go sideways? – Scott Hunter May 27 '22 at 12:20
  • 2
    The code you present is not C, but instead C-style C++ while additionally using few C++ only features. Now decide, which one do you actually want to have? – Aconcagua May 27 '22 at 12:26
  • "I am unable to figure out why." -- Have you tried running your code line by line in a debugger while monitoring the values of all variables, in order to determine in which line your program stops behaving as intended? If you did not try this, then you may want to read this: [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/12149471) You may also want to read this: [How to debug small programs?](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) – Andreas Wenzel May 27 '22 at 12:32

2 Answers2

1

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;
        // ...
John Bollinger
  • 160,171
  • 8
  • 81
  • 157
0

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];
chqrlie
  • 131,814
  • 10
  • 121
  • 189
  • If C++, interesting alternative would yet be using a reference: `auto& tmp = ptrLocal[i];` – Aconcagua May 27 '22 at 12:59
  • @Aconcagua: of course using `malloc()` and `memcpy` in a C++ program is not idiomatic either. – chqrlie May 27 '22 at 13:01
  • @Aconcagua: with this definition, I suppose `tmp` would be used as a reference, eg: `ptrTemp.data_id` – chqrlie May 27 '22 at 13:03
  • Of course it would – and agree on those C functions as well. Actually question author should finally clarify which language it actually intends to write code for... – Aconcagua May 27 '22 at 13:08
  • Hi all , this is a c code snippet which tried on a C++ program. – kernel May 27 '22 at 13:09