0

I am working on a c++ problem where I need to convert a vector pair of type struct to an array. My code is as follows:

struct YObject
{
    YObject(unsigned int val1,
        float val2,
        float val3)
     : m_V1(val1)
     , m_V2(val2)
     , m_V3(val3)
    {}

    unsigned int m_V1;
    float m_V2;
    float m_V3;
};

int result(float *output)
{
  int output_size = 3*3;
 
  std::vector<YObject> vect;
       
    // using emplace() to insert pair in-place 
    vect.emplace_back(32, 24.5, 56.3); 
        
    vect.emplace_back(45, 30.3, 67.8);
    
    vect.emplace_back(99, 78.6, 59.3);
    
    float out[3];
    for (int i=0; i<vect.size(); i++)
    {
        out[0]=vect[i].m_V1;
        out[1]=vect[i].m_V2;
        out[2]=vect[i].m_V3;
      
        for (int n=0; n<3; n++)
        {
            output[n] = out[n];
            //cout << output[n] << endl;
        }
    
    }
return 0;
}

int final(float *outr)
{
    result(outr);
    return 0;
}

int main()
{
    int size = 3*3;
    float arr[size];
    final(arr);
    cout << "arr[0]: " << arr[0] << " arr[1]: " << arr[1] << " arr[2]: " << arr[2] << " arr[3]: " << arr[3] << " arr[4]: " << arr[4] << " arr[5]: " << arr[5] << " arr[6]: " << arr[6] << " arr[7]: " << arr[7] << " arr[8]: " << arr[8];
    return 0;
} 

I want that the output array passed in result function to return the full vector which is formed after three emplace_back statements. i.e. the cout statement in main function should print:

arr[0]: 32 arr[1]: 24.5 arr[2]: 56.3 arr[3]: 45 arr[4]: 30.3 arr[5]: 67.8 arr[6]: 99 arr[7]: 78.6 arr[8]: 59.3

but right now its printing:

arr[0]: 99 arr[1]: 78.6 arr[2]: 59.3arr[3]: 0 arr[4]: -1.94201e+27 arr[5]: 4.59121e-41 arr[6]: -9.96761e+17 arr[7]: -5.79801e+60 arr[8]: 1.98345e-22
Neet
  • 13
  • 4
  • Declaring `float out[3]` and using that, then immediately transposing those values to `output` does seem like a whole lot of wasted effort. Why not just directly assign to `output[n] = vect[i].m_V1` and so on? – tadman Jul 06 '20 at 22:24
  • It's strange that this code uses both `std::vector`, which is good, and C-style pointers to arrays, which is clunky and weird. This would be a lot cleaner with `std::vector` references being passed around. – tadman Jul 06 '20 at 22:25
  • yes you may do that. But how to return the entire array? – Neet Jul 06 '20 at 22:26
  • That's not the problem. The issue seems to be that you never initialize your C-style array, and that you never properly populate it. You probably mean to set `output[n + i * 3]` or something like that instead of writing the same three values repeatedly. – tadman Jul 06 '20 at 22:28
  • I have tried quite a few things but nothing seems to output the desired result. – Neet Jul 06 '20 at 22:33
  • 3
    [ISO C++ forbids variable length arrays](https://stackoverflow.com/q/11379433/6865932) – anastaciu Jul 06 '20 at 22:34
  • 1
    `float arr[size];` -- This is not valid C++. – PaulMcKenzie Jul 06 '20 at 22:45
  • 1
    You can declare `int index = 0;` outside loops and and in line `output[index++] = out[n];` – pvc Jul 07 '20 at 05:23
  • @pvc Thankyou for your reply. This is what I was missing. – Neet Jul 07 '20 at 12:11

1 Answers1

1

If you lightly clean up your code and use std::vector there's really not much that can go wrong and it works as you expect:

#include <vector>
#include <iostream>

struct YObject
{
  YObject(const unsigned int val1, const float val2, const float val3)
    : m_V1(val1)
    , m_V2(val2)
    , m_V3(val3)
  {}

  unsigned int m_V1;
  float m_V2;
  float m_V3;
};

std::vector<float> result(const std::vector<YObject>& input)
{
  std::vector<float> output;

  // Easily iterate over each "input" given using for
  for (auto& vect : input) {
    output.push_back(vect.m_V1);
    output.push_back(vect.m_V2);
    output.push_back(vect.m_V3);
  }

  return output;
}

int main()
{
  std::vector<YObject> vect;
  
  // Define example inputs here, not deep inside a function
  vect.emplace_back(32, 24.5, 56.3); 
  vect.emplace_back(45, 30.3, 67.8);
  vect.emplace_back(99, 78.6, 59.3);

  auto arr = result(vect);

  std::cout << "arr[0]: " << arr[0] << " arr[1]: " << arr[1] << " arr[2]: " << arr[2] << " arr[3]: " << arr[3] << " arr[4]: " << arr[4] << " arr[5]: " << arr[5] << " arr[6]: " << arr[6] << " arr[7]: " << arr[7] << " arr[8]: " << arr[8] << std::endl;

  return 0;
}

In your code you referenced arr[9] which doesn't exist in a length 9 array, the indexes are only 0 .. 8.

Try and write functions that take inputs and return useful outputs. Having a useless int return value instead of returning a new vector is one such case. Also get into the habit of declaring your arguments as const and references for non-trivial types to avoid inadvertent copies or mutations.

tadman
  • 208,517
  • 23
  • 234
  • 262
  • Sorry, writing arr[9] was a mistake.This whole code snippet is a portion of a bigger problem where the input to vector (vector.emplace_back(a,b,c)) is defined deep in the function and the output of result function is being accessed by some other function. – Neet Jul 06 '20 at 23:18
  • The actual function is int result ( float *output), which I think should return an array not a std::vector. – Neet Jul 06 '20 at 23:28
  • What "actual function" are you talking about? Are you constrained by some requirements? If so, please be more specific about what you must do here. – tadman Jul 06 '20 at 23:30
  • To keep it simple, is there a way that the result function int result (float *output) returns an array by converting the std::vector vect; – Neet Jul 06 '20 at 23:38
  • You can't "return an array" from a function that returns `int`. – tadman Jul 07 '20 at 00:25
  • I think you are diverting from the actual problem. As you may have seen in the output of the initial code that I have posted, the function result is returning the array but not the desired one. – Neet Jul 07 '20 at 01:06
  • A) You're not returning it, you're manipulating it. This is an important distinction. B) You're not manipulating it correctly as I've pointed out in earlier comments. – tadman Jul 07 '20 at 01:10