Hello,
I wrote a little program (Create a vector and give it back via pointer.) for a better understanding of pointers.
First it works but when I would like get access to the vector alloacated with "new",
problems occur. For example saving a element is not possible.

#include <iostream>
#include <vector>

using namespace std;

//create vector and pointer
vector<int>*pVector();
vector<int>*pVector()
{
    vector<int>*myvector=new vector<int>;
    return myvector;
}

//printing vector on the monitor via iterator
void VektorPrint();
void VektorPrint()
{
 vector<int>*PrintV=pVector();
 vector<int>::iterator it;
 for(it=(*PrintV).begin();it!=(*PrintV).end();++it)
 {
    cout<<*it<<endl;
 }
delete PrintV;   //Frees the memory allocated with new)
PrintV=0;        //pointer to zero so program will crash, if some saves something.
}

int main()
{
    *pVector();
    //shall save something to the vector allocated on the heap but it doesn't work.
    int c=700;
    (*pVector()).push_back(c);
    VektorPrint();
    return 0;
}
//create vector and pointer
vector<int>*pVector();
vector<int>*pVector()
{
    vector<int>*myvector=new vector<int>;
    return myvector;
}

//printing vector on the monitor via iterator
void VektorPrint();
void VektorPrint()
{
 vector<int>*PrintV=pVector();
 vector<int>::iterator it;
 for(it=(*PrintV).begin();it!=(*PrintV).end();++it)
 {
    cout<<*it<<endl;
 }
delete PrintV;   //Frees the memory allocated with new)
PrintV=0;        //pointer to zero so program will crash, if some saves something.
}

These look like function prototypes and implementations. Is that your intent?

If so, it looks okay to me. I think your issue is coming from your main(). Take another look at the first line: *pVector(); What does this line do? It really doesn't do anything useful. The problem is that you are attempting to, well I'm not sure what you're trying to do. As far as I know, this isn't even valid syntax.

Think a little more closely about how variables and pointers work. You need to provide a dataType, an Identifier, and (optionally) an initialization value:

//declare an integer variable
int anInt = 0;

//declare an integer pointer
int *pAnInt = NULL;

Your declaration in main() is missing the dataType, and additionally is formatted more like a function call. You could do something like this:

int *generateAnInt() {
  return new int(0);
}

int main() {
  int *pAnInt = generateAnInt();

  //...

  return 0;
}

But not what you've done. You didn't complete the statement.

Yes, the part before the main are the prototypes and the implementation.

Mein intention is to create a vector on the heap and then get access to it (giving out, saving, erasing, etc.).
This was no problem for me, so I thought if it would be also possible to do all (giving out, saving, erasing, etc) this through functions.

So I wrote VektorPrint(); to print the vector - created on the heap- on the monitor.
This works, so additionally I would like to write a function for saving a new element into the vector created on the heap.

And there is the problem, all the code I wrote didn't work.
E.G.:

vector<int>*SavingVector=pVector(); //prototype
vector<int>*SavingVector=pVector()  //implementation
{
  int c=700;
  (*SavingVector).push_back(c);
  return SavingVector;
}

As I figured out will this function doesn't work, it seems to create a new vector and saves there the variable c.
But how can I save a variable through a function in the origin(first) vector given back by the function "*pVector()" ?

Okay.

First, you don't need a prototype and an implementation above your main(), use one or the other. If you choose to use a prototype, you can then implement the function at a later time. Here is your code re-arranged in a more appropriate fashion:

#include <iostream>
#include <vector>

using namespace std;

//function prototypes:
vector<int>*pVector();  //create vector and pointer
void VektorPrint();     //printing vector on the monitor via iterator

//IMPLEMENT THE MAIN FUNCTION
int main()
{
    *pVector();
    //shall save something to the vector allocated on the heap but it doesn't work.
    int c=700;
    (*pVector()).push_back(c);
    VektorPrint();
    return 0;
}

