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 6752
Martin Ambuhl <ma*****@earthl ink.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_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)
In article <ln************ @nuthaus.mib.or g>,
Keith Thompson <ks***@mib.or g> wrote: Martin Ambuhl <ma*****@earthl ink.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
Kevin Goodsell <us************ *********@never box.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*******@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
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:
#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?
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 :)
Kevin Goodsell <us************ *********@never box.com> wrote: Alex wrote:
Alex <al*******@hotm ail.com> wrote:
Christophe r 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.
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
Alex wrote: Kevin Goodsell <us************ *********@never box.com> wrote:
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.
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. 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: 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...
|
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: 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.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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: 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: 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: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |