answersLogoWhite

0

Yes, provided both structures are of the same type:

struct S {/*...*/};

S a, b;

// ...

a = b;

The built-in structure assignment operator is typically implemented as a memory copy (memcpy) operation as this is the most efficient means of performing member-wise copy, as opposed to performing individual assignment operations upon each member.

Be aware that pointer members are shallow-copied not deep-copied:

struct S {

void* ptr;

size_t size;

};

S a;

a.size = 100 * sizeof(int); // allow for storage of 100 integers

a.ptr = malloc (a.size); // allocate memory

At this point, object 'a' owns the memory allocated to a.ptr.

S b;

b = a;

At this point, ownership of the memory is ambiguous because both a.ptr and b.ptr are referring to the same memory address. Shared memory is best avoided because releasing that memory will invalidate all objects that share the same memory:

free (a.ptr);

free (b.ptr); // Undefined behaviour: releasing the same memory twice!

To avoid this, we must manually perform a deep-copy:

S b;

b.size = a.size; // copy the size member

b.ptr = malloc (b.size); // allocate new memory

memcpy (b.ptr, a.ptr, a.size); // deep-copy

Note that the onus is entirely upon the programmer to ensure resource ownership is unambiguous. The compiler cannot help you.

In object oriented languages like C++, we use "smart" pointers (resource handles) rather than "naked" pointers. In this way, the onus of resource ownership shifts to the language itself.

User Avatar

Wiki User

9y ago

What else can I help you with?

Continue Learning about Engineering

Why is it necessary to overload an operator?

The assignment is done explicit without internal operation. Subject to the programming language, explicit assignment operators are needed wherever implicit ones are insufficient. Implicit assignment is typically implemented as a flat copy, while explicit overloading of the assignment operator allows for any other suitable behavior. Consider this example in pseudocode similar to C++: class Demo { int* valuepointer; ... }; Demo a, b; ... b = a; Assigning a to b using implicit assignment means that a.valuepointer and b.valuepointer share the same value. Both a and b can change the pointed-to value, and the either will "see" the change. This is the required behavior in some cases, but often, you'd want to explicitly assign a to b such that each has its own pointer, accessing different copies of the value. This behavior would require an explicit assignment operator (or copy constructor).


What is another word for science project?

Some synonyms for 'science project' are assignment or experiment.


How operator overloading is useful in class based programming?

The concept of Operator Overloading is similar to Method Overloading, in that the meaning of a given operator symbol changes according to the context it is being used in. That is, the semantics of the operator symbol are flexible, rather than fixed.The idea behind Operator Overloading is to take a common symbol, and adjust it's meaning to something logical for contexts other than what it was originally restricted to.The arithmetic operators ( + - * / ) are good examples. Using Operator Overloading, I could define that 'SomeArray + SomeValue' means that I should add SomeValue to the end of the array SomeArray.In general, Operator Overloading is what is called 'syntactic sugar' - it makes things more readable. For instance, the equivalent way to do the above example via method calls would be: SomeArray.addToEnd(SomeValue)The major problem with Operator Overloading is that it depends on people having the exact same interpretation of what an operator would mean in the new context, which is difficult to assure. Going back to the above example, there is some ambiguity as to where 'SomeArray + SomeValue' would mean to add in SomeValue - should SomeValue be added to the start of the array, or the end of the array? The answer is not obvious, and one would have to go look through the overload definition. While this confusion is also possible with methods, properly named methods (i.e. using addToEnd() rather than just add() ) helps avoid this entirely.For this reason, Java does not support user-defined Operator Overloading. Java does support certain operator overloading in narrow contexts, but only those defined by the language itself. That is, the '+' sign is overloaded to allow for string concatenation. However, the designer of Java (James Gosling) decided that his preference was to avoid Operator Overloading completely due to his perception of a "clean" language.


Is it possible to compare the return value of a function call with another value using a relational operator?

pancakes


What is the role of data structure in compiler design?

The role of the data structure in compiler designer is to take an input of a program written in another language and produce an output in another language. It also performs error detection.

Related Questions

What is the root word of assignment?

The word assign is a verb (assign, assigns, assigning, assigned). The noun forms for the verb to assign are assigner, one who assigns, assignability, and the gerund, assigning. Another noun form is assignment.


Difference between assignment and equal to operator?

