I'm a beginner and I'm following a course on C programming.
In my book,a variable is defined as a memory space that stores a single unit of data (datum).
Is a data structure considered to be a single unit of data (datum)?
Ex: struct Point single_point;
C Terminology – Can Data Hold Multiple Values?
cterminology
Related Solutions
Yes, the design of your Vector
is 'wrong'.
- Such a vector is usually thought of as a resizeable array, with the associated expectation that, like an array, it directly contains the elements. Your requirement that
Vector
stores pointers to allocated elements breaks that expectation. - Storing pointers is inefficient if the elements to be stored are small (for example, integers or doubles), because the overhead of dynamic allocation becomes significant (2 to 3 times the data size) and you need to store both the pointer and the actual data, where in the traditional design, you only need to store the few bytes for the actual data. Additionally, in the traditional design, all the elements are in adjacent memory locations, which makes the typical access patterns (looping over the items) much more cache-friendly for the CPU than your design, where the elements are scattered around in memory.
- Sometimes it is necessary to conceptually store items in multiple containers. With the conventional design, this can be accommodated by storing pointers in the containers and managing the actual objects yourself. This technique can also be used to limit the amount of copying that needs to be done, if the user of the container thinks that it is a reasonable trade-off. With your design, you would need to dynamically allocate the pointers that get stored in the container, which is far from convenient and completely counter-intuitive for most developers.
- Any interface that requires a pointer to allocated memory is prone to misuse, because the compiler can't see that
Vector_append(&a)
is an error and to a human reviewer it is also not immediately obvious. If you are lucky, this is detected during testing, but in some cases it will go undetected for a very long time. - The fact that
Vector_remove()
deletes the data item will also not sit nicely in most development circles, because- Many coding guidelines require that corresponding
malloc
andfree
calls should be made from the same module. Your design makes it impossible to keep that requirement. - It is common practice to create, for complex structures that contain pointers to allocated memory, to create a pair of 'constructor'/'destructor' functions that take care of all the allocations/cleanup in one go, including the 'root' struct. This also does not work correctly with
Vector_remove
callingfree
.
- Many coding guidelines require that corresponding
First off, "defining" and "declaring" are things you do in source code, rather than at runtime. Some variable declarations and definitions correspond to actual machine code that gets executed, and some do not. As far as I know, the definition of an additional function parameter or local variable will not generate any additional machine code, but will merely change how many bytes the CPU's frame/stack/etc pointers get moved as part of the function call.
What is true is that memory for a function parameter gets allocated on the stack every time you call that function (by the aforementioned pointers being moved), while memory for a global variable only gets allocated once. In that sense you're absolutely right. But the memory allocated by those function calls will also be immediately deallocated when the function exits, while the global variable's memory has to stick around for the entire length of the program. In that sense "more memory will be used" is not really correct.
Of course, this would be a very silly reason to use a global variable, because any change in memory usage or runtime performance is likely to be extremely tiny or totally nonexistent, while adding global variables you don't need usually makes programs far more brittle and untestable in the long run (see Why is Global State so Evil? for more on that). Definitely do not do it unless you have hard data from profiling tools that shows the global variable "optimization" is actually a big performance win over the local variable.
Best Answer
Well, sometimes yes, in the very abstract, but often no if you look under the hood.
But even that single unit of data usually consists of multiple individual binary bits, maybe multiple bytes or words. So it is really a matter of terminology, or more importantly, perspective, as to whether we consider one something a datum or data. It's both is probably the most complete answer.
I think the main point I'd like to make is that there are different points of view depending on who is looking at it.
Consider the nature of the client role relative to the implementation role -- one of the most important relationships in programming. We try to separate client from implementation using abstraction; this eases programming especially as programs get larger, and change over time.
From the high-level, client point of view many items might be considered a single abstraction, and thus, logically a datum, despite consisting of multiple individual bits of state.
However, under the hood and from the implementation perspective, the datum decomposes into an organization or composition of other items.
For example, an array of int's might be considered a single datum to one way of looking at it. But it is also a composition of multiple int's. A linked list or tree is an even more complex data structure (than an array), and typically consists of multiple independent memory blocks, so it is composed of multiple data. However, we can often treat a linked list or tree as an abstraction, for example, we can pass a whole tree by reference to another function with a single parameter. Thus, from another point of view it is logically a single entity (though definitely composed of multiple words of memory).
C has
union
's andstruct
's, which reflect sum types and product types in Algebraic Data Types. These may or may not be considered data structures: however, when most use the term data structure, they are evoking a more complex (and possibly algorithmic) entity (than a single a struct or union) composed of multiple structs linked to each other by references of some sort (e.g. pointers).Note that regarding data structures the term Abstract Data Type comes up, which need to be differentiated from Algebraic Data Type (both abbreviated ADT). Abstract data structures support the client/implementation relationship (by abstraction, of course).
There is also mereology, which is the study of whole part composition, and is a recursive concept. A car is an entity in and of itself, however, can be decomposed into its parts. Mereology is the philosophical study of the various aspects and relationships between whole and part, and as such is apropos to the question about data vs. datum.