473,385 Members | 2,044 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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 2136
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Connell Gauld | last post by:
Hi, I have come across a problem with a slightly complicated section to a program I am writing. I have a class (let's call it ClassA) which has, in its public area, a variable (which happens to...
11
by: Enquiries, Hopkins Research | last post by:
Hi all I have a conundrum that is puzzling me. I have a large codebase in C that I am converting to C++ as fast as possible (i.e. slowly because I keep learning new idioms and stumbling with...
4
by: anonymous | last post by:
Thanks your reply. The article I read is from www.hakin9.org/en/attachments/stackoverflow_en.pdf. And you're right. I don't know it very clearly. And that's why I want to understand it; for it's...
5
by: AK | last post by:
I'm writing a Windows Forms application in C++.NET. I've defined function F which takes 2 arguments a & b. I need to use the pointer of F inside another function G & I can't figure out how to do...
10
by: Ant | last post by:
Hi, I am having trouble with a member function pointer. It sees to give me the followign error Run-Time Check Failure #0 - The value of ESP was not properly saved across a function call. ...
17
by: I.M. !Knuth | last post by:
Hi. I'm more-or-less a C newbie. I thought I had pointers under control until I started goofing around with this: ...
26
by: Bill Reid | last post by:
Bear with me, as I am not a "professional" programmer, but I was working on part of program that reads parts of four text files into a buffer which I re-allocate the size as I read each file. I...
11
by: Felix Kater | last post by:
Hi, I can compile and run this code (see below) which twice calls the function f, first with too less, second with too much arguments. But is it legal and free of memory leaks and other...
8
by: Rahul | last post by:
Please read the following code class Test{ public: void * operator new (size_t t) { return malloc(t); } void operator delete (void *p) { free(p); } };
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.