Problem in returning a vector from a function

R

Rahul K

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 != NULL)
{
cout << "Inside while loop";
allLocalIP.push_back(inet_ntoa(*(struct in_addr
*)(hp->h_addr_list)));
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 << "\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
 
J

jimmy

Rahul said:
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 != NULL)
{
cout << "Inside while loop";
allLocalIP.push_back(inet_ntoa(*(struct in_addr
*)(hp->h_addr_list)));
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 << "\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
 
R

Rahul K

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
Rahul said:
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 != NULL)
{
cout << "Inside while loop";
allLocalIP.push_back(inet_ntoa(*(struct in_addr
*)(hp->h_addr_list)));
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 << "\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
 
J

Jens Theisen

Rahul said:
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 != NULL)
{
cout << "Inside while loop";
allLocalIP.push_back(inet_ntoa(*(struct in_addr
*)(hp->h_addr_list)));
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 << "\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
 
I

Ian Collins

Rahul K wrote:

Please don't top post.
jimmy said:
Rahul said:
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.
 
I

Ian Collins

jimmy said:
Rahul said:
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'.
 
J

jimmy

Rahul K 写é“:
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
Rahul said:
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 != NULL)
{
cout << "Inside while loop";
allLocalIP.push_back(inet_ntoa(*(struct in_addr
*)(hp->h_addr_list)));
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 << "\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


I dont know how to help you then.
 

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

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top