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 7004059
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T21:09:01+00:00 2026-05-27T21:09:01+00:00

I want to have a fix length mutable content shared data buffer and that

  • 0

I want to have a fix length mutable content shared data buffer and that is how do I create it:

void *buffer = malloc(length);
// initialize buffer content
NSData *sharedData = [[NSData alloc] initWithBytesNoCopy:buffer length:length freeWhenDone:YES]

What happen if I modify buffer after I created a NSData from it? Will NSData reflect the change I did to buffer?

I can guaranty that sharedData will not get dealloc when I want to modify buffer.

This is how I actually want to use it:


void *my_alloc(CFIndex allocSize, CFOptionFlags hint, void *info) {return NULL;}
void my_dealloc(void *ptr, void *info) {
    mach_vm_deallocate(mach_task_self(), (mach_vm_address_t)ptr, (size_t)info);
}

size_t length = //some number
mach_vm_address_t buffer;
mach_vm_allocate(mach_task_self(), &buffer, length, VM_FLAGS_ANYWHERE);
// do something to buffer, for example pass to other process using mach RPC and expect other process will modify the content
CFAllocatorContext context = {0, (void *)length, NULL, NULL, NULL, my_alloc, NULL, my_dealloc, NULL};
CFAllocatorRef allocator = CFAllocatorCreate(NULL, &context);
CFDataCreateWithBytesNoCopy(NULL, (const UInt8 *)buffer, length, allocator);
  • 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-05-27T21:09:02+00:00Added an answer on May 27, 2026 at 9:09 pm

    The initWithBytesNoCopy: will effectively create an NSData wrapper around the existing buffer; so yes, things accessing through [sharedData bytes] will see any updates you make.

    Of course, it does nothing to link other objects which are created from the NSData instance, so for instance an [NSImage initWithData:sharedData] may make a copy for the NSImage instance, which won’t reflect any changes.

    Also, with freeWhenDone:YES the NSData will destroy the buffer when the last reference is removed, so look out for that =)


    So, given that the NSData is effectively a thin wrapper around a malloc() allocation, yes it will reflect changes made to that memory (by any process); but as it will call free() on it, it’s a bad idea to use it to wrap a buffer created another way (mach_vm_allocate) with freeWhenDone:YES.

    If you don’t really-really need to use a custom allocator (why?), I think you’d be better off with:

    NSMutableData* sharedData = [NSMutableData dataWithCapacity:length];
    // `dataWithLength:` will fully allocate and zero the buffer first, if you prefer
    void* buffer = [sharedData mutableBytes];
    // do something to `buffer`, mach RPC, etc.
    // 3: profit.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have three equal-sized data frames that I want to merge in a particular
So I have this big file of fix length lines. I want to do
i have a double, the decimal place isn't fix (8-?) i want to fix
I want to have a select-only ComboBox that provides a list of items for
I want to have a text box that the user can type in that
i have Devxpress GridControl on the form, and i want to send data on
I want to copy F:\test to folder F:\123 so that I have folder F:\123\test.
I have a JTextField that I want to be limited to fifteen characters. The
Straight to the chase I want to create a function which calculates the length
I have some Visual C++ code that receives a pointer to a buffer with

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.