Well the assignment operator is used to assign the value of one variable to another variable. for eg. when we write a := b it means the value of b is now assigned to a. While an equal to operator is a truth relation between two variables. It will check if the variables are equal or not for eg. a = b will check if a is equal to b or not. The difference become clear when we write a := a+1 and a = a+1. In the first case the value of a is changed to a+1 while the second one is an absurd expression and it is wrong to write like that. Well in most of the programming languages like C , C++ and java they use '=' symbol as an assignment operator and '==' symbol for equal to operator. That is why you are able to write a = a+1 in your programming language but try writing a == a+1 and it will give error. I hope I am quite clear in what I wanted to explain. Regards Mohit Arora


Is it possible to assign data from one object of a class to another object of a same class?

Yes. In fact this is precisely why the copy constructor and assignment operator exist.


What is a mortgage assigned?

A mortgage assignment is a legal document whereby a lender transfers all its rights under a note and mortgage to another lender. The property owner continues to make their payments to the new owner of that mortgage.


Is TACP considered part of Special Tactics?

Yes they are but only about 1 % are assigned under Special Tactics. They have to go through another selection then attend the Special Tactics Training Squadron Operator Standards Course. After they graduate, they are then assigned to the 17 ASOS which supports the 75th Ranger Regiment or they will go to an STS.


Why is it necessary to overload an operator?

The assignment is done explicit without internal operation. Subject to the programming language, explicit assignment operators are needed wherever implicit ones are insufficient. Implicit assignment is typically implemented as a flat copy, while explicit overloading of the assignment operator allows for any other suitable behavior. Consider this example in pseudocode similar to C++: class Demo { int* valuepointer; ... }; Demo a, b; ... b = a; Assigning a to b using implicit assignment means that a.valuepointer and b.valuepointer share the same value. Both a and b can change the pointed-to value, and the either will "see" the change. This is the required behavior in some cases, but often, you'd want to explicitly assign a to b such that each has its own pointer, accessing different copies of the value. This behavior would require an explicit assignment operator (or copy constructor).


Which toru operator is an example of a domestic tour operator?

this answer was decided that it was unuseful and you should look at another answer.


What is another word for 911 operator?

Dispatcher


What is meant when you say you overload an operator?

All operators are built-in but not all operators can operate upon data types they know absolutely nothing about. There are some exceptions such as the new operator and the sizeof operator -- both will work on any datatype. However, for those that cannot, operator overloads allow you to cater specifically for those types. An operator overload is implemented just as you would overload a function, but it is not a function per se because operators have different calling conventions to functions. It is also important to keep in mind that just because you can overload an operator, it does not mean that you should. Operators should only be overloaded when the overload would allow a user to interact with your data type in an intuitive manner; a manner that is consistent with the operator's intended purpose. So while it can be amusing to overload the plus (+) operator to perform a subtraction (-), it could hardly be called intuitive. The assignment operator is the most overloaded operator of them all. This is because it is extremely useful to be able to copy the members of one object and assign those values to another object of the same type. We can also overload the assignment operator to cater for objects of different types, but such assignments are rarely intuitive. Does it make sense to assign the properties of a banana object to a person object? Probably not. Even if you could find a practical reason for doing so, would it be an intuitive operation? Definitely not. Therefore there's no point in providing an operator to cater for this. To create an operator overload, the declaration will often be placed inside the class it pertains to. However there are exceptions. The output stream insertion operator is a good example of this. The following example demonstrates how we can overload an internal operator (the assignment operator) as well as an external operator (output stream insertion operator). #include<iostream> // required to make use of I/O streams class A { private: unsigned m_data; public: // constructors... A (const unsigned data = 0): m_data (data) {} A (const A& copy): m_data (copy.m_data) {} // accessor function (interface) unsigned get_data() const { return m_data; } // operator overloads... A& operator= (const A& rhs) { m_data = rhs.m_data; } A& operator= (const unsigned rhs) { m_data = rhs; } }; std::ostream& operator<< (std::ostream& os, const A& a { os << a.get_data(); return os; } int main() { A a, b; // invoke default constructors a = 42; // call assignment operator overload b = a; // call default assignment operator overload // call insertion operator overload std::cout << a << std::endl; std::cout << b << std::endl; } Output: 42 42


What are the most common coding errors that occur in Cpp and what are the best ways to prevent them?

Using the assignment operator (=) instead of the equality operator (==) is one of the most common errors, one of the few that was inherited directly from C, and is unavoidable. Another common error is in not releasing resources when they are no longer required, but this can be avoided by using references instead of pointers as much as is possible.


What is bubble assignment?

Bubble assignment is a programming concept where multiple variables are assigned values in a single line of code. The values "bubble up" from right to left, with the rightmost value being assigned to the rightmost variable, then the next value to the left, and so on. This can be a convenient way to quickly assign values to multiple variables without writing separate assignment statements for each variable.


What is another name for a hustler?

floozie. operator. streetwalker.