Skip to content

Added simple example for imcompatible allocators #33

Draft
vikashsiwach wants to merge 4 commits intorustfoundation:mainfrom
vikashsiwach:add-incompatible-allocators-example
Draft

Added simple example for imcompatible allocators #33
vikashsiwach wants to merge 4 commits intorustfoundation:mainfrom
vikashsiwach:add-incompatible-allocators-example

Conversation

@vikashsiwach
Copy link
Copy Markdown
Contributor

Summary

This PR adds a minimal example demonstrating the "incompatible_allocators" problem between Rust and C++.
The example shows a case where memory is allocated in Rust and ownership is transferred to C++, which then attempts to deallocate it using a different allocator. This results in undefined behavior.

What this example demonstrates

  • Allocation of memory in Rust (using vec)
  • Passing a raw pointer and length across the FFI boundary
  • Incorrect deallocation in C++ using free()
  • Why allocator compatibility is required for soundness

Why this is useful

This example highlights that allocator compatibility is equally important.

Notes

  • The example may appear to work on some systems, but it is still undefined behavior.
  • This is intentionally incorrect code to demonstrate the problem.

Future work (optional)

This example could be extended to include:

  • A correct approach where Rust deallocates the memory
  • Variants using Box or other allocation patterns

@teor2345
Copy link
Copy Markdown
Collaborator

Hi, thanks for this example! I've already seen your first PR in #24, let's focus on one PR at a time.

Once that PR has finished, we can decide what to do with this one. I'll mark this PR as draft until then.

@teor2345 teor2345 marked this pull request as draft March 25, 2026 00:11
@vikashsiwach vikashsiwach force-pushed the add-incompatible-allocators-example branch 2 times, most recently from dbe2698 to b2b8b93 Compare March 26, 2026 21:03
@vikashsiwach vikashsiwach force-pushed the add-incompatible-allocators-example branch from b2b8b93 to 85c2b19 Compare March 26, 2026 21:05
@vikashsiwach vikashsiwach marked this pull request as ready for review March 26, 2026 21:06
@vikashsiwach
Copy link
Copy Markdown
Contributor Author

I have updated the project structure with different folder for incompatible allocators example. I have added run.sh file for CI. It is passing now all checks. @teor2345 I request you to review the PR and give your feedback.

@teor2345 teor2345 added the p-build-system Build system problems or examples label Mar 26, 2026
@teor2345
Copy link
Copy Markdown
Collaborator

Hi, thanks for this example! I've already seen your first PR in #24, let's focus on one PR at a time.

Once that PR has finished, we can decide what to do with this one. I'll mark this PR as draft until then.

Sorry, we're focusing on one PR per person for now.

@teor2345 teor2345 marked this pull request as draft March 30, 2026 01:31
@rustfoundation rustfoundation locked and limited conversation to collaborators Mar 30, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

p-build-system Build system problems or examples

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants