0

This is my first question here. I hope I'm doing everything right.

So, the title speaks for itself. I am working on Windows10, Visual Studio. This basic code will later be addopted in a windowed application (c++ / cli).

I am not a programmer. I learned to program in C, 15 years ago under Linux and haven't used it since. I started picking up c++ coding recently.

Here is an example code I am struggling with:

#include <string>
#include <iostream>
#include <fstream>

using namespace std;

struct mystructure {
    string id;
    int ra_h;
    int ra_m;
    float ra_s;
    int dec_deg;
    int dec_min;
    string constellation;
    string object;
    string mag;
    string arc_size;
    string name;
};

int load_csv( struct mystructure* _table) {
    //I cut out the csv loader part to make the code shorter.

    int i = 0;
    while (i++ < 10) {

        _table = (mystructure*)realloc(_table, (i + 1) * sizeof(mystructure)); //(i+1) --> +1 might be unnecessary, but it's not working 

        _table[i].id = "id";
        _table[i].ra_h = i + 1;
        _table[i].ra_m = i + 2;
        _table[i].ra_s = i + 3.3;
        _table[i].dec_deg = i + 4;
        _table[i].dec_min = i + 5;
        _table[i].constellation = "name of constellation";
        _table[i].object = "name of object";
        _table[i].mag = "mag value";
        _table[i].arc_size = "text..";
        _table[i].name = "text..";
    }

    return i; //return the number of lines on the csv file
}

int main() {

    //struct mystructure table[15];  //not dynamic but working without realloc
    struct mystructure table[1]; //use realloc in function loop
    int num_of_lines;
    num_of_lines = load_csv(table); //this function fills table struct
     //printout for debug
    for (int i = 1; i < num_of_lines; i++) {
        std::cout << "Ciklus count: " << i << '\n';
        std::cout << "Id: " << table[i].id << '\n';
        std::cout << "Ra: " << table[i].ra_h << "h  " << table[i].ra_m << "m  " << table[i].ra_s << "s" << '\n';
        std::cout << std::showpos << "Dec: " << table[i].dec_deg << "\370 ";
        std::cout << std::noshowpos << table[i].dec_min << "' " << '\n';
        std::cout << "Const: " << table[i].constellation << '\n';
        std::cout << "Object: " << table[i].object << '\n';
        std::cout << "Mag: " << table[i].mag << '\n';
        std::cout << "Arc Size: " << table[i].arc_size << '\n';
        std::cout << "Name: " << table[i].name << '\n';
        std::cout << "-------------------" << '\n';
    }
    return 0;
}

I am filling up the table struct array with load_csv function. If I specify the size of the struct array the code work but I can not make it dynamic. I tried a number of ways nothing worked. This is just one example I put here.

I'd appreciate any comment and help to solve this issue.

Thank you Robert

anastaciu
  • 23,467
  • 7
  • 28
  • 53
Robert
  • 11
  • 3
  • 4
    Firstly, don't ask about C when you're using C++. They are two different languages. You can [edit] your question to fix this. As a new user, also make sure you take the [tour] and read [ask]. – Ulrich Eckhardt Jan 28 '20 at 10:23
  • 3
    `realloc()` has undefined behaviour unless it is passed a pointer that was assigned the result of `malloc()`, `calloc()`, `realloc()`, or a `NULL`. Your code is passing the (address of the first and only element) an array with one element that is of automatic storage duration. Hence the undefined behaviour. – Peter Jan 28 '20 at 10:26
  • 1
    Why not use std::vector? – Tom Jan 28 '20 at 10:28
  • 3
    In C++ it's very easy: `std::vector mytable;`. Done. For more details refer to your favorite [C++ Book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – rustyx Jan 28 '20 at 10:28
  • I haven't used `std::vector` because well I don't know it. I'll look into it and get back to you all. Thank you for the quick answers! – Robert Jan 28 '20 at 10:37
  • At the end of the day, this was the **solution**: `std::vector mytable;` I couldn't do it with: "How to create a dynamic array of integers". **Thank you all!** – Robert Jan 28 '20 at 14:17

0 Answers0