469,964 Members | 1,518 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,964 developers. It's quick & easy.

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

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.
Jul 22 '05 #1
5 1955
amit kumar wrote:
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.
Jul 22 '05 #2

"amit kumar" <am*************@yahoo.co.in> wrote in message
news:ba**************************@posting.google.c om...
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
Jul 22 '05 #3
"John Harrison" <jo*************@hotmail.com> wrote in message news:<2g************@uni-berlin.de>...
"amit kumar" <am*************@yahoo.co.in> wrote in message
news:ba**************************@posting.google.c om...
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

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
Jul 22 '05 #4
>

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
Jul 22 '05 #5
[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]
Jul 22 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by Connell Gauld | last post: by
11 posts views Thread by Enquiries, Hopkins Research | last post: by
10 posts views Thread by Ant | last post: by
1 post views Thread by rainxy | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.