473,563 Members | 2,653 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

struggling to use calloc and realloc

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 #1
26 6735
In article <3f********@new s1.mweb.co.za>, dagger <fe****@mweb.co .za> 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:
[trimmed mercilessly] char *ptr;
printf( "initial size (1 char) = %d\n", sizeof(ptr) );
printf( "new size (10 chars) = %d\n", sizeof(ptr) ); 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?


sizeof(ptr) gives you the size of the pointer (and getting 4 for that
means you're running the program on a machine where pointers are 4 bytes
wide, which in this day and age suggests a desktop machine running a
32-bit operating system).
There's no way to get back the size of the memory a pointer points to;
"sizeof *ptr" will give the size of one of whatever type ptr points at,
but (especially for memory obtained from malloc and its friends) your
code needs to keep track of how many of those it has space for itself.
dave

--
Dave Vandervies dj******@csclub .uwaterloo.ca
Yes, I suppose it could if it really wanted. I stand corrected. Of course,
I'd question such a compiler's morals and drinking habits...
--Richard Heathfield in comp.lang.c
Nov 14 '05 #2
"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.
int main() {

char *ptr;

ptr = (char *) calloc(1, sizeof(char));
Ah, you didn't get a warning because of the cast, don't cast the return
value of the alloc functions.
printf( "initial size (1 char) = %d\n", sizeof(ptr) );
sizeof(ptr) is the same as sizeof(char *), on your system sizeof(char *) is
4 bytes.
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?


sizeof gives the size of the variable itself, not the size of its value.

You wouldn't expect this either.

int x = 100;
if (sizeof x == 100) false ot true?
Nov 14 '05 #3
dagger <fe****@mweb.co .za> spoke thus:
#include <stdio.h>
int main() {
char *ptr;
ptr = (char *) calloc(1, sizeof(char)); ^^^^^^^^
The cast isn't needed, and will hide helpful compiler warnings should
you have the misfortune to forget to include stdio.h. You should
check that the allocation succeeded before continuing. Why not just
use malloc()? And you might want to do

malloc( sizeof(*ptr) );
printf( "initial size (1 char) = %d\n", sizeof(ptr) );
ptr is a pointer to a character. sizeof(ptr) is the space required
by such an entity; on your implementation, it happens to be four
bytes. This number has no relation to how many bytes of allocated
memory, if any, ptr points to.
ptr = (char *) realloc(ptr, sizeof(char)*10 );
Again, lose the cast. If the original allocation succeeds, but this
one fails, the memory you originally allocated is now gone forever.
Make sure you can free() what you allocated in this event:

char *ptr, *tmp;

ptr=malloc( sizeof(*ptr) );
tmp=ptr;
ptr=realloc( ptr, 10*sizeof(*ptr) );
if( !ptr ) {
free( tmp ); /* tmp pointed at the memory originally malloc()'ed,
and that memory is now freed */
/* go on with life */
}
else {
/* do what you want with ptr */
}
printf( "new size (10 chars) = %d\n", sizeof(ptr) );


Same error as above.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cybers pace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #4
Christopher Benson-Manica <at***@nospam.c yberspace.org> spoke thus:
ptr = (char *) calloc(1, sizeof(char));

The cast isn't needed, and will hide helpful compiler warnings should
you have the misfortune to forget to include stdio.h. You should

^^^^^^^

Of course, what would a post from me be like without some silly error
or other? This is, of course, meant to be stdlib.h. I need a post
preprocessor to catch these nasssties...

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cybers pace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #5
nrk
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>

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 :)


sizeof operator determines the size of the type of its operand. In this
case the type of the operand (ptr) is char *, and the size of a char * on
your machine happens to be 4, which is what's being printed. sizeof will
not tell you how many valid bytes ptr is pointing to (that is, it won't
tell you what was allocated using calloc/malloc/realloc). This information
cannot be extracted from the pointer. You need to keep track of the
allocated size in a separate variable.

-nrk.
Nov 14 '05 #6
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>

int main() {

char *ptr;

ptr = (char *) calloc(1, sizeof(char));
A few notes here: first, sizeof(char) is a fancy way of writing 1.
Second, calloc() is not a useful as it seems. Strictly speaking, it may
not give the objects you allocate the value you expect. This is
particularly true for floating point and pointer types. I need to check
the standard to be sure, but I've been told that all-bits-zero is not
guaranteed to be the representation of 0 for any type other than the
character types.

Finally, casting the return from malloc & friends is not recommended,
and your example seems to demonstrate one of the reasons why. You forgot
to #include <stdlib.h>, which means that there is no prototype in scope
for calloc. This in turn means that the compiler assumed (incorrectly) a
return type of int. ints and pointers may not be returned the same way,
so the pointer that calloc returned might be lost, and ptr might be
assigned garbage. Without the cast, the compiler would have been
required to warn you about the conversion from int to a pointer. With
the cast, there is no such guarantee.

Consider using the comp.lang.c-approved idiom for calling malloc & friends:

ptr = calloc(1, sizeof *ptr);

This has number of advantages. The most important are that it doesn't
mask the error mentioned above, it's largely self-maintaining if the
type of ptr changes (for example, of you later choose to support
internationaliz ation using the type wchar_t instead of char), and it's
more difficult to screw up than other idioms.

printf( "initial size (1 char) = %d\n", sizeof(ptr) );
There are at least 2 apparent problems here. First, you seem to be
expecting sizeof to tell you how many characters you've allocated. It
cannot do that. sizeof is purely a compile-time construct. Here it will
give the size of the pointer ptr. If you want the size of a dynamically
allocated object, you need to keep track of it yourself.

The other problem is that you are using the wrong format specifier. %d
can never be an appropriate format specifier for type size_t. %d is ONLY
for type (signed) int. size_t cannot be an alias for (signed) int
because size_t must be unsigned.

Keep in mind that printf() is stupid. It can't determine the types of
the objects you pass to it. It relies completely on you telling it what
the types are. If you lie, all bets are off. Stack corruption leading to
a program crash or worse is a likely result. Always double-check your
format strings.

For size_t, you can cast to unsigned long and print using %lu. This is
mostly safe, but could potentially give incorrect results in C99, if the
size_t value exceeds the range of unsigned long.

ptr = (char *) realloc(ptr, sizeof(char)*10 );
This is not a safe way of using realloc. If it fails and returns NULL,
you leak whatever memory you had already allocated. To realloc safely,
store the result into a temporary pointer, then assign into your
original pointer only if the result is non-NULL.

Also, all the notes about calloc apply here as well (except that realloc
doesn't initialize the bytes to all-bits-0).

printf( "new size (10 chars) = %d\n", sizeof(ptr) );
Same problems as the previous printf().

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?


Yes. You are printing the sizeof a pointer, not a dynamically allocated
object. C provides no way of determining the size of a dynamically
allocated object, post-allocation.

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.
Nov 14 '05 #7
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?

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cybers pace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #8
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. :/

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.
Nov 14 '05 #9
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.

Alex
Nov 14 '05 #10

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

Similar topics

43
3370
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!
29
40354
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));
16
8961
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.
14
6489
by: Rahul Gandhi | last post by:
Which one is more fast? malloc followed by memset or calloc
7
2293
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...
6
10679
by: Ramasubramanian XR (AS/EAB) | last post by:
What is diff b/w malloc,calloc,realloc could any one explain
21
1920
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
2
5475
by: rasmidas | last post by:
I have a function, int dmgDocExplodeInception ( sENTITY* Entity, sDOC_SOURCE* DocSource, sUINT Mode ) {
5
2204
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 -...
0
7583
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...
0
7885
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7638
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...
0
7948
tracyyun
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...
0
6250
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...
0
5213
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...
0
3626
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2082
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
1
1198
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.