0

The elements of the Array are not being initialized, because when I run the code I only see the values from the memory and not the values that I set in the vector. Can you explain to me why is this happening and what should I do in future to avoid this mistake? Please ignore the comments from my code

#include <iostream>
using namespace std;

//ne va crapa deoarece nu am initializat valorile vectorului, de asta cream o functie de initializare vector

void initialisingVector(int* vector, int n, int InitialValue)
{
    for (int i = 0; i < n; i++)
    {
        vector[i] = InitialValue;
    }

}
//mereu vom pune initializarea inainte de afisare

void displayingtheVector(int vector[], int n)
{
    cout << endl << "The vector is:";
    for (int i = 0; i < n; i++)
    {
        cout << vector[i] << ",";
    }
}

int* creatingAndInitialisingtheVector(int* n)//nu avem nevoie de nr de elemente deoarece le ia din main;si nici de vector;
{//nu avem nevoie de nimic la parametrii, totusi punem n deoarece ne da eroare-n main, ai comentat acolo

//vom folosi in* n ca parametru deoarece n e o valoare ce se va modifica , prin urmare e transmisa prin pointer

//vom avea eroare cu expected ; ,nu are legatura cu ; problema este de la int[] ,expected an identifier
//nu o sa putem nici o data pune int[] la tipul unei functii, mereu vom pune int*, int[]---sa incercam sa uitam forma asta;
//INTOTDEAUNA int*

    int vectorNou[30];
    int numberElements;
    int InitialValue;

    std::cout << std::endl << "Number of elements:";
    std::cin >> numberElements;
    *n = numberElements;

    std::cout << std::endl << "Initial value is:";
    std::cin >> InitialValue;


    initialisingVector(vectorNou, numberElements, 0);
    //afisareVector(vectorNou, numarElemente); nu are sens sa o facem in fct de creare

    return vectorNou;
}


int main()
{
    int listaPreturi[30];
    int numarProduse;

    /*std::cout << std::endl<<"Numarul de produse este:";
    std::cin >> numarProduse;

    initializareVector(listaPreturi, numarProduse, 0);
    afisareVector(listaPreturi,numarProduse);
    */

    //listaPreturi=crearesiInitializareVector();//ne va trebui o variabila care sa prinda vectorul creat de functie,de asta punem listaPreturi
    //vom obtine o alta eroare dupa ce egalam variabila cu functia; expression must be a modifiable lvalue==>listaPreturi e o variabila constanta, si trebuie sa fie modifiable
    //dc avem o fct care inhtoarce un vector nu vom putea prinde vectorul nou intr-un alt vector deja creat static.
    //il va prinde u8n vetor dinamic in schimb
    int nrElements = 0;
    int* vectorNew = creatingAndInitialisingtheVector(&nrElements);//functioneaza

    //afisareVector(vectorNou, sizeof(vectorNou)); e periculos folosind sizeof(vectorNou) deoarece vectorNou are 4 , utilizatoru poate vrea mai multe elemente

    displayingtheVector(vectorNew, nrElements);
}

I expect that the vector with the values set by the user to be displayed, and not the memory addresses.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Corina
  • 137
  • 1
  • 1
  • 12
  • With the code you show, what is the output you get, and what output did you expect? Please copy-paste the actual output into the question, and paste the actual output and modify it with the expected values. – Some programmer dude Oct 21 '19 at 17:10
  • @Someprogrammerdude do you want me to write by hand the memory adresses from the compiler? – Corina Oct 21 '19 at 17:14
  • [Enable warnings on your compiler](https://stackoverflow.com/questions/57842756/why-should-i-always-enable-compiler-warnings). It will tell you what you are doing wrong. – walnut Oct 21 '19 at 17:15
  • @uneven_mark I don't have compiler errors, nor is the code crashing – Corina Oct 21 '19 at 17:16
  • 1
    @Corina That may be the case, but your program still has [undefined behavior](https://stackoverflow.com/questions/2397984/undefined-unspecified-and-implementation-defined-behavior) and if you enable warnings your compiler will give you a warning about it (although warning messages may be hard to understand, in that case google for them and if necessary ask about them here). Also read the links I posted. – walnut Oct 21 '19 at 17:17
  • @uneven_mark I will – Corina Oct 21 '19 at 17:19

2 Answers2

3

The program has undefined behavior because the function creatingAndInitialisingtheVector returns pointer to the local array

int vectorNou[30];

that will not alive after exiting the function.

It seems you should declare the function with one more parameter the following way

size_t creatingAndInitialisingtheVector( int *a, size_t n );

and call the function in main like

size_t nrElements = 
    creatingAndInitialisingtheVecto( listaPreturi, sizeof( listaPreturi ) / sizeof( *listaPreturi ) );

The function definition can look like

size_t creatingAndInitialisingtheVector( int *a, size_t n)//nu avem nevoie de nr de elemente deoarece le ia din main;si nici de vector;
    {//nu avem nevoie de nimic la parametrii, totusi punem n deoarece ne da eroare-n main, ai comentat acolo

    //vom folosi in* n ca parametru deoarece n e o valoare ce se va modifica , prin urmare e transmisa prin pointer

    //vom avea eroare cu expected ; ,nu are legatura cu ; problema este de la int[] ,expected an identifier
    //nu o sa putem nici o data pune int[] la tipul unei functii, mereu vom pune int*, int[]---sa incercam sa uitam forma asta;
    //INTOTDEAUNA int*

        size_t numberElements;
        int InitialValue;

        std::cout << std::endl << "Number of elements:";
        std::cin >> numberElements;

        if ( n < numberElements ) numberElements = n;

        std::cout << std::endl << "Initial value is:";
        std::cin >> InitialValue;

        initialisingVector( a, numberElements, InitialValue);
        //afisareVector(vectorNou, numarElemente); nu are sens sa o facem in fct de creare

        return numberElements;
    }
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
0

Inside of your function creatingAndInitialisingtheVector you are returning a pointer to memory that will go out of scope once the function returns. The memory is being allocated for the scope of the function and is freed after returning. I would recommend reading up on scope and how memory is allocated for things such as arrays in C++.

Jesse Yentsch
  • 174
  • 14