| Topic: |
DEVELOP > c-Plus-Plus |
| User: |
"Rahul K" |
| Date: |
29 Aug 2006 01:04:51 AM |
| Object: |
Problem in returning a vector from a function |
Hi
I am working on Visual Studio on Windows. I have a function which
return the list of all the IP Addresses of a machine:
vector<char *> getAllLocalIPAddress()
{
char localHostName[MAX_HOSTNAME_LENGTH];
struct hostent *hp;
int i=0,rc;
vector<char *> allLocalIP;
/* Get the local hostname */
rc = getLocalHostName(localHostName, MAX_HOSTNAME_LENGTH); //This is
my function to get local hostname. Assume that it works fine
/* If getLocalHostName() failed */
if(rc != 0)
return allLocalIP;
#ifdef WIN32
/* Initiate wsock32.dll */
int success = utilWSAStartup(); //This is my function to initialize
wsock32.dll Lets assume that it is successful
/* The WinSock DLL is acceptable. Proceed. */
if(success == 0)
/* Get host information corresponding to local host */
hp = gethostbyname(localHostName);
WSACleanup();
#else
hp = gethostbyname(localHostName);
#endif
if(hp == NULL)
return allLocalIP ;
/*Push all the Local IP Addresses in the vector */
while(hp->h_addr_list[i] != NULL)
{
cout << "Inside while loop";
allLocalIP.push_back(inet_ntoa(*(struct in_addr
*)(hp->h_addr_list[i])));
i++;
}
/*Return the vector containing all the Local IP Address */
cout << "Outputting all local IP addresses inside function itself\n";
for(i=0 ;i<allLocalIP.size(); i++)
cout << allLocalIP[i] << "\n";
/*Return the vector containing all the Local IP Address */
return allLocalIP;
}
This function is in a DLL .
As can be seen, I am printing all the IP Addresses inside function
itself also. This is working fine and printing IP addresses well.
When i write a application and link it to this DLL, I make the call as
follows;
vector<char *> localIP;
localIP = getAllLocalIPAddress();
At this point when the function return and try to assign the return
value to vector localIP, it gives error something like this:
Debug Assertion failed
File: dbgheap.c
Line: 1132
Expression: _CrtIsValidHeapPointer(pUserData)
Can anybody please tell me where I am doing wrong.
Regards
Rahul
.
|
|
| User: "Jens Theisen" |
|
| Title: Re: Problem in returning a vector from a function |
29 Aug 2006 02:54:38 AM |
|
|
Rahul K wrote:
Hi
I am working on Visual Studio on Windows. I have a function which
return the list of all the IP Addresses of a machine:
vector<char *> getAllLocalIPAddress()
{
char localHostName[MAX_HOSTNAME_LENGTH];
struct hostent *hp;
int i=0,rc;
vector<char *> allLocalIP;
/* Get the local hostname */
rc = getLocalHostName(localHostName, MAX_HOSTNAME_LENGTH); //This is
my function to get local hostname. Assume that it works fine
/* If getLocalHostName() failed */
if(rc != 0)
return allLocalIP;
#ifdef WIN32
/* Initiate wsock32.dll */
int success = utilWSAStartup(); //This is my function to initialize
wsock32.dll Lets assume that it is successful
/* The WinSock DLL is acceptable. Proceed. */
if(success == 0)
/* Get host information corresponding to local host */
hp = gethostbyname(localHostName);
WSACleanup();
#else
hp = gethostbyname(localHostName);
#endif
if(hp == NULL)
return allLocalIP ;
/*Push all the Local IP Addresses in the vector */
while(hp->h_addr_list[i] != NULL)
{
cout << "Inside while loop";
allLocalIP.push_back(inet_ntoa(*(struct in_addr
*)(hp->h_addr_list[i])));
i++;
}
/*Return the vector containing all the Local IP Address */
cout << "Outputting all local IP addresses inside function itself\n";
for(i=0 ;i<allLocalIP.size(); i++)
cout << allLocalIP[i] << "\n";
/*Return the vector containing all the Local IP Address */
return allLocalIP;
}
This function is in a DLL .
As can be seen, I am printing all the IP Addresses inside function
itself also. This is working fine and printing IP addresses well.
When i write a application and link it to this DLL, I make the call as
follows;
vector<char *> localIP;
localIP = getAllLocalIPAddress();
At this point when the function return and try to assign the return
value to vector localIP, it gives error something like this:
The function inet_ntoa returns a pointer to an internal buffer. This
buffer is only valid until the next call to this function (or maybe even
less than that, I'm not sure). Do you really see all _different_ ip
addresses on your debug output before returning? I'd suspect that they
are all the last IP address.
Try replacing the string holding array to type
vector< std::string >
Then the string will be copied in it's own buffer.
Use std::string as often as you can, raw pointers are error prone.
Jens
.
|
|
|
|
| User: "jimmy" |
|
| Title: Re: Problem in returning a vector from a function |
29 Aug 2006 02:09:02 AM |
|
|
Rahul K wrote:
Hi
I am working on Visual Studio on Windows. I have a function which
return the list of all the IP Addresses of a machine:
vector<char *> getAllLocalIPAddress()
{
char localHostName[MAX_HOSTNAME_LENGTH];
struct hostent *hp;
int i=0,rc;
vector<char *> allLocalIP;
As far as i know, the allLocalIP here is a temperoary variable, when
you return your this function the varible will be desturcted,so do the
pointers in the vector.
But u just wanner get a copy of this, i dont think it's a copy
construtor here. So u failed.
Why u just do it like this:
getAllLocalIPAddress(vector<char *> allLocallIP);
wish this be helpful.
/* Get the local hostname */
rc = getLocalHostName(localHostName, MAX_HOSTNAME_LENGTH); //This is
my function to get local hostname. Assume that it works fine
/* If getLocalHostName() failed */
if(rc != 0)
return allLocalIP;
#ifdef WIN32
/* Initiate wsock32.dll */
int success = utilWSAStartup(); //This is my function to initialize
wsock32.dll Lets assume that it is successful
/* The WinSock DLL is acceptable. Proceed. */
if(success == 0)
/* Get host information corresponding to local host */
hp = gethostbyname(localHostName);
WSACleanup();
#else
hp = gethostbyname(localHostName);
#endif
if(hp == NULL)
return allLocalIP ;
/*Push all the Local IP Addresses in the vector */
while(hp->h_addr_list[i] != NULL)
{
cout << "Inside while loop";
allLocalIP.push_back(inet_ntoa(*(struct in_addr
*)(hp->h_addr_list[i])));
i++;
}
/*Return the vector containing all the Local IP Address */
cout << "Outputting all local IP addresses inside function itself\n";
for(i=0 ;i<allLocalIP.size(); i++)
cout << allLocalIP[i] << "\n";
/*Return the vector containing all the Local IP Address */
return allLocalIP;
}
This function is in a DLL .
As can be seen, I am printing all the IP Addresses inside function
itself also. This is working fine and printing IP addresses well.
When i write a application and link it to this DLL, I make the call as
follows;
vector<char *> localIP;
localIP = getAllLocalIPAddress();
At this point when the function return and try to assign the return
value to vector localIP, it gives error something like this:
Debug Assertion failed
File: dbgheap.c
Line: 1132
Expression: _CrtIsValidHeapPointer(pUserData)
Can anybody please tell me where I am doing wrong.
Regards
Rahul
.
|
|
|
| User: "Rahul K" |
|
| Title: Re: Problem in returning a vector from a function |
29 Aug 2006 02:32:19 AM |
|
|
I would like to tell some more observations:
1. The same flow of code works fine on Solaris. Had it been the
question of a local variable getting destroyed, it should have behaved
the same way on Linux also, as its a C++ concept and should not vary
across OS or compilers.
2. If instead of linking the calling function with the DLL, I put
entire stuff in a single file, it works fine on windows also.
3. This successfull working also proves that vector provides you with a
copy constructor. And ideally it should provide also as its a part of
STL .
4. The function gethostbyname() returns struct hostent *. SO it should
hav malloced() it inside itself. So this pointer is pointing to a
memory location in heap which contains some data(which in this case,
turns out to be another pointer). And my vector contains this data. So
assuming that the copy constructor for vector works, this pointer
should still be valid as it points to memory in heap.
So please take these points also in consideration, and correct me if I
am wrong somewhere.
Rahul
jimmy wrote:
Rahul K wrote:
Hi
I am working on Visual Studio on Windows. I have a function which
return the list of all the IP Addresses of a machine:
vector<char *> getAllLocalIPAddress()
{
char localHostName[MAX_HOSTNAME_LENGTH];
struct hostent *hp;
int i=0,rc;
vector<char *> allLocalIP;
As far as i know, the allLocalIP here is a temperoary variable, when
you return your this function the varible will be desturcted,so do the
pointers in the vector.
But u just wanner get a copy of this, i dont think it's a copy
construtor here. So u failed.
Why u just do it like this:
getAllLocalIPAddress(vector<char *> allLocallIP);
wish this be helpful.
/* Get the local hostname */
rc = getLocalHostName(localHostName, MAX_HOSTNAME_LENGTH); //This is
my function to get local hostname. Assume that it works fine
/* If getLocalHostName() failed */
if(rc != 0)
return allLocalIP;
#ifdef WIN32
/* Initiate wsock32.dll */
int success = utilWSAStartup(); //This is my function to initialize
wsock32.dll Lets assume that it is successful
/* The WinSock DLL is acceptable. Proceed. */
if(success == 0)
/* Get host information corresponding to local host */
hp = gethostbyname(localHostName);
WSACleanup();
#else
hp = gethostbyname(localHostName);
#endif
if(hp == NULL)
return allLocalIP ;
/*Push all the Local IP Addresses in the vector */
while(hp->h_addr_list[i] != NULL)
{
cout << "Inside while loop";
allLocalIP.push_back(inet_ntoa(*(struct in_addr
*)(hp->h_addr_list[i])));
i++;
}
/*Return the vector containing all the Local IP Address */
cout << "Outputting all local IP addresses inside function itself\n";
for(i=0 ;i<allLocalIP.size(); i++)
cout << allLocalIP[i] << "\n";
/*Return the vector containing all the Local IP Address */
return allLocalIP;
}
This function is in a DLL .
As can be seen, I am printing all the IP Addresses inside function
itself also. This is working fine and printing IP addresses well.
When i write a application and link it to this DLL, I make the call as
follows;
vector<char *> localIP;
localIP = getAllLocalIPAddress();
At this point when the function return and try to assign the return
value to vector localIP, it gives error something like this:
Debug Assertion failed
File: dbgheap.c
Line: 1132
Expression: _CrtIsValidHeapPointer(pUserData)
Can anybody please tell me where I am doing wrong.
Regards
Rahul
.
|
|
|
| User: "jimmy" |
|
| Title: Re: Problem in returning a vector from a function |
31 Aug 2006 08:22:43 PM |
|
|
Rahul K =E5=86=99=E9=81=93=EF=BC=9A
I would like to tell some more observations:
1. The same flow of code works fine on Solaris. Had it been the
question of a local variable getting destroyed, it should have behaved
the same way on Linux also, as its a C++ concept and should not vary
across OS or compilers.
2. If instead of linking the calling function with the DLL, I put
entire stuff in a single file, it works fine on windows also.
3. This successfull working also proves that vector provides you with a
copy constructor. And ideally it should provide also as its a part of
STL .
4. The function gethostbyname() returns struct hostent *. SO it should
hav malloced() it inside itself. So this pointer is pointing to a
memory location in heap which contains some data(which in this case,
turns out to be another pointer). And my vector contains this data. So
assuming that the copy constructor for vector works, this pointer
should still be valid as it points to memory in heap.
So please take these points also in consideration, and correct me if I
am wrong somewhere.
Rahul
jimmy wrote:
Rahul K wrote:
Hi
I am working on Visual Studio on Windows. I have a function which
return the list of all the IP Addresses of a machine:
vector<char *> getAllLocalIPAddress()
{
char localHostName[MAX_HOSTNAME_LENGTH];
struct hostent *hp;
int i=3D0,rc;
vector<char *> allLocalIP;
As far as i know, the allLocalIP here is a temperoary variable, when
you return your this function the varible will be desturcted,so do the
pointers in the vector.
But u just wanner get a copy of this, i dont think it's a copy
construtor here. So u failed.
Why u just do it like this:
getAllLocalIPAddress(vector<char *> allLocallIP);
wish this be helpful.
/* Get the local hostname */
rc =3D getLocalHostName(localHostName, MAX_HOSTNAME_LENGTH); //Thi=
s is
my function to get local hostname. Assume that it works fine
/* If getLocalHostName() failed */
if(rc !=3D 0)
return allLocalIP;
#ifdef WIN32
/* Initiate wsock32.dll */
int success =3D utilWSAStartup(); //This is my function to initia=
lize
wsock32.dll Lets assume that it is successful
/* The WinSock DLL is acceptable. Proceed. */
if(success =3D=3D 0)
/* Get host information corresponding to local host */
hp =3D gethostbyname(localHostName);
WSACleanup();
#else
hp =3D gethostbyname(localHostName);
#endif
if(hp =3D=3D NULL)
return allLocalIP ;
/*Push all the Local IP Addresses in the vector */
while(hp->h_addr_list[i] !=3D NULL)
{
cout << "Inside while loop";
allLocalIP.push_back(inet_ntoa(*(struct in_addr
*)(hp->h_addr_list[i])));
i++;
}
/*Return the vector containing all the Local IP Address */
cout << "Outputting all local IP addresses inside function itself\n";
for(i=3D0 ;i<allLocalIP.size(); i++)
cout << allLocalIP[i] << "\n";
/*Return the vector containing all the Local IP Address */
return allLocalIP;
}
This function is in a DLL .
As can be seen, I am printing all the IP Addresses inside function
itself also. This is working fine and printing IP addresses well.
When i write a application and link it to this DLL, I make the call as
follows;
vector<char *> localIP;
localIP =3D getAllLocalIPAddress();
At this point when the function return and try to assign the return
value to vector localIP, it gives error something like this:
Debug Assertion failed
File: dbgheap.c
Line: 1132
Expression: _CrtIsValidHeapPointer(pUserData)
Can anybody please tell me where I am doing wrong.
=20
Regards
Rahul
I dont know how to help you then.
.
|
|
|
|
| User: "Ian Collins" |
|
| Title: Re: Problem in returning a vector from a function |
29 Aug 2006 06:26:11 AM |
|
|
Rahul K wrote:
Please don't top post.
jimmy wrote:
Rahul K wrote:
Hi
I am working on Visual Studio on Windows. I have a function which
return the list of all the IP Addresses of a machine:
vector<char *> getAllLocalIPAddress()
{
char localHostName[MAX_HOSTNAME_LENGTH];
struct hostent *hp;
int i=0,rc;
vector<char *> allLocalIP;
As far as i know, the allLocalIP here is a temperoary variable, when
you return your this function the varible will be desturcted,so do the
pointers in the vector.
But u just wanner get a copy of this, i dont think it's a copy
construtor here. So u failed.
I would like to tell some more observations:
1. The same flow of code works fine on Solaris. Had it been the
question of a local variable getting destroyed, it should have behaved
the same way on Linux also, as its a C++ concept and should not vary
across OS or compilers.
Probably more by luck tan anything else.
3. This successfull working also proves that vector provides you with a
copy constructor. And ideally it should provide also as its a part of
STL .
It's not the vector that's your problem, it's the array localHostName.
Your vector contains pointers into this local array, which vanishes when
it goes out of scope.
Change your vector to a vector of std::string.
--
Ian Collins.
.
|
|
|
|
|
| User: "Ian Collins" |
|
| Title: Re: Problem in returning a vector from a function |
29 Aug 2006 06:27:55 AM |
|
|
jimmy wrote:
Rahul K wrote:
Hi
I am working on Visual Studio on Windows. I have a function which
return the list of all the IP Addresses of a machine:
vector<char *> getAllLocalIPAddress()
{
char localHostName[MAX_HOSTNAME_LENGTH];
struct hostent *hp;
int i=0,rc;
vector<char *> allLocalIP;
As far as i know, the allLocalIP here is a temperoary variable, when
you return your this function the varible will be desturcted,so do the
pointers in the vector.
But u just wanner get a copy of this, i dont think it's a copy
construtor here. So u failed.
Please try and use proper English words and capitalisation, rather than
silly abbreviations like 'u'.
--
Ian Collins.
.
|
|
|
|
|

|
Related Articles |
|
|