pointer to map is not behaving properly across function calls (for VC++ compiler)

A

amit kumar

I am calling a function which returns pointer to a map.
The declaration of the map is map<int,vectxyz*>. vectxyz is a vector
containing pointer to a class xyz.
For map<int,vectxyz*>* p1
In the called function, I am using p1->find(1) which is returning a
valid iterator and not going to the end. I am returning p1 from the
called function.

But in the calling function, find(1) is going to the end, i.e unable
to find the key 1, which was located in the called function.
Also p1->count(1) is showing 1 in called function whereas showing 0 in
calling function.
The pointer value is same in both calling and calld function, as I
printed them in both calling and called program.
The other map functions, say size(), begin() are working in the
calling function.

Can anyone help me and tell why the find() function is working in
called function and not in calling function.

I also tried to implement compare function (function object) for the
map but I am getting an error fatal error LNK1120: 1 unresolved
externals.

Thanks & Regards
Amit Kumar.
 
J

Jeff Schwab

amit said:
I am calling a function which returns pointer to a map.
....

Can anyone help me and tell why the find() function is working in
called function and not in calling function.

Post code.
 
J

John Harrison

amit kumar said:
I am calling a function which returns pointer to a map.
The declaration of the map is map<int,vectxyz*>. vectxyz is a vector
containing pointer to a class xyz.

So you are returning a pointer to a map, which contains pointers to a
vector, which contains pointer to xyz. Did you ever think there are too many
pointers?
For map<int,vectxyz*>* p1
In the called function, I am using p1->find(1) which is returning a
valid iterator and not going to the end. I am returning p1 from the
called function.

But in the calling function, find(1) is going to the end, i.e unable
to find the key 1, which was located in the called function.
Also p1->count(1) is showing 1 in called function whereas showing 0 in
calling function.
The pointer value is same in both calling and calld function, as I
printed them in both calling and called program.
The other map functions, say size(), begin() are working in the
calling function.

Can anyone help me and tell why the find() function is working in
called function and not in calling function.

It's because there is a bug in your program. I wouldn't be surprised to find
that the bug was due to the complicated pointer usage. You need to post some
code, try posting the called function, the calling function and the
declaration of all objects concerned.
I also tried to implement compare function (function object) for the
map but I am getting an error fatal error LNK1120: 1 unresolved
externals.

Again without seeing any code it is impossible to help.

john
 
A

amit kumar

John Harrison said:
So you are returning a pointer to a map, which contains pointers to a
vector, which contains pointer to xyz. Did you ever think there are too many
pointers?


It's because there is a bug in your program. I wouldn't be surprised to find
that the bug was due to the complicated pointer usage. You need to post some
code, try posting the called function, the calling function and the
declaration of all objects concerned.


Again without seeing any code it is impossible to help.

john


In file x1.h

class Cx1
{
public:
typedef vector<Cx1 *> vectx1;
private:
...
protected:
...
}


In file xempts.h

#include "x1.h"
class xempts :: public Cx1

{
public:
typedef map<int, vectx1*> mapTableData;
private:
mapTableData** m_ppvtableData;
...
public:
mapTableData* getxemptData();
...
};

In file xempts.cpp

#include "xempts.h"

xempts::mapTableData* xempts::getxemptData()
{
if (m_ppvtableData != 0)
{
// Here (*m_ppvtableData)->find(1) != end() ,
(*m_ppvtableData)->count(1) == 1
// *m_ppvtableData is pointing to a correct location.Using the
*m_ppvtableData I am able
// to find the element 1 in the map.
return *m_ppvtableData;
}
else
return 0;

} //getxemptData


In file Pkg.h

namespace bp_pkg
{
class A
{
public:
int getxemptions();
private:
...
protected:
...
};
}


In file Pkg.cpp

#include "xempts.h"

using namespace bp_pkg;
using namespace std;

int A :: getxemptions()
{
xempts * p1;
p1 = (xempts *)getPointer();// will return a valid pointer to xempts
xempts::mapTableData* mapData = 0;
mapData = p1->getxemptData(); // pointer mapData is same as that
returned by getXemptData
int i = mapData->count(1); //returns 0, whereas 1 in getxemptData();
xempts::mapTableData::iterator tmpItor;

tmpItor = mapData->find(1); // Points to end();
If (tmpItor != mapData->end())
{ cout << "Key 1 found";
return 1;
}
else
{
cout << "pointing to the end ";
return 0;
}
}


