473,770 Members | 4,552 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

malloc in 64-bit

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

Aug 8 '06 #1
4 9792
Gon wrote:
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_stri ng) + 1];

? Always prefer 'new' to 'malloc'.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 8 '06 #2
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
Aug 8 '06 #3
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
Frederick Gotham wrote:
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
Aug 10 '06 #4
Gon wrote:

(post reordered - please don't top-post)
char *new_string = (char *) malloc (strlen (old_string) + 1);
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

Aug 11 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

25
5077
by: H.A. Sujith | last post by:
If malloc fails what should I do? 1. Exit imediately. 2. Print an error message (or put a log entry) and exit. 3. Print an error message (or put a log entry) and continue execution (after possibly recovering from the error). Printing an error message might be difficult in a graphical environment. --
18
3318
by: cs | last post by:
This is the function malloc_m() that should be like malloc() but it should find memory leak, and over bound writing in arrays. How many errors do you see? Thank you ******************************************** /* mallocm.c */ /* funzione di libreria per trattamento della memoria e rilevamento errori */ /* uso: c_compiler malloc.c e usare malloc .obj */
68
15712
by: James Dow Allen | last post by:
The gcc compiler treats malloc() specially! I have no particular question, but it might be fun to hear from anyone who knows about gcc's special behavior. Some may find this post interesting; some may find it off-topic or confusing. Disclaimers at end. The code samples are intended to be nearly minimal demonstrations. They are *not* related to any actual application code.
6
5974
by: mdrons | last post by:
I have a structure defined as: struct channel { int chanid; int channum; char callsign; char name; }; I then have a global variable defined as: static struct channel *chan=NULL;
12
818
by: Yevgen Muntyan | last post by:
Hey, Consider the following code: #include <stdlib.h> #define MAGIC_NUMBER 64 void *my_malloc (size_t n) { char *result = malloc (n + MAGIC_NUMBER);
82
31154
by: quiberon2 | last post by:
Hi, Sorry if it might be a stupid question but what should returns malloc(0) ? void *ptr = malloc(0); I am running gcc 3.3.5 and a non-null address is returned. ( in the compiler that I am currently implementing, NULL is returned. Is it wrong ?)
21
5433
by: Anton Dec | last post by:
Just curious about this... malloc is defined in stdlib.h, right? But if I write a program without #include<stdlib.hand use malloc, it still works as expected. Why is this? Is malloc automatically linked from somewhere else magically?
34
13409
by: niranjan.singh | last post by:
This is regarding to test an SDK memory stuff. In what situation malloc gets fail. any comment/reply pls.... regards
10
1911
by: somenath | last post by:
Hi All, I have one question regarding return value cast of malloc. I learned that we should not cast the return value of malloc because it is bug hider. But my question is as mentioned bellow . Lets say I have not included stdlib.h in my program still I am using malloc so compiler will throw warring because with out prototype
0
9602
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9439
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10017
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8905
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7431
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5326
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3987
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3589
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2832
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.