malloc in 64-bit

G

Gon

Hello,

Can some one help me out how to rewrite the below line to avoid
errors/warings on a windows 64-bit platform?

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.

I have many such lines in my code. I know this was poor coding but I
have got to work on this,

I am sure this must have discussed earlier. But please help me out.

Thanks in advance,
Gon
 
V

Victor Bazarov

Gon said:
Can some one help me out how to rewrite the below line to avoid
errors/warings on a windows 64-bit platform?

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.

I have many such lines in my code. I know this was poor coding but I
have got to work on this,

I am sure this must have discussed earlier. But please help me out.

Doesn't this work for you:

char *new_string = new char[strlen(old_string) + 1];

? Always prefer 'new' to 'malloc'.

V
 
F

Frederick Gotham

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_string) + 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(unsigned);

You can however suppress the warning with:

malloc( (unsigned)strlen(old_string) + 1 );
 
G

Gon

Hello,

Thanks for your attention to the post...

Interestingly...

When I run the same by creating new solution after installing VS2k5
PSDK, I do not see any such warnings. I am sure I am using a 64-bit
compiler. Here is the compiler output...

"Microsoft (R) C/C++ Optimizing Compiler Version 14.00.40310.39 for
IA-64"

I was getting said warning mesage on .net framework 1.1 with /Wp64
switch for the compiler.

~gon
 
M

Michiel.Salters

Gon wrote:

(post reordered - please don't top-post)
When I run the same by creating new solution after installing VS2k5
PSDK, I do not see any such warnings. I am sure I am using a 64-bit
compiler.

I was getting (said) warning mesage on .net framework 1.1 with /Wp64
switch for the compiler.

/Wp64 doesn't make it a 64 bit compiler. It's in fact a rather dumb
flag.
It looks at the argument types and guesses. In this case, wrongly. That
could be caused by a number of reasons. A real 64 bit standard C++
compiler doesn't guess but applies the rules. The rules say it's OK.

It's not pretty, perhaps, and rather 20th century, but that's another
story.

HTH,
Michiel Salters
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top