answersLogoWhite

0


Best Answer

Assuming that the structure you want to free points to OTHER structures that have been malloc'ed, you need to free any malloc'ed elements that are pointeed to by the elements in your structure. Doing otherwise will result in a memory leak. As you free the elements of the structure, make sure that the pointers are NULL, then you should be able to free the structure. If you're doing all this in a UNIX like environment, all memory structures will be freed when the application ends. If you're doing it in a Windows environment, all bets are off.

User Avatar

Wiki User

18y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How 'free' works in c language if you want to free a structure whose elements are itself pointers?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

Why does not use pointer in java?

You do not use pointers in Java because the language designers decided to abstract memory management to a higher level in Java than in C. The reason for this is that it is easy to make mistakes using pointers and other lower level memory management techniques. These mistakes can lead to bugs. hard to read code, memory leaks that waste system resources, and security issues. Instead for the most part Java takes care of memory management for the user who can instead specify behavior though the object oriented techniques that are safer and easier to understand. The downside is that the programmers lose some control and flexibility in using memory. Also, programs using Java take a small performance hit in some cases because of the extra work Java has to do to manage memory itself. They are, however in Java they are called references.


What is text oriented approach?

Are the approaches that focus mainly on the form, the structure and the language of the text without paying any attention to the historical or social context of the writer and the text itself.


How arrays must be handled in c plus plus?

If the array is static it can declared in the structure itself: struct myArrayTag { int num[12]; // array of 12 integers (e.g., 48 bytes). } myArray; If it is dynamic then you must use a pointer and allocate the array outside the structure. You should also maintain a variable in the structure to keep track of how many elements the array currently has: struct myBufferTag { int * array; // Pointer to array of integers. int size; // Size of array (number of elements); } myBuffer;


Why would you use the array of pointers to pointers?

You would use an array of pointers to pointers whenever you wished to implement a dynamic multi-dimensional array of 3 or more dimensions. Every multi-dimensional array can ultimately be reduced to a one-dimensional array where each element is itself a one-dimensional array (an array of arrays). With fixed-size arrays, all elements can be allocated contiguously regardless of how many dimensions there are. Fixed size arrays can be allocated both statically (when the size is known at compile time) or dynamically (when the size is unknown at compile time). However with large arrays it is often necessary to divide the array into smaller subarrays each of which is allocated separately (non-contiguously with each other) and maintain a separate array of pointers to keep track of each of those subarrays. Although this consumes more memory than a contiguously-allocated array would, it has the added benefit in that each subarray need not be the same length, thus it can actually save memory overall. However, if we had several such arrays then we would need yet another array in order to keep track of them all, and this array would need to be an array of pointers to pointers.


Why multidimensional array element access using indirection operator?

The number of dimensions is immaterial. All arrays are implemented as a one dimensional array. A multidimensional array is simply an array where every element is itself an array. The only thing actually known about any array is that its name is a reference to the start address. Unlike an ordinary (non-array) variable, the elements in the array do not have names, we can only refer to them by their memory offsets from the start of the array. As such, in order to obtain the values stored at those offsets, we must dereference them. While the subscript operator gives us notational convenience, it's easy to forget that there's actually pointer arithmetic and dereferencing going on behind the scenes.

Related questions

What you mean by forward references?

In computer programming, a forward reference is a reference to an item not yet fully known at the point when the forward reference is required.The classic example would be that of a tree or list node structure of type Node, which itself contains elements which are pointers to Node elements. For a linked list, these elements might be pointers to previous and next nodes in the list, for a tree, those might point at right and left descendants.


What elements are in gold?

Gold is an element itself, with the symbol Au on the periodic table of elements.


Why does not use pointer in java?

You do not use pointers in Java because the language designers decided to abstract memory management to a higher level in Java than in C. The reason for this is that it is easy to make mistakes using pointers and other lower level memory management techniques. These mistakes can lead to bugs. hard to read code, memory leaks that waste system resources, and security issues. Instead for the most part Java takes care of memory management for the user who can instead specify behavior though the object oriented techniques that are safer and easier to understand. The downside is that the programmers lose some control and flexibility in using memory. Also, programs using Java take a small performance hit in some cases because of the extra work Java has to do to manage memory itself. They are, however in Java they are called references.


What are the elements of scandium?

Scandium is, itself, an element and so there are no elements of scandium.


How language and culture be linked together?

Language and culture are closely linked because language is an important aspect of culture, reflecting the beliefs, values, and customs of a society. Language shapes how individuals communicate, express their identities, and convey cultural norms. Similarly, culture influences language by determining the vocabulary, expressions, and even the structure of the language itself.


What is text oriented approach?

Are the approaches that focus mainly on the form, the structure and the language of the text without paying any attention to the historical or social context of the writer and the text itself.


Which sentence best describes the role carbon plays in the structure of compounds present in living things?

Carbon plays a fundamental role in the structure of organic compounds.


What structure into which DNA arranges itself?

DNA arranges itself into a double helix.


How arrays must be handled in c plus plus?

If the array is static it can declared in the structure itself: struct myArrayTag { int num[12]; // array of 12 integers (e.g., 48 bytes). } myArray; If it is dynamic then you must use a pointer and allocate the array outside the structure. You should also maintain a variable in the structure to keep track of how many elements the array currently has: struct myBufferTag { int * array; // Pointer to array of integers. int size; // Size of array (number of elements); } myBuffer;


Why is a set always a subset of itself?

Because all its elements belong to itself.


What element of Dante's Inferno shows the reader that he believes some sins are worse than others?

The structure of Hell itself


What are the elements in iron?

Iron is an element in itself.