//function implementations:
vector<int>*pVector()   //create vector and pointer
{
    vector<int>*myvector=new vector<int>;
    return myvector;
} //end function

void VektorPrint()      //printing vector on the monitor via iterator
{
 vector<int>*PrintV=pVector();
 vector<int>::iterator it;
 for(it=(*PrintV).begin();it!=(*PrintV).end();++it)
 {
    cout<<*it<<endl;
 }
 delete PrintV;   //Frees the memory allocated with new)
 PrintV=0;        //pointer to zero so program will crash, if some saves something.
} //end function

I've added a couple comments for clarification, but do you see how much easier it is to understand the behavior of the code (except for the very confusing syntax you attempted within your main())?

Mein intention is to create a vector on the heap and then get access to it (giving out, saving, erasing, etc.).
This was no problem for me, so I thought if it would be also possible to do all (giving out, saving, erasing, etc) this through functions.

It is possible, but you have to format your functions and your declarations correctly. Have another look at this (slightly-modified) integer-based example:

#include <iostream>

//function prototypes:
int *generateAnInt();           //allocate a new int and return the pointer
void displayAnInt(int const *); //display the contents of the pointed integer


//IMPLEMENT THE MAIN FUNCTION
int main() {
  int *pAnInt = generateAnInt();

  *pAnInt = 50;

  displayAnInt(pAnInt);

  delete pAnInt;
  pAnInt = NULL;

  return 0;
}


//function implementations:
int *generateAnInt() {  //allocate a new int and return the pointer
  return new int(0);
} //end function

void displayAnInt(int const *pTheInt) { //display the contents of the pointed integer
  std::cout << *pTheInt << std::endl;
} //end function

Notice how I declared the pointer in main(). Then, to get the actual pointer, I called the function generateAnInt() to generate it and return it.

This is what you seem to attempt to do in the beginning of your main(), but your syntax is incomplete. I would also suggest that you change the name of your "pVector()" function to something more appropriate like "generateIntVector()" so that you aren't getting conflicts caused by re-using names.

Based on this information, how can you take your code and make it behave similarly to my example?

You are allocating new memory every time you call *pVketor() and I think that's the problem.

You are allocating new memory every time you call *pVketor() and I think that's the problem.

That is a problem, and a fairly significant one, but it's not THE problem. The bigger problem is the OP's understanding (or should I lack) of how the pointer works. It seems that the OP is trying to use the function pVector() as a variable instead of a function, forgetting that the natural encapsulation of the vector within the function prevents such use. Because they are attempting to use it in this way, they are generating a pointer to a vector, but not storing the returned pointer and so immediately lose access to it causing a memory leak (the issue that you are referring to).

I've demonstrated a correct technique a couple times, but they don't seem to be making the connection.

Based on this information, how can you take your code and make it behave similarly to my example?

Thanks for the explanation, now the code works and I think my understanding of pointers is a little bit better :-)

#include <iostream>
#include <vector>

using namespace std;

//prototypes
vector<int>*generateAVector();    //create a vector on the heap
void displayAVector(vector<int>*pTheVec);  //print the vector on the monitor
void SaveAnElementinTheVector(vector<int>*pTheVec,int a); //save an element in the vector

int main()
{
    vector<int>*pAVec=generateAVector();
    int a=50;
    SaveAnElementinTheVector(pAVec,a);
    displayAVector(pAVec);
    delete pAVec;
    pAVec=0;

    //cout << "Hello world!" << endl;
    return 0;
}

//implementation creating vector
vector<int>*generateAVector()
{
    return new vector<int>(0);
}
//implementation saving an element to the vector
void SaveAnElementinTheVector(vector<int>*pTheVec,int a)
{
    (*pTheVec).push_back(a);
}
//implementation of displaying the vector on the monitor
void displayAVector(vector<int>*pTheVec)
{
    cout<<(*pTheVec)[0]; 
}

Glad to hear it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.