0

Inside a thread, I run this function:

char *r = NetString("ch","aaaaa");
printf("%s",r); printf("%s","\n");

Which calls

char *NetString(char *id, char *data) {
  char *result;
  result = (char *)malloc(strlen(id)+strlen(data)+2);
  strcpy(result, id);
  strcat(result, "/");
  strcat(result, data);
  printf("%s",result); printf("%s","\n");
  return (char *)result;
}

In the console, when I compile this warning shows up:

warning: initialization makes pointer from integer without a cast
[enabled by default]: char *r = NetString("ch","aaaaa");
                                ^

If you need the full code (which may be a bit unorganized), here it is: pastebin

Apparently, this code runs as expected (returns "ch/aaaaa" twice) when not run in a thread, but when I run it in a thread, the string only prints once (that is, in the NetString function). Any help? Thanks a lot.

Manel498
  • 3
  • 2
  • My guess is return in NetString finishes off your thread. But I am not sure. – Tony Tannous Feb 08 '17 at 11:58
  • 2
    always check the return value of malloc – Kami Kaze Feb 08 '17 at 11:59
  • 1
    and don't cast the return of malloc – Stargateur Feb 08 '17 at 12:00
  • do you run Netstring as a thread or is the call to Netstring in a thread? – Kami Kaze Feb 08 '17 at 12:01
  • If you understood how `strcat()` works you would never use it consecutively, specially if you already used `strlen()` with the components. Also, there is no guarantee that `id` or `data` are `nul` terminated because you did not post the code that initializes them. Finally casting `void *` to `char *` is not necessary but casting `char *` to `char *` indicates that something is wrong in your code. – Iharob Al Asimi Feb 08 '17 at 12:02
  • In short, the lack of context in your example does not help in finding out the problem. – Iharob Al Asimi Feb 08 '17 at 12:03
  • @TonyTannous I believe the thread keeps running, strings after that get printed correctly, it's just not the "r" one. – Manel498 Feb 08 '17 at 12:04
  • @IharobAlAsimi id and data are initialized in the NetString("ch","aaaaa") line... right? Also I removed both (char *) casts, no additional errors showed up. – Manel498 Feb 08 '17 at 12:10
  • Can you show how is the whole program running? – Iharob Al Asimi Feb 08 '17 at 12:12
  • @IharobAlAsimi added full code link in my question: http://pastebin.com/M38uFLbQ – Manel498 Feb 08 '17 at 12:16
  • 1
    The warning says that you forgot to declare NetString properly. Enable all warnings and treat them as errors (-Wall -Wextra -Werror). Saves all of us time. – n. m. could be an AI Feb 08 '17 at 12:17
  • @n.m. to be fair it doesn't say exactly that (there's [another warning](http://stackoverflow.com/questions/8440816/warning-implicit-declaration-of-function) for that one), but it does hint strongly at the issue. – Quentin Feb 08 '17 at 12:32

1 Answers1

1

This code

char *r = NetString("ch","aaaaa");

and this error

warning: initialization makes pointer from integer without a cast
[enabled by default]: char *r = NetString("ch","aaaaa");
                                ^

almost certainly mean:

  1. You are calling NetString() without providing a function prototype, thus it is treated as returning int.
  2. You are running on a 64-bit platform where pointers are 64 bit and int values are 32 bit.

Thus, your char * pointer value from NetString() gets truncated to an int, then assigned to a pointer, where it no longer points to anything useful.

You need to provide a proper prototype with a function declaration:

char *NetString( char *, char * );

in all code that calls NetString(), and the prototype needs to be before any such calls.

Andrew Henle
  • 32,625
  • 3
  • 24
  • 56