In the above mentioned code snippet, int A :: getxemptions() is the
calling function and xempts::mapTableData* xempts::getxemptData() is
the called function.
Even though the pointer returned is the same in both the called
function and the calling function, map is behaving differently.
In the called function
(*m_ppvtableData)->find(1) returns a valid iterator and
(*m_ppvtableData)->count(1) returns 1
whereas in the calling function
mapData->count(1); returns 0 and mapDat0a->find(1) points to the
end(), i.e does not return a valid iterator.
I am using Visual Studio 6.0 with service pack 3
In debug build, I am not getting error but in release build I am
observing this strange behaviour. The other map functions say begin(),
size() are working fine in both the functions, but find(), count() are
not working properly.

Could anyone please tell the possible causes of the problem and any
work around.

Thanks & Regards
Amit
 
J

John Harrison

In file x1.h

class Cx1
{
public:
typedef vector<Cx1 *> vectx1;
private:
...
protected:
...
}


In file xempts.h

#include "x1.h"
class xempts :: public Cx1

{
public:
typedef map<int, vectx1*> mapTableData;
private:
mapTableData** m_ppvtableData;
...
public:
mapTableData* getxemptData();
...
};

In file xempts.cpp

#include "xempts.h"

xempts::mapTableData* xempts::getxemptData()
{
if (m_ppvtableData != 0)
{
// Here (*m_ppvtableData)->find(1) != end() ,
(*m_ppvtableData)->count(1) == 1
// *m_ppvtableData is pointing to a correct location.Using the
*m_ppvtableData I am able
// to find the element 1 in the map.
return *m_ppvtableData;
}
else
return 0;

} //getxemptData


In file Pkg.h

namespace bp_pkg
{
class A
{
public:
int getxemptions();
private:
...
protected:
...
};
}


In file Pkg.cpp

#include "xempts.h"

using namespace bp_pkg;
using namespace std;

int A :: getxemptions()
{
xempts * p1;
p1 = (xempts *)getPointer();// will return a valid pointer to xempts
xempts::mapTableData* mapData = 0;
mapData = p1->getxemptData(); // pointer mapData is same as that
returned by getXemptData
int i = mapData->count(1); //returns 0, whereas 1 in getxemptData();
xempts::mapTableData::iterator tmpItor;

tmpItor = mapData->find(1); // Points to end();
If (tmpItor != mapData->end())
{ cout << "Key 1 found";
return 1;
}
else
{
cout << "pointing to the end ";
return 0;
}
}


In the above mentioned code snippet, int A :: getxemptions() is the
calling function and xempts::mapTableData* xempts::getxemptData() is
the called function.
Even though the pointer returned is the same in both the called
function and the calling function, map is behaving differently.
In the called function
(*m_ppvtableData)->find(1) returns a valid iterator and
(*m_ppvtableData)->count(1) returns 1
whereas in the calling function
mapData->count(1); returns 0 and mapDat0a->find(1) points to the
end(), i.e does not return a valid iterator.
I am using Visual Studio 6.0 with service pack 3
In debug build, I am not getting error but in release build I am
observing this strange behaviour. The other map functions say begin(),
size() are working fine in both the functions, but find(), count() are
not working properly.

Could anyone please tell the possible causes of the problem and any
work around.

I can't see any bugs in the code you posted, and I can't think of anything
that would cause the symptoms you describe. Probably the bug is somewhere
else in your code.

Probably the best thing to do is start removing code from your program. Try
to create a small program that still has the strange behaviour you describe.
Then post that complete small program here.

Sorry I couldn't be of more help.

john
 
V

velthuijsen

[snip]
xempts::mapTableData* xempts::getxemptData()
{
if (m_ppvtableData != 0)
{
// Here (*m_ppvtableData)->find(1) != end() ,
(*m_ppvtableData)->count(1) == 1
// *m_ppvtableData is pointing to a correct location.Using the
*m_ppvtableData I am able
// to find the element 1 in the map.
return *m_ppvtableData;
}
else
return 0;

} //getxemptData
If this is a piece of code that you did a copy/paste on then you might
want to alter
(*m_ppvtableData)->count(1) == 1
to
if (*m_ppvtableData)->count(1) == 1)

to keep it in line with how you claim it is supposed to work
since now it just checks if the pointer exists.
If this is not the case then I have no clue what might be wrong.

[snip]
In the above mentioned code snippet, int A :: getxemptions() is the
calling function and xempts::mapTableData* xempts::getxemptData() is
the called function.
Even though the pointer returned is the same in both the called
function and the calling function, map is behaving differently.
In the called function
(*m_ppvtableData)->find(1) returns a valid iterator and
(*m_ppvtableData)->count(1) returns 1
whereas in the calling function
mapData->count(1); returns 0 and mapDat0a->find(1) points to the
end(), i.e does not return a valid iterator.
[snip]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top