473,503 Members | 2,150 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 6730
In article <3f********@news1.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)cyberspace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #4
Christopher Benson-Manica <at***@nospam.cyberspace.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)cyberspace.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
internationalization 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*********************@neverbox.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)cyberspace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #8
Christopher Benson-Manica wrote:
Kevin Goodsell <us*********************@neverbox.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.cyberspace.org> wrote:
Kevin Goodsell <us*********************@neverbox.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
Alex <al*******@hotmail.com> wrote:
Christopher Benson-Manica <at***@nospam.cyberspace.org> wrote:
Kevin Goodsell <us*********************@neverbox.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
Nov 14 '05 #11
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.powernet.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
Nov 14 '05 #12
Alex wrote:
Alex <al*******@hotmail.com> wrote:
Christopher Benson-Manica <at***@nospam.cyberspace.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 misunderstanding, 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.
Nov 14 '05 #13
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

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

Nov 14 '05 #15
In article <br**********@sparta.btinternet.com>,
do******@address.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
Nov 14 '05 #16
Kevin Goodsell <us*********************@neverbox.com> wrote:
Christopher Benson-Manica wrote:
Kevin Goodsell <us*********************@neverbox.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*******@freenet.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
Nov 14 '05 #17
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.
Nov 14 '05 #18
Randy Howard wrote:
In article <br**********@sparta.btinternet.com>,
do******@address.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.
Nov 14 '05 #19
Kevin Goodsell <us*********************@neverbox.com> writes:
Randy Howard wrote:
In article <br**********@sparta.btinternet.com>,
do******@address.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_Keith) 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)
Nov 14 '05 #20
Martin Ambuhl <ma*****@earthlink.net> writes:
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);


Typo alert: This last should be

ptr = malloc(sizeof *ptr);

--
Keith Thompson (The_Other_Keith) 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)
Nov 14 '05 #21
In article <ln************@nuthaus.mib.org>,
Keith Thompson <ks***@mib.org> wrote:
Martin Ambuhl <ma*****@earthlink.net> writes:

ptr = malloc(*ptr);


Typo alert: This last should be

ptr = malloc(sizeof *ptr);


int i=sizeof i;
int *ptr=&i;
ptr=malloc(*ptr);
dave

--
Dave Vandervies dj******@csclub.uwaterloo.ca
That amuses the warped minds of some people here, but will be little
help to you.
--Francis Glassborow in comp.lang.c
Nov 14 '05 #22
Kevin Goodsell <us*********************@neverbox.com> wrote:
Irrwahn Grausewitz wrote:
I recall there is this Defect Report #263 filed: <snippage> 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.


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.


Sorry, I should have mentioned that the status of DR#263 is
"Closed, 2002-04-18" on this page:
http://anubis.dkuug.dk/jtc1/sc22/wg1...cs/summary.htm

IIRC from a recent thread on c.s.c, this means it is approved,
but it could theoretically be reopened for review. However,
most likely, it will be published in the next Technical
Corrigendum in it's current form (re-quoted above).

Regards
--
Irrwahn Grausewitz (ir*******@freenet.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
Nov 14 '05 #23
In article <3f********@news1.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:

#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?


How big is a pointer to one byte, and how big is a pointer to ten bytes?

If you don't understand the question: How much costs your car, how much
costs the license plate of your car, and how much costs a piece of paper
with the number of the license plate of your car on it? Do they all cost
the same? Why not?
Nov 14 '05 #24
thank you everyone for the help. I don't think I'll be struggling with this
again soon after all your helpful feedback.

Thanks you :-)

"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>

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 #25
Kevin Goodsell <us*********************@neverbox.com> wrote:
Alex wrote:
Alex <al*******@hotmail.com> wrote:
Christopher Benson-Manica <at***@nospam.cyberspace.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 misunderstanding, 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.


The standard does not explicitly guarantee this, but any
implementation that would generate a trap representation for
all-bits-zero would never see the light of day, if simply
for the common practice of:

int array[2];
memset(array, 0, sizeof array);

Alex
Nov 14 '05 #26
Alex wrote:
Kevin Goodsell <us*********************@neverbox.com> wrote:

You seem to be implying that all-bits-0 may not be a trap representation
for any integer type. If I am misunderstanding, 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.

The standard does not explicitly guarantee this, but any
implementation that would generate a trap representation for
all-bits-zero would never see the light of day, if simply
for the common practice of:

int array[2];
memset(array, 0, sizeof array);


Yes, that does seem likely. But this being comp.lang.c we tend to
consider the letter of the standard before practical concerns. In any
case, it appears this is considered a defect now, and all-bits-zero will
be guaranteed to be a valid representation of the value 0 for all
integer types soon.

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.
Nov 14 '05 #27

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

Similar topics

43
3353
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
40338
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,...
16
8957
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...
14
6481
by: Rahul Gandhi | last post by:
Which one is more fast? malloc followed by memset or calloc
7
2286
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...
6
10668
by: Ramasubramanian XR (AS/EAB) | last post by:
What is diff b/w malloc,calloc,realloc could any one explain
21
1915
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
5463
by: rasmidas | last post by:
I have a function, int dmgDocExplodeInception ( sENTITY* Entity, sDOC_SOURCE* DocSource, sUINT Mode ) {
5
2200
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...
0
7205
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,...
0
7287
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,...
0
7349
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7008
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...
0
5594
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,...
0
4688
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...
0
3177
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...
0
1521
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 ...
1
746
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.