Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed2a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'python,': 0.02; 'subject:: [': 0.04; 'subject:Python': 0.06; 'padding': 0.07; 'puts': 0.07; '#include': 0.09; '[],': 0.09; 'bytes,': 0.09; 'definition,': 0.09; 'inherited': 0.09; 'main()': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'skip:% 20': 0.09; 'subject:into': 0.09; 'sucks': 0.09; 'sure,': 0.09; 'wrote': 0.14; '240': 0.16; 'block.': 0.16; 'brackets': 0.16; 'element.': 0.16; 'foo,': 0.16; 'hmm.': 0.16; 'malloc': 0.16; 'none),': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'sizeof(int)': 0.16; 'sizeof.': 0.16; 'structs': 0.16; 'subject:variable': 0.16; 'size,': 0.16; 'wrote:': 0.18; 'obviously': 0.18; 'starts': 0.20; 'subject:] ': 0.20; 'memory': 0.22; 'rules': 0.22; 'bytes': 0.24; 'convenient': 0.24; 'instance,': 0.24; 'pointer': 0.24; 'skip:% 10': 0.24; 'mon,': 0.24; 'versions': 0.24; 'header:X-Complaints-To:1': 0.27; 'function': 0.29; 'chris': 0.29; 'am,': 0.29; 'array': 0.29; 'room': 0.29; '(like': 0.30; 'returned': 0.30; 'specified': 0.30; '(which': 0.31; 'included': 0.31; '120': 0.31; 'aligned': 0.31; 'block,': 0.31; 'obliged': 0.31; 'overhead': 0.31; 'struct': 0.31; 'class': 0.32; 'there.': 0.32; 'beginning': 0.33; 'cases': 0.33; 'table': 0.34; 'case,': 0.35; 'definition': 0.35; 'but': 0.35; 'there': 0.35; 'subject:?': 0.36; 'virtual': 0.37; 'needed': 0.38; 'to:addr:python-list': 0.38; 'previous': 0.38; 'that,': 0.38; 'structure': 0.39; 'delete': 0.39; 'to:addr:python.org': 0.39; 'skip:p 20': 0.39; 'received:org': 0.40; 'dave': 0.60; 'subject:Can': 0.60; 'new': 0.61; 'skip:* 10': 0.61; 'first': 0.61; 'such': 0.63; 'choose': 0.64; 'effectively': 0.66; 'between': 0.67; 'mar': 0.68; 'acts': 0.74; 'eight': 0.74; 'laid': 0.84; 'presumably': 0.84; 'received:myvzw.com': 0.84; 'angel': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Dave Angel Subject: Re: [OT] Can global variable be passed into Python function? Date: Sun, 2 Mar 2014 14:17:41 -0500 (EST) Organization: news.gmane.org References: <27ac2248-0ca3-4ba6-9d25-eaad324bc5e9@googlegroups.com> <87sird7wuw.fsf@handshake.de> <8454E8CB-E6E3-452F-8E54-9A77BFF34EC2@gmail.com> <1m3gg9lbf2ln5m2kbki954t17mqni3b20k@4ax.com> <53095145$0$29985$c3e8da3$5496439d@news.astraweb.com> <877g8mcg1m.fsf@elektro.pacujo.net> <87ob1yay9m.fsf@elektro.pacujo.net> <08aa32de-cd51-4888-bd60-2c2b53d86ecc@googlegroups.com> X-Gmane-NNTP-Posting-Host: 101.sub-70-208-129.myvzw.com X-Newsreader: PiaoHong Usenet NewsReaders 1.36 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 89 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1393787621 news.xs4all.nl 2951 [2001:888:2000:d::a6]:58290 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:67460 Chris Angelico Wrote in message: > On Mon, Mar 3, 2014 at 12:22 AM, Dave Angel wrote: >> Sure, for some definition of "usable". Overhead such as block >> size, freelist pointer etc., are obviously outside of the >> returned block. But the array size that's specified in a call to >> new [], and the vptr, are definitely inside the malloc'ed block, >> and may be before the struct data. > > Hmm. Last I was working with it, the array size to new[] was outside > the block, just as the block size to malloc(). The vptr is part of any > struct/class with virtual functions, and effectively acts as a hidden > class member, so you get one of those inside the block, and it's > included in sizeof. > > //Allocated Space: The Final Frontier! > #include //cout sucks :) > > class Foo > { > int x; > int y; > int z; > }; > > class Bar > { > int x; > int y; > int z; > virtual int get_x() {return x;} > }; > > int main() > { > printf("sizeof(int) = %u\n",sizeof(int)); > printf("sizeof(int*) = %u\n",sizeof(int*)); > printf("sizeof(Foo) = %u\n",sizeof(Foo)); > printf("sizeof(Bar) = %u\n",sizeof(Bar)); > Foo *foo = new Foo[10]; > printf("foo = %p/%p = %u\n",foo,foo+10,(char *)(foo+10)-(char *)foo); > Bar *bar = new Bar[10]; > printf("bar = %p/%p = %u\n",bar,bar+10,(char *)(bar+10)-(char *)bar); > return 0; > } > > > rosuav@sikorsky:~$ g++ frontier.cpp && ./a.out > sizeof(int) = 4 > sizeof(int*) = 8 > sizeof(Foo) = 12 > sizeof(Bar) = 24 > foo = 0xf38010/0xf38088 = 120 > bar = 0xf38090/0xf38180 = 240 > > > > The rules of structs are that they be contiguous, that they be laid > out sequentially, and that any padding needed between structures is at > the end of the previous one (which is why three of 4 bytes makes 12 > bytes, but three of 4 bytes plus 8 bytes makes 24 - the eight-byte > pointer has to be aligned on a multiple of eight bytes, so having a > 20-byte structure that starts with an 8-byte pointer is a no-no). The > allocated block of memory is, by definition, the same as the pointer > to its first element. As it happens, the pointer bar is not synonymous > with &bar->x, &bar->y, or &bar->z, which means the vptr is at the > beginning of bar, which makes sense; but the compiler's not obliged to > do that, and in some cases may choose not to - for instance, if bar > (with a virtual function) inherited from foo (with none), it might be > convenient to allow a pointer-cast to not change the value of the > pointer. (g++ 4.7.2 still puts the vptr at the beginning of bar in > that case, but other compilers or other versions may differ.) > > Array size is outside the block, presumably before it, as &foo[0] is > by definition identical to foo, and there's no room inside the > structure for any spare data. Virtual function table is inside the > block because it's a hidden member of the object (like __class__ in > Python, only better hidden). > Array size is inside the malloc block, but outside the struct block. As you can see if you try to delete without the brackets when you used new [], some runtimes will crash. This is not to say that there will always be these extra offsets, just that they can be there. -- DaveA