pass-by-reference

Swift 3 - Pass struct by reference via UnsafeMutableRawPointer?

孤人 提交于 2020-01-04 07:51:49
问题 In the Core Audio -Framework user data can be passed into callbacks via an UnsafeMutableRawPointer? . I was wondering how to pass a struct by reference via this UnsafeMutableRawPointer? . Changes made inside the callback should be reflected outside the callback. I set up a playground to test this: struct TestStruct { var prop1: UInt32 var prop2: Float64 var prop3: Bool } func printTestStruct(prefix: String, data: TestStruct) { print("\(prefix): prop1: \(data.prop1), prop2: \(data.prop2),

Swift 3 - Pass struct by reference via UnsafeMutableRawPointer?

房东的猫 提交于 2020-01-04 07:51:02
问题 In the Core Audio -Framework user data can be passed into callbacks via an UnsafeMutableRawPointer? . I was wondering how to pass a struct by reference via this UnsafeMutableRawPointer? . Changes made inside the callback should be reflected outside the callback. I set up a playground to test this: struct TestStruct { var prop1: UInt32 var prop2: Float64 var prop3: Bool } func printTestStruct(prefix: String, data: TestStruct) { print("\(prefix): prop1: \(data.prop1), prop2: \(data.prop2),

Python multiprocessing: object passed by value?

狂风中的少年 提交于 2020-01-04 06:04:49
问题 I have been trying the following: from multiprocessing import Pool def f(some_list): some_list.append(4) print 'Child process: new list = ' + str(some_list) return True if __name__ == '__main__': my_list = [1, 2, 3] pool = Pool(processes=4) result = pool.apply_async(f, [my_list]) result.get() print 'Parent process: new list = ' + str(my_list) What I get is: Child process: new list = [1, 2, 3, 4] Parent process: new list = [1, 2, 3] So, it means that the my_list was passed by value since it

Python multiprocessing: object passed by value?

|▌冷眼眸甩不掉的悲伤 提交于 2020-01-04 06:04:07
问题 I have been trying the following: from multiprocessing import Pool def f(some_list): some_list.append(4) print 'Child process: new list = ' + str(some_list) return True if __name__ == '__main__': my_list = [1, 2, 3] pool = Pool(processes=4) result = pool.apply_async(f, [my_list]) result.get() print 'Parent process: new list = ' + str(my_list) What I get is: Child process: new list = [1, 2, 3, 4] Parent process: new list = [1, 2, 3] So, it means that the my_list was passed by value since it

Passing/Returning references to object + changing object is not working

|▌冷眼眸甩不掉的悲伤 提交于 2020-01-04 05:17:09
问题 I am using an answer from How to get random value out of an array to write a function that returns a random item from the array. I modified it to pass by reference and return a reference. Unfortunately, it doesn't appear to work. Any modifications to the returned object do not persist. What am I doing wrong? I'm on PHP 5.4 if that makes a difference (Don't ask). function &random_value(&$array, $default=null) { $k = mt_rand(0, count($array) - 1); $return = isset($array[$k])? $array[$k]:

How can I use array-references inside arrays in PHP?

风流意气都作罢 提交于 2020-01-03 19:37:15
问题 I want to be able to do the following: $normal_array = array(); $array_of_arrayrefs = array( &$normal_array ); // Here I want to access the $normal_array reference **as a reference**, // but that doesn't work obviously. How to do it? end( $array_of_arrayrefs )["one"] = 1; // choking on this one print $normal_array["one"]; // should output 1 Regards /R 回答1: end() doesn't return a reference of the last value, but rather the last value itself. Here is a workaround: $normal_array = array();

Pass strings by reference in Powershell?

烈酒焚心 提交于 2020-01-03 06:48:11
问题 How can I pass strings by reference to the parent scope? This doesn't work since strings are not acceptable "values". function Submit([ref]$firstName){ $firstName.value = $txtFirstName.Text } $firstName = $null Submit([ref]$firstName) $firstName Error: "Property 'value' cannot be found on this object; make sure it exists and is settable" Doing this doesn't give an error but it doesn't change the variable either: $firstName = "nothing" function Submit([ref]$firstName){ $firstName =

Unexpected behaviour with PHP array references

核能气质少年 提交于 2020-01-03 06:01:08
问题 I'm using references to alter an array: foreach($uNewAppointments as &$newAppointment) { foreach($appointments as &$appointment) { if($appointment == $newAppointment){ $appointment['index'] = $counter; } } $newAppointment['index'] = $counter; $newAppointments[$counter] = $newAppointment; $counter++; } If I print the array contents, then I receive the expected result. When I iterate over it, all elements seem to be the same (the first). When I remove the reference operator & in the inner array

PHP's current() & key() functions; inconsistent with function signature

坚强是说给别人听的谎言 提交于 2020-01-02 05:02:16
问题 I've noticed that PHP's current() and key() array functions ( like other array-pointer functions ) take the array argument by reference: mixed current ( array &$array ) Every array has an internal pointer to its "current" element, which is initialized to the first element inserted into the array. After a few quick checks, it appears that both current() and key() ( unlike other array-pointer functions ) accept the array argument by value, thereby not throwing an error when passed the return

Pass std algos predicates by reference in C++

…衆ロ難τιáo~ 提交于 2020-01-02 01:24:08
问题 I am trying to remove elements from a std::list and keep some stats of deleted elements. In order to do so, I use the remove_if function from the list, and I have a predicate. I would like to use this predicate to gather statistics. Here is the code for the predicate: class TestPredicate { private: int limit_; public: int sum; int count; TestPredicate(int limit) : limit_(limit), sum(0), count(0) {} bool operator() (int value) { if (value >= limit_) { sum += value; ++count; // Part where I