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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T20:38:40+00:00 2026-05-29T20:38:40+00:00

I am trying to allocate memory using sun.misc.Unsafe.allocateMemory() and access it in native code.

  • 0

I am trying to allocate memory using sun.misc.Unsafe.allocateMemory() and access it in native code.

however, it seems the long value returned by allocateMemory doesn’t quite work as the right address in C code.

            Unsafe unsafe = getUnsafe();
            long address = unsafe.allocateMemory(64);

            for (int i = 0; i < 64; i += 8)
                unsafe.putByte(memory + i, (byte) 0xFF);

            nativeMethod(address);

However in my native code when I try to access ‘address’ as a pointer, it doesn’t work 🙁

Update: I have attached an image showing the issue.
I passed the ‘address’ to the native code, however, examining the memory at that location doesnt show the 0xFF values I put in there.

Image: https://i.stack.imgur.com/KoIYG.png

enter image description here

  • 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-29T20:38:41+00:00Added an answer on May 29, 2026 at 8:38 pm

    I think it’s working perfectly fine! Your “for” loop sets every eighth byte to 0xff, and if you look closely at the display and count, you’ll see that every eighth byte is 0xff. There are some other random values in there because Unsafe. allocateMemory() doesn’t return zeroed memory; it returns uninitialized memory, just like C’s malloc().

    If you changed that “i += 8” to just “i++”, then every byte would be 0xff; it’d be worth doing that experiment to prove to yourself that it’s working.

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

Sidebar

Related Questions

i'm trying to declare and allocate memory for two 2d-arrays. However when trying to
i am trying to allocate shared memory by using a constant parameter but getting
i am trying to allocate memory for a 2 dimensional array using c and
I am trying to allocate a block of memory, and store a list of
I'm trying to figure out how to allocate memory for a struct in SPARC
I'm trying to allocate some memory for a char* as follows. static ssize_t memo_write(struct
When trying to get shared memory, shmget() often fails because being unable to allocate
I am trying to find a way to allocate memory dynamically for memory mapped
I am trying to mmap a binary file (~ 8Gb) using the following code
I am trying to write a crawler using simple_html_dom.php version 1.5 but it seems

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.