-4

Hello guys how can i do this on C++ (98). It's working good on visual studio or something, but on place where i want to use it doesn't work.

https://image.prntscr.com/image/loIxb-YRRiGh9XAFmZ_0dA.png

I'm trying to send multiple functions with other values in array like:

Print(xx, parse_array()); // No elements in parsearray all will be 0
Print(xx, parse_array(25)); // First element [0] will be 25, rest of them 0.



int * parse_array(int val1 = 0, int val2 = 0, int val3 = 0, int val4 = 0)
{
    int m_pDataArray[4] = {
        val1, val2, val3, val4
    };

    return m_pDataArray;
}

void Print(bool notImportant, int m_pDataArray[4])
{
    printf("m_pDataArray 0: %d", m_pDataArray[0]);
    printf("m_pDataArray 1: %d", m_pDataArray[1]);
    printf("m_pDataArray 2: %d", m_pDataArray[2]);
    printf("m_pDataArray 3: %d", m_pDataArray[3]);
}

Print(true, parse_array(10, 20, 30);
Print(true, parse_array(10);
Print(true, parse_array());
πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
Dr.Vendetta
  • 15
  • 1
  • 7
  • Describe your problem. Don't just say "it doesn't work" - define "*it*". What did you want it to do that it does not? And don't link to external images; put all relevant information *in the question* as *text*. – Jesper Juhl Jun 08 '17 at 21:19
  • Sorry, i edited first pot some sec ago and i attached a photo. https://image.prntscr.com/image/loIxb-YRRiGh9XAFmZ_0dA.png – Dr.Vendetta Jun 08 '17 at 21:22

1 Answers1

0

You are returning the address of a local variable in parse_array. This is undefined behavior, the object int m_pDataArray[4] no longer exists after you've returned from your function, the pointer you return is immediately invalidated. Undefined behavior can do anything, including work sometimes on some computer but not on another computers, like you've described.

int [] does not have value semantics, you cannot return it easily. Use std::array instead.

#include <array>

std::array<int, 4> parse_array(int val1 = 0, int val2 = 0, int val3 = 0, int val4 = 0)
{
    std::array<int, 4> m_pDataArray = {
        val1, val2, val3, val4
    };

    return m_pDataArray;
}

Otherwise, you will need to return dynamically allocated buffers and then remember to delete them. The easiest way would be to use std::unique_ptr but those don't exist in C++98.

Edit : It seems I forgot that std::array was added in C++ 11. You can get the same results with std::vector instead.

#include <vector>

std::vector<int> parse_array(int val1 = 0, int val2 = 0, int val3 = 0, int val4 = 0)
{
    std::vector<int> m_pDataArray;
    m_pDataArray.push_back(val1);
    m_pDataArray.push_back(val2);
    m_pDataArray.push_back(val3);
    m_pDataArray.push_back(val4);

    return m_pDataArray;
}
François Andrieux
  • 28,148
  • 6
  • 56
  • 87
  • Thanks for answer Francois, but i'm on c++98...doesn't exist a really method for what i want..? or maybe like i can do it with a vector or i dont know.. std::vector parse_array(int a = 0, int b = 0, int c = 0, int d = 0) { std::vector m_vec_data; m_vec_data.push_back(a); m_vec_data.push_back(b); m_vec_data.push_back(c); m_vec_data.push_back(d); return m_vec_data; } void Print(std::vector m_vec_data) { for (int i = 0; i < m_vec_data.size(); i++) printf("m_pDataArray i: %d \n", m_vec_data[i]); } – Dr.Vendetta Jun 08 '17 at 21:35
  • @Dr.Vendetta Updated. If you can, look into upgrading your compiler. The improves from C++98 to C++11 is truly enormous, the way of doing things changed completely. – François Andrieux Jun 08 '17 at 21:45
  • Thanks bro, works perfectly, god bless you! – Dr.Vendetta Jun 08 '17 at 21:46