Gon posted:
Can some one help me out how to rewrite the below line to avoid
errors/warings on a windows 64-bit platform?
Whether you run it on Windows, and whether the machine is 64-Bit, is
irrelevant to your problem.
(And we only deal with portable code here.)
char *new_string = (char *) malloc (strlen (old_string) + 1);
"argument' : conversion from 'size_t' to 'unsigned int', possible loss
of data" is the warning I am getting.
The signature of "malloc" should be:
void *malloc(size_t) ;
, and the signature of "strlen" should be:
size_t strlen(char const*);
Note that they both deal with "size_t" -- not "int", or "unsigned", or
"long".
In your code, the argument you supply to "malloc" is:
strlen(old_stri ng) + 1
The sub-expression on the left of the addition is a size_t. The sub-
expression on the right is a signed int. In line with integer promotion,
adding them both should result in a size_t (or the underlying unsigned type
to which size_t is congruent.)
Judging by the compiler warning, a "size_t" probably maps to a "unsigned
long" on your system (which would be larger than an "unsigned int", which
is why you're getting the truncation warning.)
In the line of code you posted, I don't see any place where an "size_t" is
converted to an "unsigned int". I can only conclude that perhaps your
implementation is defective in that it might define "malloc" as:
void *malloc(unsigne d);
You can however suppress the warning with:
malloc( (unsigned)strle n(old_string) + 1 );
--
Frederick Gotham