Hi there. I'm using C under FreeBSD with the gcc compiler and am having a
bit of trouble using the calloc and realloc calls.
As an example the code snippet:
#include <stdio.h>
int main() {
char *ptr;
ptr = (char *) calloc(1, sizeof(char));
printf( "initial size (1 char) = %d\n", sizeof(ptr) );
ptr = (char *) realloc(ptr, sizeof(char)*10 );
printf( "new size (10 chars) = %d\n", sizeof(ptr) );
return 0;
}
and yet when I run it I get a size of 4 for each printf even though
initially I allocated only the size of 1 char, and after reallocation there
should be room for 10 bytes...? Or where am I making a mistake in my
reasoning?
Thanks for any help :)
Nov 14 '05
26 6753
Alex <al*******@hotm ail.com> wrote: Christopher Benson-Manica <at***@nospam.c yberspace.org> wrote: Kevin Goodsell <us************ *********@never box.com> spoke thus:
all-bits-zero is not guaranteed to be the representation of 0 for any type other than the character types.
It may be a trap representation for integer types, yes?
Signed integer types. Yes.
No!
I should have read the question first.
All bits ONE can be a trap representation in one's complement.
Alex
dagger wrote: Hi there. I'm using C under FreeBSD with the gcc compiler and am having a bit of trouble using the calloc and realloc calls.
As an example the code snippet:
#include <stdio.h>
OH NO! You forgot to get yourself a prototype for calloc and realloc. You
can get these most simply by doing this:
#include <stdlib.h> int main() {
char *ptr;
ptr = (char *) calloc(1, sizeof(char));
If you hadn't added this cast, you would have got a very useful diagnostic.
Note that (provided you remember to #include <stdlib.h>) the above is
functionally equivalent to:
ptr = calloc(1, sizeof *ptr);
which is rather neater, easier to type, and easier to maintain.
printf( "initial size (1 char) = %d\n", sizeof(ptr) );
You are misinterpreting the result of the sizeof operator. It yields the
size of the /pointer/, not the size of the thing to which it points.
--
Richard Heathfield : bi****@eton.pow ernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
Alex wrote: Alex <al*******@hotm ail.com> wrote:
Christopher Benson-Manica <at***@nospam.c yberspace.org> wrote:
It may be a trap representation for integer types, yes?
Signed integer types. Yes.
No!
I should have read the question first.
All bits ONE can be a trap representation in one's complement.
You seem to be implying that all-bits-0 may not be a trap representation
for any integer type. If I am misunderstandin g, please clarify.
Otherwise, I'd be interested in how you justify this claim. My reading
of the standard so far strongly suggests that any non-character integer
type can have padding bits, and that those bits can be used to form a
trap representation. Furthermore, it does not specify what values these
padding bits take in any circumstance, and as far as I've seen does not
specify that all-padding-bits-0 (and all value bits "don't cares")
cannot be a trap representation.
-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.
dagger wrote: Hi there. I'm using C under FreeBSD with the gcc compiler and am having a bit of trouble using the calloc and realloc calls.
As an example the code snippet:
#include <stdio.h>
You forgot
#include <stdlib.h> int main() {
char *ptr;
ptr = (char *) calloc(1, sizeof(char));
I think you mean
ptr = calloc(1,1);
or
ptr = malloc(1);
or
ptr = calloc(1, sizeof *ptr);
or
ptr = malloc(*ptr);
Your cast serves no useful function; all it does is mask your error in
failing to #include <stdlib.h>.
sizeof(char) is 1 by definition. The 3rd and 4th options above are in case
the type of ptr should be changed later to some other kind of pointer.
Calling calloc() instead of malloc() when you don't need to initialize the
allocated array to "all bit zero" is wasteful.
Failing to check whether malloc(), calloc(), or realloc() succeeded is a
severe design error. printf( "initial size (1 char) = %d\n", sizeof(ptr) );
This tells you what the size of a pointer to char is, so should be rewritten
printf("Size of pointer to char is %u\n", (unsigned) sizeof ptr);
Since sizeof yields a size_t, an unsigned integer (not necessarily an
unsigned int), a cast should always be used unless you have the new C99
specifier modifiers "%zu" and family. It would be safer to use "%lu" with
(unsigned long) or "%llu" with (unsigned long long) rather than plain "%d"
or "%u".
ptr = (char *) realloc(ptr, sizeof(char)*10 );
See comments on the calloc() call.
printf( "new size (10 chars) = %d\n", sizeof(ptr) );
See comments on the previous printf(). You are _still_ printing the size
of a pointer to char.
return 0; }
All of this is covered in the FAQ. You should learn to check the FAQ
always before posting.
--
Martin Ambuhl
Servé Lau wrote: "dagger" <fe****@mweb.co .za> wrote in message news:3f******** @news1.mweb.co. za...
Hi there. I'm using C under FreeBSD with the gcc compiler and am having a bit of trouble using the calloc and realloc calls.
As an example the code snippet:
#include <stdio.h>
Didn't you get a warning? For realloc and calloc you need stdlib.h too.
His casting the return values from realloc and calloc would, for many
compilers, supress any warning. And make sure that his code was wrong, too.
--
Martin Ambuhl
In article <br**********@s parta.btinterne t.com>, do******@addres s.co.uk.invalid says... printf( "initial size (1 char) = %d\n", sizeof(ptr) );
You are misinterpreting the result of the sizeof operator. It yields the size of the /pointer/, not the size of the thing to which it points.
Note also that it returns a size_t, which is not guaranteed to fit in
the "%d" specified in the printf() call.
--
Randy Howard _o
2reply remove FOOBAR \<,
_______________ _______()/ ()_____________ _______________ _______________ ___
SCO Spam-magnet: po********@sco. com
Kevin Goodsell <us************ *********@never box.com> wrote: Christopher Benson-Manica wrote: Kevin Goodsell <us************ *********@never box.com> spoke thus:
all-bits-zero is not guaranteed to be the representation of 0 for any type other than the character types.
It may be a trap representation for integer types, yes?
I think I've heard conflicting reports from different experts, or possibly I've misunderstood some of what I've heard. It's one of those things I've been meaning to look up for myself, but I'm afraid it'll take me hours to find and decrypt all the relevant sections. :/
I recall there is this Defect Report #263 filed:
( http://anubis.dkuug.dk/jtc1/sc22/wg1...ocs/dr_263.htm )
[ idiomatic memset(..., 0, ...) and calloc examples ]
Suggested[1] Technical Corrigendum
Append to 6.2.6.2#5:
For any integer type, the object representation where all the
bits are zero shall be a representation of the value zero in
that type.
[1] The last subheading in this DR should read: "Proposed
Technical Corrigendum", checked with comp.std.c recently.
Regards
--
Irrwahn Grausewitz (ir*******@free net.de)
welcome to clc : http://www.angelfire.com/ms3/bchambl...me_to_clc.html
clc faq-list : http://www.eskimo.com/~scs/C-faq/top.html
acllc-c++ faq : http://www.contrib.andrew.cmu.edu/~a...acllc-c++.html
Irrwahn Grausewitz wrote: I recall there is this Defect Report #263 filed: ( http://anubis.dkuug.dk/jtc1/sc22/wg1...ocs/dr_263.htm )
[ idiomatic memset(..., 0, ...) and calloc examples ]
Suggested[1] Technical Corrigendum
Append to 6.2.6.2#5:
For any integer type, the object representation where all the bits are zero shall be a representation of the value zero in that type.
[1] The last subheading in this DR should read: "Proposed Technical Corrigendum", checked with comp.std.c recently.
So what is the status of this report? Is it accepted as a defect? I'm
not familiar with how the C committee handles defect reports, but C++
defects are usually given a status that indicates if it has been
accepted (or rejected) as a defect and what plans are in the works to
fix it. I don't see anything like that on the page you linked.
-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.
Randy Howard wrote: In article <br**********@s parta.btinterne t.com>, do******@addres s.co.uk.invalid says...
printf( "initial size (1 char) = %d\n", sizeof(ptr) );
You are misinterpreting the result of the sizeof operator. It yields the size of the /pointer/, not the size of the thing to which it points.
Note also that it returns a size_t, which is not guaranteed to fit in the "%d" specified in the printf() call.
size_t is guaranteed NOT to fit %d... unless size_t happens to promote
to int, which is probably unlikely.
-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.
Kevin Goodsell <us************ *********@never box.com> writes: Randy Howard wrote: In article <br**********@s parta.btinterne t.com>, do******@addres s.co.uk.invalid says...
printf( "initial size (1 char) = %d\n", sizeof(ptr) );
You are misinterpreting the result of the sizeof operator. It yields the size of the /pointer/, not the size of the thing to which it points.
Note also that it returns a size_t, which is not guaranteed to fit in the "%d" specified in the printf() call.
size_t is guaranteed NOT to fit %d... unless size_t happens to promote to int, which is probably unlikely.
It depends on what you mean by "fit". On many systems, size_t happens
to be the same size as int (say, 32 bits), and the following:
printf("sizeof( foo) = %d\n", sizeof(foo));
is likely to work as expected as long as sizeof(foo) doesn't exceed
INT_MAX (assuming an appropriate declaration of foo, of course).
Having said that, it's still undefined behavior; working as expected
is just one of the infinitely many possible results of undefined
behavior. To avoid undefined behavior, you can do one of the
following:
/* Approach 1 */
printf("sizeof( foo) = %d\n", (int)sizeof(foo ));
which will work if you happen to know that sizeof(foo) will never
exceed INT_MAX; or
/* Approach 2 */
printf("sizeof( foo) = %lu\n", (unsigned long)sizeof(foo ));
which is guaranteed to work in C90, but could conceivably fail in C99
if size_t is bigger than unsigned long and sizeof(foo) > ULONG_MAX; or
/* Approach 3 */
printf("sizeof( foo) = %zu\n", sizeof(foo));
which will work in C99 (more precisely, if your C library's printf
implementation supports the 'z' length modifier), but not in C90.
In general, approach 2 is better than approach 1, and is unlikely to
fail in practice.
--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"
(Note new e-mail address) This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: M-One |
last post by:
See subject: how do I calloc (and free the memory, if that's not
free(my_bytes);) this?
TIA!
|
by: David Hill |
last post by:
Is there a difference between:
/* code 1 */
struct sample test;
test = malloc(sizeof(struct sample));
memset(&test, 0, sizeof(test));
/* code 2 */
struct sample test;
test = calloc(1, sizeof(struct sample));
|
by: laberth |
last post by:
I've got a segmentation fault on a calloc and I don'tunderstand why?
Here is what I use :
typedef struct noeud {
int val;
struct noeud *fgauche;
struct noeud *fdroit;
} *arbre; //for those who don't speak french arbre means tree.
|
by: Rahul Gandhi |
last post by:
Which one is more fast?
malloc followed by memset
or
calloc
|
by: pervinder |
last post by:
Hi,
I have a c applicaiton which uses calloc to allocate the storage from
heap.
A page is allocated (4096bytes) and then its used in smal small chunks
on need.
It works fine till some n number of pages are alloacted and used. But
it fails
to allocate a fresh page of 4096 after some m times when the prev.
block gets exhausted and new block needs to be allocated/reserved.
| |
by: Ramasubramanian XR (AS/EAB) |
last post by:
What is diff b/w malloc,calloc,realloc
could any one explain
|
by: Michael McGarry |
last post by:
Hi,
What would cause calloc() to return a NULL pointer?
Is the system simply out of memory?
Regards,
Michael
|
by: rasmidas |
last post by:
I have a function,
int dmgDocExplodeInception
(
sENTITY* Entity,
sDOC_SOURCE* DocSource,
sUINT Mode
)
{
|
by: reachanil |
last post by:
Hi,
We've interposed malloc/calloc/realloc/memalign/valloc in our
application. While all of our application calls our own implementation
of these functions, there seems to be some issue with the dl.so shared
library (Windriver, ppc, linux 32 bit). The dl.so library defines a
function called - _dl_tls_setup and another one - _dl_deallocate_tls.
When our application that linked in -ldl and -lpthread, was shutting
down, we saw a crash with...
|
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,...
|
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,...
| |
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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...
|
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...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |