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

Cannot pass char * array

Xx r3negade
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 2014
JosAH
11,448 Expert 8TB
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
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 Expert 2GB
@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 "simultaneous" 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
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...
6
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 ...
18
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
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...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...
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...
0
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...

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.