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

Discussion in 'C++' started by amit kumar, May 17, 2004.

  1. amit kumar

    amit kumar Guest

    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.
     
    amit kumar, May 17, 2004
    #1
    1. Advertising

  2. amit kumar

    Jeff Schwab Guest

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

    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.
     
    Jeff Schwab, May 17, 2004
    #2
    1. Advertising

  3. "amit kumar" <> wrote in message
    news:...
    > 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
     
    John Harrison, May 17, 2004
    #3
  4. amit kumar

    amit kumar Guest

    "John Harrison" <> wrote in message news:<>...
    > "amit kumar" <> wrote in message
    > news:...
    > > 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
     
    amit kumar, May 18, 2004
    #4
  5. >
    >
    > 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
     
    John Harrison, May 18, 2004
    #5
  6. amit kumar

    velthuijsen Guest

    [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]
     
    velthuijsen, May 18, 2004
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Steve - DND
    Replies:
    3
    Views:
    563
    Saravana [MVP]
    May 20, 2004
  2. DamonChong
    Replies:
    8
    Views:
    329
    Old Wolf
    Jan 30, 2005
  3. Siam
    Replies:
    4
    Views:
    411
    Pete Becker
    Jul 27, 2006
  4. Kai-Uwe Bux
    Replies:
    1
    Views:
    1,158
    Kai-Uwe Bux
    Dec 21, 2008
  5. Bob
    Replies:
    5
    Views:
    272
Loading...

Share This Page