473,698 Members | 2,445 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Cannot pass char * array

Xx r3negade
39 New Member
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:

Expand|Select|Wrap|Line Numbers
  1. struct __tryConnect_args
  2. {
  3.     int sock;
  4.     char * hoststr;
  5.     unsigned int port;
  6.     int proto;
  7. };
  8.  
My problem is with the char * hoststr member of the struct. Take a look at this code:

Expand|Select|Wrap|Line Numbers
  1. char * hostString;
  2. hostString = dnsLookup(argv[1], AF_INET); /* prototype : char * dnsLookup(char *, int) */
  3.  
  4. printf("Connecting to %s\n", hostString);
  5.  
  6. for (i = 0; i < maxConns; i++)
  7. {
  8.         connArgs[i].sock = sock;
  9.         connArgs[i].hoststr = hostString;
  10.         connArgs[i].proto = PF_INET;
  11.         connArgs[i].port = port;
  12.  
  13.         pthread_create( &connThreads[i],
  14.                                  NULL,
  15.                                  &__tryConnect,
  16.                                  (void *) &connArgs[i] );
  17. }
  18.  
When __tryConnect() is executed, "sock", "proto", and "port" are passed correctly, but "hoststr" becomes gibberish.

__tryConnect() looks like this:

Expand|Select|Wrap|Line Numbers
  1. void * __tryConnect(void * args)
  2. {
  3.     struct __tryConnect_args * argsStruct;
  4.     argsStruct = (struct __tryConnect_args *) args;
  5.     printf("args: hoststr: %s\nport: %d\n", argsStruct->hoststr, argsStruct->port);
  6. }
  7.  
The program's output looks like this:
Expand|Select|Wrap|Line Numbers
  1. Getting address info for google.com
  2. Connecting to 209.85.171.100
  3. args: hoststr: �        �@
  4. port: 80
  5.  
What is happening with hoststr?
Dec 20 '08 #1
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
Dec 21 '08 #2
Xx r3negade
39 New Member
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:
Expand|Select|Wrap|Line Numbers
  1. char * dnsLookup(char * hoststr, unsigned int proto)
  2. {
  3.    return "testing";
  4. }
  5.  
everything works fine, without having to copy the character buffer. So the problem lies in my dnsLookup() function...
Dec 21 '08 #3
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!).
Dec 22 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

7
25169
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.
6
38069
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
18
4054
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;
45
18872
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
0
8675
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, 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...
0
8604
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,...
0
9029
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 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...
1
8897
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,...
0
8862
tracyyun
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...
0
7729
agi2029
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...
0
5860
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();...
0
4619
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2331
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.