about summary refs log tree commit diff
path: root/src/librustrt
diff options
context:
space:
mode:
authorAlex Crichton <alex@alexcrichton.com>2014-09-19 10:00:35 -0700
committerAlex Crichton <alex@alexcrichton.com>2014-09-19 10:00:35 -0700
commita116c7272bb2fb81ad9ae54fb229eb9809b152b3 (patch)
tree7bbcd76ccfec1a110d422a0a1f12c9415f9b8204 /src/librustrt
parentdbaa9300acb9a43a70f1aeca17b074a34e6275b8 (diff)
parent50d179d0016ea5f3a3c820a45cdaa6921f9fb14c (diff)
downloadrust-a116c7272bb2fb81ad9ae54fb229eb9809b152b3.tar.gz
rust-a116c7272bb2fb81ad9ae54fb229eb9809b152b3.zip
rollup merge of #17355 : gamazeps/issue17210
Diffstat (limited to 'src/librustrt')
-rw-r--r--src/librustrt/c_str.rs17
1 files changed, 13 insertions, 4 deletions
diff --git a/src/librustrt/c_str.rs b/src/librustrt/c_str.rs
index 5e0004f2a2a..04a4e96ecc4 100644
--- a/src/librustrt/c_str.rs
+++ b/src/librustrt/c_str.rs
@@ -36,6 +36,12 @@ not tied to the lifetime of the original string/data buffer). If C strings are
 heavily used in applications, then caching may be advisable to prevent
 unnecessary amounts of allocations.
 
+Be carefull to remember that the memory is managed by C allocator API and not
+by Rust allocator API.
+That means that the CString pointers should be freed with C allocator API
+if you intend to do that on your own, as the behaviour if you free them with
+Rust's allocator API is not well defined
+
 An example of creating and using a C string would be:
 
 ```rust
@@ -91,8 +97,8 @@ pub struct CString {
 
 impl Clone for CString {
     /// Clone this CString into a new, uniquely owned CString. For safety
-    /// reasons, this is always a deep clone, rather than the usual shallow
-    /// clone.
+    /// reasons, this is always a deep clone with the memory allocated
+    /// with C's allocator API, rather than the usual shallow clone.
     fn clone(&self) -> CString {
         let len = self.len() + 1;
         let buf = unsafe { malloc_raw(len) } as *mut libc::c_char;
@@ -131,7 +137,9 @@ impl<S: hash::Writer> hash::Hash<S> for CString {
 }
 
 impl CString {
-    /// Create a C String from a pointer.
+    /// Create a C String from a pointer, with memory managed by C's allocator
+    /// API, so avoid calling it with a pointer to memory managed by Rust's
+    /// allocator API, as the behaviour would not be well defined.
     ///
     ///# Failure
     ///
@@ -265,7 +273,8 @@ impl CString {
     /// forgotten, meaning that the backing allocation of this
     /// `CString` is not automatically freed if it owns the
     /// allocation. In this case, a user of `.unwrap()` should ensure
-    /// the allocation is freed, to avoid leaking memory.
+    /// the allocation is freed, to avoid leaking memory. You should
+    /// use libc's memory allocator in this case.
     ///
     /// Prefer `.as_ptr()` when just retrieving a pointer to the
     /// string data, as that does not relinquish ownership.