473,386 Members | 1,706 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,386 software developers and data experts.

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

Aug 29 '06 #1
6 2714

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
Aug 29 '06 #2
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
Aug 29 '06 #3
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
Aug 29 '06 #4
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.
Aug 29 '06 #5
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.
Aug 29 '06 #6

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

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
I dont know how to help you then.

Sep 1 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: mjm | last post by:
Folks, Stroustrup indicates that returning by value can be faster than returning by reference but gives no details as to the size of the returned object up to which this holds. My question is...
2
by: HealsJnr | last post by:
Hi all, Just wondering if there is any way to return a vector from a mixed mode dll? I understand that it is quite easy to pass by reference to achieve the same end, but i'd like to know if it...
26
by: cdg | last post by:
Could anyone correct any mistakes in this example program. I am just trying to return an array back to "main" to be printed out. And I am not sure how a "pointer to an array" is returned to the...
39
by: Martin Jrgensen | last post by:
Hi, I'm relatively new with C-programming and even though I've read about pointers and arrays many times, it's a topic that is a little confusing to me - at least at this moment: ---- 1)...
13
by: Gernot Frisch | last post by:
Which method is the fastest/best: std::vector<intfoo1() { std::vector<intv; ... reutun v; } std::vector<int>& foo2()
3
by: Michele | last post by:
Hello: What is the syntax for returning a reference to a vector from a function? I have a private vector and I want to return it using a public get function (like in the code below), but I...
13
by: Jeroen | last post by:
Hi all, I'm trying to implement a certain class but I have problems regarding the copy ctor. I'll try to explain this as good as possible and show what I tried thusfar. Because it's not about a...
7
by: arnuld | last post by:
/* C++ Primer - 4/e * * 1st example from section 7.2.2, page 234 * returning 2 values from a function * * STATEMENT: * to find a specific value in a vector and number of times * that...
23
by: pauldepstein | last post by:
Below is posted from a link for Stanford students in computer science. QUOTE BEGINS HERE Because of the risk of misuse, some experts recommend never returning a reference from a function or...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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
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.