pass-by-value

Dynamic memory access only works inside function

末鹿安然 提交于 2019-12-16 19:05:23
问题 This question is meant to be used as a canonical duplicate for this FAQ: I am allocating data dynamically inside a function and everything works well, but only inside the function where the allocation takes place. When I attempt to use the same data outside the function, I get crashes or other unexpected program behavior. Here is a MCVE: #include <stdlib.h> #include <stdio.h> void create_array (int* data, int size) { data = malloc(sizeof(*data) * size); for(int i=0; i<size; i++) { data[i] = i

C++: Passing objects by value to a member function of the same class

一曲冷凌霜 提交于 2019-12-14 03:49:43
问题 I'm a beginner in C++ and I've just started learning about OOP. In the following program I've added objects of the same classes and displayed the result. However, I'm not able to understand the fact that if I pass the objects to the function by value then how is the change reflected in the calling function. The addNumbers() function expects two objects of the class Complex and the object which is used to invoke the function ( c3.addNumbers(c1, c2) ) is implicitly passed to the function but

Using Pass by value or Pass by Reference?

自古美人都是妖i 提交于 2019-12-14 03:34:43
问题 I have two methods in my code. Below is one of them. private async void Characteristic_ValueChanged(GattCharacteristic sender, GattValueChangedEventArgs args) { var newValue = FormatValueByPresentation(args.CharacteristicValue, presentationFormat); var message = $"Value at {DateTime.Now:hh:mm:ss.FFF}: {newValue}"; await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => CharacteristicLatestValue.Text = message); } And it's printing time (Value At) like this. Now, this is the second

Confused about accessing struct members via a pointer

不羁的心 提交于 2019-12-13 15:04:42
问题 I'm new to C, and am confused by results I'm getting when referencing a member of a struct via a pointer. See the following code for an example. What's happening when I reference tst->number the first time? What fundamental thing am I missing here? #include <stdio.h> #include <stdlib.h> typedef struct { int number; } Test; Test* Test_New(Test t,int number) { t.number = number; return &t; } int main(int argc, char** argv) { Test test; Test *tst = Test_New(test,10); printf("Test.number = %d\n"

Why are all the values in my list the same? [closed]

∥☆過路亽.° 提交于 2019-12-13 10:37:22
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 6 years ago . In java, I'm trying to populate a list with a bunch of different objects. But what is happening is, each time I add another object to the list, all the objects in the list get turned into the same thing. The code in question: private static ArrayList<Card> deck = new ArrayList<Card>(); private static void

Efficiency of passing a struct to a function without instantiating a local variable

南笙酒味 提交于 2019-12-13 07:09:20
问题 I recently learned that I can do the following with passing a a struct to a function in C++: (My apologies for not using a more appropriate name for this "feature" in the title, feel free to correct me) #include <iostream> typedef struct mystruct{ int data1; int data2; } MYSTRUCT; void myfunction( MYSTRUCT _struct ){ std::cout << _struct.data1 << _struct.data2; } int main(){ //This is what I recently learned myfunction( MYSTRUCT{2,3} ); return 0; } This makes me wonder is this less costly

Rewind File, Create Dynamic Struct

£可爱£侵袭症+ 提交于 2019-12-13 05:49:49
问题 this function will rewind file, create the dynamic array (of size), and read in the data, populating the _data struct dynamic array. Note that stream is passed by value this time. The function then returns the populated array of struct struct _data { char* name; long number; }; struct _data *load(FILE *stream, int size) { struct _data BlackBox = calloc(size, sizeof(_data)); char tempName[3]; stream = fopen("names.txt", "r"); for (int i=0; i<size; i++) { fscanf(stream, "%s %ld", tempName,

Pass by value confusion in Scheme

末鹿安然 提交于 2019-12-13 02:27:45
问题 Consider the following procedure taken from SICP: (define (make-withdraw balance) (lambda (amount) (if (>= balance amount) (begin (set! balance (- balance amount)) balance) "Insufficient funds"))) Suppose I say: (define x (make-withdraw 100)) make-withdraw returns a procedure ( (lambda (amount) ... ) ) inside a new environment called e2 (enclosing the binding for the variable balance ), and binds that procedure to x in the global frame. Now, say I call: (f x) where (define (f y) (y 25)) 1 . I

DataGridView: Pass by Value or Reference?

不打扰是莪最后的温柔 提交于 2019-12-12 18:12:28
问题 I think of DataGridView's as being memory hogs. Is it better to pass by value a datagridview or by reference? Should it even be passed at all? 回答1: Passing it by value or reference isn't going to matter from a memory stand point, because the DataGridView is a reference type, not a value type. 回答2: The type DataGridView is a reference type and hence it's not possible to pass the object by value. You can pass the reference to the object by value but that is a very small (typically pointer sized

Pass by value or const reference to function?

狂风中的少年 提交于 2019-12-12 12:17:56
问题 Should I pass std::string by value or by reference to one function. This function store this values in member variable of class. I am always confuse when about pass by value or reference. Please clear my confusion about this. Here is code : class DataStore { public: void addFile(const string& filename, const set< std::string>& filePaths) { if (dataStoreMap.insert(make_pair(filename, filePaths)).second) { cout << "Data Added" <<endl; } else { cout << "Data not Added" << endl; } } private: //