Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed1.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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'subject:: [': 0.04; 'subject:Python': 0.06; 'statically': 0.07; '[],': 0.09; 'correspond': 0.09; 'lookup': 0.09; 'pretend': 0.09; 'subject:into': 0.09; 'cc:addr:python-list': 0.11; 'itself.': 0.14; "wouldn't": 0.14; 'block.': 0.16; 'brackets': 0.16; 'declared': 0.16; 'elsewhere,': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'invisible': 0.16; 'malloc': 0.16; 'sizeof': 0.16; 'subject:variable': 0.16; 'wrote:': 0.18; 'subject:] ': 0.20; 'memory': 0.22; 'cc:addr:python.org': 0.22; 'pointer': 0.24; 'earlier': 0.24; 'mon,': 0.24; 'cc:2**0': 0.24; 'this:': 0.26; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'am,': 0.29; 'array': 0.29; 'message-id:@mail.gmail.com': 0.30; 'block,': 0.31; 'struct': 0.31; 'table,': 0.31; 'bugs': 0.33; 'implemented': 0.33; 'table': 0.34; 'could': 0.34; 'something': 0.35; 'case,': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'c++': 0.36; 'subject:?': 0.36; 'easily': 0.37; 'delete': 0.39; 'catch': 0.60; 'dave': 0.60; 'subject:Can': 0.60; 'new': 0.61; 'kind': 0.63; 'choose': 0.64; 'mar': 0.68; 'compliant': 0.68; "'new'": 0.84; 'angel': 0.91; 'to:none': 0.92 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=yWNOvryCjjTPUsZrZanfZaKlm7DAXyT8iJe3kweb6l8=; b=NTXGiXgqmp+53szANUyAW8ABwwBzViyF29jXzF/QZyYvDCat6rmn0GZYh+lTzXQ36e 02uDjNIVnbTNYql55Lmxbjhqu0H2dhV8l3+3W7TPxSzMJNYU8iBc2GNYuQ2ukpcl/ovg siAsyGjONUPj9s8NhwhHEOL0Xp8mfDjQkUJctxP2aHtU8kI6pz4ssp5oE1W6gccqIluN XGUkIBvvxwMRov0TBAE7AR3K+SS9UlM4kEyXg0fRNItNv2NpOavhQ9k+zxGnYObnHKhp IiY4y5sJ3+uYJmSVyaeQsfgLLD0nYDjdSQyNKUfg7tzZRbDn7DYYQzBGurX6UwBHu5q0 bsUQ== MIME-Version: 1.0 X-Received: by 10.68.178.66 with SMTP id cw2mr15255135pbc.89.1393788748245; Sun, 02 Mar 2014 11:32:28 -0800 (PST) In-Reply-To: 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> Date: Mon, 3 Mar 2014 06:32:28 +1100 Subject: Re: [OT] Can global variable be passed into Python function? From: Chris Angelico Cc: "python-list@python.org" Content-Type: text/plain; charset=UTF-8 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: 26 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1393788751 news.xs4all.nl 2854 [2001:888:2000:d::a6]:40900 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:67463 On Mon, Mar 3, 2014 at 6:17 AM, Dave Angel wrote: > 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. As in, you have to use "delete [] x" to correspond with "x = new whatever[n]"? Yes, that's right, but that array size is earlier in memory than x itself. I can pretend that x is the same as one declared statically as "whatever x[n]", and it'll function the same way. When new[] is implemented using malloc(), it'll be something like this: { data = malloc(n * sizeof(whatever) + sizeof n); *(int *)data = n; return ((int *)data)+1; } so in that case, the array size is inside the malloc'd block, but it's still invisible to the calling function. A fully compliant C++ implementation could choose to store that elsewhere, in some kind of lookup table - it could then easily catch bugs like "delete malloc(1)", "delete [] malloc(1)", "delete [] new whatever", and "delete new whatever[1]" (because the pointer given wouldn't be in the 'new' table or the 'new[]' table, respectively). ChrisA