Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8169507
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T20:51:43+00:00 2026-06-06T20:51:43+00:00

My impression is that in NumPy, two arrays can share the same memory. Take

  • 0

My impression is that in NumPy, two arrays can share the same memory. Take the following example:

import numpy as np
a=np.arange(27)
b=a.reshape((3,3,3))
a[0]=5000
print (b[0,0,0]) #5000

#Some tests:
a.data is b.data #False
a.data == b.data #True

c=np.arange(27)
c[0]=5000
a.data == c.data #True ( Same data, not same memory storage ), False positive

So clearly b didn’t make a copy of a; it just created some new meta-data and attached it to the same memory buffer that a is using. Is there a way to check if two arrays reference the same memory buffer?

My first impression was to use a.data is b.data, but that returns false. I can do a.data == b.data which returns True, but I don’t think that checks to make sure a and b share the same memory buffer, only that the block of memory referenced by a and the one referenced by b have the same bytes.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T20:51:46+00:00Added an answer on June 6, 2026 at 8:51 pm

    I think jterrace’s answer is probably the best way to go, but here is another possibility.

    def byte_offset(a):
        """Returns a 1-d array of the byte offset of every element in `a`.
        Note that these will not in general be in order."""
        stride_offset = np.ix_(*map(range,a.shape))
        element_offset = sum(i*s for i, s in zip(stride_offset,a.strides))
        element_offset = np.asarray(element_offset).ravel()
        return np.concatenate([element_offset + x for x in range(a.itemsize)])
    
    def share_memory(a, b):
        """Returns the number of shared bytes between arrays `a` and `b`."""
        a_low, a_high = np.byte_bounds(a)
        b_low, b_high = np.byte_bounds(b)
    
        beg, end = max(a_low,b_low), min(a_high,b_high)
    
        if end - beg > 0:
            # memory overlaps
            amem = a_low + byte_offset(a)
            bmem = b_low + byte_offset(b)
    
            return np.intersect1d(amem,bmem).size
        else:
            return 0
    

    Example:

    >>> a = np.arange(10)
    >>> b = a.reshape((5,2))
    >>> c = a[::2]
    >>> d = a[1::2]
    >>> e = a[0:1]
    >>> f = a[0:1]
    >>> f = f.reshape(())
    >>> share_memory(a,b)
    80
    >>> share_memory(a,c)
    40
    >>> share_memory(a,d)
    40
    >>> share_memory(c,d)
    0
    >>> share_memory(a,e)
    8
    >>> share_memory(a,f)
    8
    

    Here is a plot showing the time for each share_memory(a,a[::2]) call as a function of the number of elements in a on my computer.

    share_memory function

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was under the impression that these days javascript can be used as a
I was under the impression that webm played on Android, but I can't get
I'm under the impression that changing an anchor tag on hover can be done
I was under the impression that argument-less functions can be called with empty parentheses
I was under impression that .replace and -replace were the exact same thing, however
I had the impression that Class.forName(String className) used the Thread.currentThread().getContextClassLoader() to load the class
I was under the impression that when we send mail to any domain (i.e.
I'm under the impression that a control in a nested UpdatePanel will cause the
I was under the impression that when you called Flush() in a StreamWriter object
I was under the impression that I could get the value of a select

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.