unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: Ken Raeburn <raeburn@raeburn.org>
To: Qiang Guo <qguo@ualberta.ca>
Cc: emacs-devel@gnu.org
Subject: Re: Quesition about Lisp_Vector
Date: Fri, 11 Nov 2011 04:01:27 -0500	[thread overview]
Message-ID: <553A1AC5-4002-4175-AD1F-CC596356E44C@raeburn.org> (raw)
In-Reply-To: <m21utfrx37.fsf@ualberta.ca>

On Nov 11, 2011, at 01:29, Qiang Guo wrote:
> Hi, I'm new to devel aspect of Emacs. And I'm having a problem reading
> Emacs source code: in the definition of Lisp_Vector
> struct Lisp_Vector
>  {
>    EMACS_UINT size;
>    struct Lisp_Vector *next;
>    Lisp_Object contents[1];
>  };
> 
> why contents's size is fixed ? And later when actually allocating space

This is a common idiom for handling a structure with a variable-sized array at the end.  The latest spec for the C language has support built in for a slightly different mechanism, but this scheme used above has been a common way to get around the absence of variable-sized arrays in structures in traditional C for a long time, and it works on most if not all C compilers.

> 
> nbytes = sizeof *p + (len - 1) * sizeof p->contents[0];
> 
> I don't understand how this nbytes is calculated. why only (len-1)
> contents[0] ? and how can the size of an array be changed ?

The "len-1" means we want an array with "len" elements in it, but the first element is covered by "sizeof *p" because "struct Lisp_Vector" includes one array element already.  If you use an index greater than zero, the value referenced may be stored in extra padding space at the end of the structure, if there is any, or in the additional space we allocated past the end of the structure.  If you think about it, it really doesn't matter which; all that matters is that for array indexes less than "len", we've allocated at least enough extra space.

If you use a compiler or interpreter designed to run C code with extensive, very pedantic run-time checks, it *might* notice if the array index used is greater than 0 and warn you.  But under pretty much any normal compiler this should just work without any complaint.

Ken


  parent reply	other threads:[~2011-11-11  9:01 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-11-11  6:29 Quesition about Lisp_Vector Qiang Guo
2011-11-11  8:59 ` Andreas Schwab
2011-11-11  9:01 ` Ken Raeburn [this message]
2011-11-12  3:42   ` Stephen J. Turnbull
2011-11-12  8:49     ` Eli Zaretskii
2011-11-12 15:45       ` Stephen J. Turnbull
2011-11-12 15:53         ` Eli Zaretskii
2011-11-13  1:02     ` Ken Raeburn

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=553A1AC5-4002-4175-AD1F-CC596356E44C@raeburn.org \
    --to=raeburn@raeburn.org \
    --cc=emacs-devel@gnu.org \
    --cc=qguo@ualberta.ca \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).