Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Passing pointer to structure

1 view
Skip to first unread message

Jon Champlin

unread,
Dec 30, 1998, 3:00:00 AM12/30/98
to
Well, hope this isn't too stupid a question or a platform-dependent issue. I
am trying to get some code I have to run after compiling it with IBM's
VisualAge C++ v3.5 for Windows. I have a struct defined that I pass to another
function with the _beginthread function call. I check the value of the
ServerLength variable before calling _beginthread and it is correct (28), but
when checking it as soon as the ProcessServer() function begins, its value is
wrong (68). When I change the order to have AdminLength first in the struct,
it is the one that is changed and ServerLength is fine, so I think I'm passing
the pointer properly, just the first element is getting screwed up. Does it
have anything to do with the packing of the memory (I am using /Sp1 - since the
API calls I am using don't seem to work without this parameter). Please post
response or e-mail me at cham...@us.ibm.com.

Here's the code:

typedef struct {
int ServerLength;
int AdminLength;
char *Server;
char *Admin;
} THREAD_STRUCT;


if ((ts = (THREAD_STRUCT *) malloc (sizeof(THREAD_STRUCT))) == NULL)
{
continue;
}

memset(ts, 0, sizeof(THREAD_STRUCT));
ts->Server = item_server;
ts->ServerLength = strlen(item_server);
ts->Admin = item_admin;
ts->AdminLength = strlen(item_admin);

<ServerLength is 28 at this point>
if ((rc = _beginthread(ProcessServer, NULL, 0, (void *) ts)) < 0)

void ProcessServer(void *ts_ptr)
THREAD_STRUCT *ts;

ts = (THREAD_STRUCT *) ts_ptr;
<ServerLength is 68 at this point>
strcpy(server_name, ts->Server);
strcpy(admin_name, ts->Admin);


Julie Holdstock

unread,
Dec 30, 1998, 3:00:00 AM12/30/98
to
On 30 Dec 1998 14:28:24 GMT, cham...@us.ibm.com (Jon Champlin) wrote:

You've not allocated any space for the strings in your structure. I've
put some ( almost certainly non-PC ) code in your snipped as an
example.

Happy new year,

Steve


>Well, hope this isn't too stupid a question or a platform-dependent issue. I
>am trying to get some code I have to run after compiling it with IBM's
>VisualAge C++ v3.5 for Windows. I have a struct defined that I pass to another
>function with the _beginthread function call. I check the value of the
>ServerLength variable before calling _beginthread and it is correct (28), but
>when checking it as soon as the ProcessServer() function begins, its value is
>wrong (68). When I change the order to have AdminLength first in the struct,
>it is the one that is changed and ServerLength is fine, so I think I'm passing
>the pointer properly, just the first element is getting screwed up. Does it
>have anything to do with the packing of the memory (I am using /Sp1 - since the
>API calls I am using don't seem to work without this parameter). Please post
>response or e-mail me at cham...@us.ibm.com.
>
>Here's the code:
>
>typedef struct {
> int ServerLength;
> int AdminLength;
> char *Server;
> char *Admin;
>} THREAD_STRUCT;
>
>
> if ((ts = (THREAD_STRUCT *) malloc (sizeof(THREAD_STRUCT))) == NULL)
> {
> continue;
> }
>
> memset(ts, 0, sizeof(THREAD_STRUCT));

ts->Server= (char *)malloc ( strlen ( item_server ) + 1 );
strcpy ( ts->Server, item_server );


> ts->Server = item_server;
> ts->ServerLength = strlen(item_server);

... and the same for this.

Will Rose

unread,
Dec 30, 1998, 3:00:00 AM12/30/98
to
Jon Champlin (cham...@us.ibm.com) wrote:
: Well, hope this isn't too stupid a question or a platform-dependent issue. I
: am trying to get some code I have to run after compiling it with IBM's
: VisualAge C++ v3.5 for Windows. I have a struct defined that I pass to another
: function with the _beginthread function call. I check the value of the
: ServerLength variable before calling _beginthread and it is correct (28), but
: when checking it as soon as the ProcessServer() function begins, its value is
: wrong (68). When I change the order to have AdminLength first in the struct,
: it is the one that is changed and ServerLength is fine, so I think I'm passing
: the pointer properly, just the first element is getting screwed up. Does it
: have anything to do with the packing of the memory (I am using /Sp1 - since the
: API calls I am using don't seem to work without this parameter). Please post
: response or e-mail me at cham...@us.ibm.com.

: Here's the code:

: typedef struct {
: int ServerLength;
: int AdminLength;
: char *Server;
: char *Admin;
: } THREAD_STRUCT;


: if ((ts = (THREAD_STRUCT *) malloc (sizeof(THREAD_STRUCT))) == NULL)
: {
: continue;
: }

: memset(ts, 0, sizeof(THREAD_STRUCT));
: ts->Server = item_server;
: ts->ServerLength = strlen(item_server);
: ts->Admin = item_admin;
: ts->AdminLength = strlen(item_admin);

: <ServerLength is 28 at this point>
: if ((rc = _beginthread(ProcessServer, NULL, 0, (void *) ts)) < 0)

: void ProcessServer(void *ts_ptr)
: THREAD_STRUCT *ts;

: ts = (THREAD_STRUCT *) ts_ptr;
: <ServerLength is 68 at this point>
: strcpy(server_name, ts->Server);
: strcpy(admin_name, ts->Admin);


You almost certainly need to check the linker conventions you are using;
under OS/2, at least, _beginthread must use _Optlink. However, you'll
get much better help on one of the IBM groups devoted to VAC++; I think
they are on news.software.ibm.com, but if not (and I don't have a
newsreader to check) ask in comp.os.os2.progamming.misc for the VAC++
newsgroups whereabouts.

Mind you, the #pragma pack won't help; you might try and see if the
link works without packing (ie. p4).

There's also a _lot_ of very powerful memory debugging tools in the
OS/2 compiler, and I'd expect them to be in the NT version as well;
you might dig around for those.


Will
c...@crash.cts.com


Jon Champlin

unread,
Dec 31, 1998, 3:00:00 AM12/31/98
to
Thanks, that did it. After I changed the int's to int *'s and malloc'ed memory
for them the parameters passed thru properly...


0 new messages