EDIT: Title is a misnomer, it's not an array it's a pointer
I have a struct that I use to pass arguments to a function referenced by pthread_create( ). It looks like this: -
struct __tryConnect_args
-
{
-
int sock;
-
char * hoststr;
-
unsigned int port;
-
int proto;
-
};
-
My problem is with the char * hoststr member of the struct. Take a look at this code: -
char * hostString;
-
hostString = dnsLookup(argv[1], AF_INET); /* prototype : char * dnsLookup(char *, int) */
-
-
printf("Connecting to %s\n", hostString);
-
-
for (i = 0; i < maxConns; i++)
-
{
-
connArgs[i].sock = sock;
-
connArgs[i].hoststr = hostString;
-
connArgs[i].proto = PF_INET;
-
connArgs[i].port = port;
-
-
pthread_create( &connThreads[i],
-
NULL,
-
&__tryConnect,
-
(void *) &connArgs[i] );
-
}
-
When __tryConnect() is executed, "sock", "proto", and "port" are passed correctly, but "hoststr" becomes gibberish.
__tryConnect() looks like this: -
void * __tryConnect(void * args)
-
{
-
struct __tryConnect_args * argsStruct;
-
argsStruct = (struct __tryConnect_args *) args;
-
printf("args: hoststr: %s\nport: %d\n", argsStruct->hoststr, argsStruct->port);
-
}
-
The program's output looks like this: -
Getting address info for google.com
-
Connecting to 209.85.171.100
-
args: hoststr: � �@
-
port: 80
-
What is happening with hoststr?
3 2024 JosAH 11,448
Recognized Expert MVP
What happens if you make a copy of the char* string right after dnsLookup() has returned it to you? It seems as if the buffer returned by that function is reused for other purposes ...
kind regards,
Jos
OK, that works. I don't know what's happening with hoststr, though...the only two times that it's used in the program are the ones shown in the code. Whatever, it works though. Thanks.
EDIT: On a side note, if I replace the code in dnsLookup() so it looks like this: -
char * dnsLookup(char * hoststr, unsigned int proto)
-
{
-
return "testing";
-
}
-
everything works fine, without having to copy the character buffer. So the problem lies in my dnsLookup() function...
donbock 2,426
Recognized Expert Top Contributor @Xx r3negade
Nothing happens to hoststr (ie, the pointer). The problem is with the string being pointed at.
Function dnsLookup apparently constructs the string in a static buffer and returns a pointer to that buffer. Apparently some other function (perhaps pthread_connect ) uses that same buffer as a scratchpad, thereby corrupting the string that you expect hoststr to point to.
You should always be suspicious of functions that return a pointer: what is the lifetime of the buffer being pointed to? There are two ways you can be burned: an interposed function call might corrupt the buffer; or "simultaneo us" calls to the same function by two threads might leave both threads pointing to the same buffer.
Best is when a function copies such information into a buffer you provide as an argument; second best is for you to copy information from a "built-in" buffer into one of your own; third best is a function that mallocs the necessary buffer (but don't forget to free it!).
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: ritchie |
last post by:
Hi all,
I am new to this group and I have question that you may be able to
help me with.
I am trying to learn C but am currently stuck on this.
First of all, I have a function for each sort (Bubble, insertion,
selection..).
I have an array of int's and am passing them to each sort function.
|
by: baret bonden |
last post by:
I get :Value of type 'String' cannot be converted to '1-dimensional array
of String' refering to curitem
Dim curItem As String
curItem = ListBox1.SelectedItem
TextBox1.Text = curItem
vcar = curItem
|
by: Pedro Pinto |
last post by:
Hi there once more........
Instead of showing all the code my problem is simple.
I've tried to create this function:
char temp(char *string){
alterString(string);
return string;
|
by: Zytan |
last post by:
This returns the following error:
"Cannot modify the return value of
'System.Collections.Generic.List<MyStruct>.this' because it is
not a variable"
and I have no idea why! Do lists return copies of their elements?
Why can't I change the element itself?
class Program
{
private struct MyStruct
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |