473,387 Members | 1,535 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

How does this 'sizeof(*area)' work?

Hi, all.

How does this 'sizeof(*area)' work? I think the reason is the sentence in
C99: If the operand has type ¡®¡®pointer to type¡¯¡¯, the result has type
¡®¡®type¡¯¡¯, but I'm not sure. Could someone ensure me or explain it more?

In linux\mm\vmalloc.c,
struct vm_struct * get_vm_area(unsigned long size, unsigned long flags)
{
unsigned long addr;
struct vm_struct **p, *tmp, *area;

area = (struct vm_struct *) kmalloc(sizeof(*area), GFP_KERNEL); // I'm
wondering how this sizeof works.
if (!area)
return NULL;
...
}

According to C99:

sizeof unary-expression
sizeof ( type-name )

4 The unary * operator denotes indirection. If the operand points to a
function, the result is
a function designator; if it points to an object, the result is an lvalue
designating the
object. If the operand has type ¡®¡®pointer to type¡¯¡¯, the result has type
¡®¡®type¡¯¡¯. If an
invalid value has been assigned to the pointer, the behavior of the unary *
operator is
undefined. 72)

NOTE: Among the invalid values for dereferencing a pointer by the unary *
operator are a null pointer, an
address inappropriately aligned for the type of object pointed to, and the
address of an automatic
storage duration object when execution of the block with which the object is
associated has
terminated.

Thanks.

Best Regards,

Xiangliang Meng
Nov 14 '05 #1
3 1788
[snips]

On Thu, 22 Jul 2004 16:13:37 +0800, Xiangliang Meng wrote:
struct vm_struct **p, *tmp, *area;

area = (struct vm_struct *) kmalloc(sizeof(*area), GFP_KERNEL); // I'm


No idea what kmalloc is. However, let's make it simpler:

int *x = malloc( sizeof(*x) );

X is a pointer-to-int. Hence, the object(s) it points to are of type int.
Hence, *x must be an int, hence sizeof( *x ) must mean sizeof(int).

This also means that things such as x[3] = 19; will work correctly, since
x, being a pointer-to-int, will be indexed in such a manner that each
element is one int wide.

Why would one do this, instead of using sizeof(int)? Simple: suppose we
change our code:

double *x = malloc( sizeof(*x) );

The sizeof automagically adapts to allocating space for the new type. If
we'd used explicit types instead of *x (eg sizeof(int)), it makes it
easier to overlook the second item and produce incorrect code such as:

double *x = malloc( sizeof(int) );
Nov 14 '05 #2
Kelsey Bjarnason <ke*****@xxnospamyy.lightspeed.bc.ca> writes:
[snips]

On Thu, 22 Jul 2004 16:13:37 +0800, Xiangliang Meng wrote:
struct vm_struct **p, *tmp, *area;

area = (struct vm_struct *) kmalloc(sizeof(*area), GFP_KERNEL); // I'm


No idea what kmalloc is. However, let's make it simpler:

int *x = malloc( sizeof(*x) );

X is a pointer-to-int. Hence, the object(s) it points to are of type int.
Hence, *x must be an int, hence sizeof( *x ) must mean sizeof(int).

This also means that things such as x[3] = 19; will work correctly, since
x, being a pointer-to-int, will be indexed in such a manner that each
element is one int wide.

Why would one do this, instead of using sizeof(int)? Simple: suppose we
change our code:

double *x = malloc( sizeof(*x) );

The sizeof automagically adapts to allocating space for the new type. If
we'd used explicit types instead of *x (eg sizeof(int)), it makes it
easier to overlook the second item and produce incorrect code such as:

double *x = malloc( sizeof(int) );


Perhaps the piece of the puzzle that the OP missed is that the operand
of sizeof is not evaluated. (There is an exception to this, but it
involves C99 VLAs, which aren't relevant to this example.) Looking at
your simple example again:

int *x = malloc(sizeof(*x));

Normally, any use of *x in the initialization of x would be Very Bad,
because x hasn't been initialized yet. But in the context of a sizeof
operator, the expression is examined only to determine its type.

BTW, the inner parentheses aren't strictly necessary, though they're
allowed. The above is equivalent to:

int *x = malloc(sizeof *x);

Some might prefer to use the parentheses because the latter form looks
too much like a multiplication. Use whichever form you like, but be
prepared to understand either one.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #3
[snips]

On Thu, 22 Jul 2004 23:02:51 +0000, Keith Thompson wrote:
Perhaps the piece of the puzzle that the OP missed is that the operand
of sizeof is not evaluated.
....
BTW, the inner parentheses aren't strictly necessary, though they're
allowed. The above is equivalent to:


Indeed, on both points. I tend to use the inner parentheses for what I
regard as clarity; YMMV.
Nov 14 '05 #4

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

Similar topics

5
by: Peter | last post by:
L.S. I am developing a PHP-login script (on Lycos Tripod) that uses Session to pass on variables. Below is the entire (stripped) structure that I use. It opens a page where you can Set and Read...
3
by: cv | last post by:
Hello All, I have used MultipartRequest like the following to upload images. MultipartRequest multi = new MultipartRequest(request, "../webapps/coreprogram/dealerlogos", 1024 * 1024); It...
4
by: Field | last post by:
Hi, the following snippet shows once executed this output: 2 2 I'd have rather expected this output: 2 10
4
by: Das | last post by:
Hi, I have made an application in ASP.net with C#. The application works fine with localhost. I have uploaded the site. I'm using web user controls in the form. but some of the button do not work...
7
by: Tom | last post by:
Hi Is this a conditional ? what is the structure of the statement? ch Tom
10
by: Antoine | last post by:
I can't work out what is causing this problem. Can anyone suggest what the typical causes beyond the obvious might be? Could you get it with datasets? Maybe I should run in debug mode and test...
5
by: Jason | last post by:
I've got a small form (400 X 310) and I want it to start up in the lower right corner of the screen. Is there a way I can easily do this? Thanks
1
by: Newbie in ChiTown | last post by:
Here's my code: I am using MS Access and I am trying to update a table (InvoiceDetails) with data input by the user on a form. However, it does not update nor does it give me an error message. ...
11
by: Jim | last post by:
Hi, I want to schedule a Python program that reads the command line for input. However, when adding an argument to the command line Python will not pick it up when using Windows scheduled...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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
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
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...

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.