![]() |
| ||
| How to delete a data structure in Builder 6.0? Hello, I am learning to use Borland Builder 6.0 (and trying to remember/learn to use C++ after taking a college course in C programming nine years ago). I have created a data structure. When I try to delete it using: delete films; I am told that deleting an object requires exactly one conversion to pointer... I wish I knew what this means. More fundamentally, I wish I knew how to modify the following structure definition in a manner that will permit me to delete it (and thus serve as a guide in the future). struct movies { char title[10]; int year; }; movies films; Please accept my thanks in advance for any assistance that you provide. |
| ||
| Re: How to delete a data structure in Builder 6.0? Exactly 1 delete for every new? struct moviesOr don't use delete and don't use new? struct moviescode tags: [code][/code] |
| ||
| Re: How to delete a data structure in Builder 6.0? Thank you for the response. I realize that not using "delete" in reference to the structure does eliminate the problem, but had hoped to learn how to free up memory that might otherwise be used by large structures in the manner usually deemed to be correct in programming circles. I did attempt: struct movies { char title[10]; int year; }; int main() { movies *films = new movies; // use films movies.title = "Harts War"; delete films; return 0; } The compiler responds with an error on the line that tries to assign a string to the title. I believe it is complaining about a pointer being on one side but no pointer on the other. If anyone can provide a means of both creating a structure and using its components, so that I can learn how to delete a structure in Builder 6.0 if I wish, I would be very grateful. Thank you. |
| |||
| Re: How to delete a data structure in Builder 6.0? Quote:
Quote:
movies.title = "Harts War";should be this films->title = "Harts War";But that is still wrong because you cannot assign C-style strings that way. Use strcpy or std::string for assignment. Quote:
|
| ||
| Re: How to delete a data structure in Builder 6.0? Quote:
THANK YOU. I am now able to create and delete a structure. As for the bigger picture, I had the functions working before I submitted my first post to this forum. I had perceived the deletion of a structure as a mere detail...until now. The reason follows and is based upon differences in the way in which one must code to use the structure after declaring it using a pointer reference. When I created the structure using "movies films;", and referenced the data using "films.title" and "films.year", my file input/output (read/write to disk) functions worked well. When I create the structure using "movies *films = new movies;", and reference the contents using "films->title" and "films->year", I can no longer display the title. What is the difference between these two approaches, and why must I use two different means of referencing the variables that comprise the structure depending on how the structure is declared? I am saving and reading structures from files using an iostream function and am referencing the structure to be saved using a char pointer as "(char*)(&films)". The problem seems to arise if I use the pointer means of declaring the structure ("movies *films = new movies;") when I read from the file and try to display "films->title" in a RichEdit text control using "RichEdit2->Text = films->title". I had this problem previously when I declared "title" to be an AnsiString, but worked through it and used char[10] to declare "title", StrCopy() to load data into "films.title", and "RichEdit2->Text = films.title" to display it. (Just a note, in that the use of movies instead of films in the variable assignment in my prior post was not a conceptual error, I typed the wrong variable name in the post. (They sound similar, and I didn't have the code in front of me). I had worked through the StrCopy() problem last night when I first encountered it, but had forgotten how to handle it this morning when the error arose again after I had retyped a segment of code with the new declaration statement. I probably would have continued to struggle had the prior post not reminded me of the manner of solution of that problem.) I am most certainly not trying to diminish the value of the assistance I have received here, and am both surprised by and grateful for the speed of the responses I have received. I had hoped to create a general structure I/O function for my future use, but now seem to need to create two differently coded structure I/O functions to accomodate the pointer means of declaring the structure, particularly if I wish to comply with the current C++ ANSI standard in my coding efforts in terms of the means by which I declare a structure. Using the iostream method, a pointer to a location in memory, and the size of the data I need to store, doesn't seem to enable me to display the title using "RichEdit2->Text = films->title;". I had previously been able to load the title as part of the structure that was loaded using the iostream method, then used "RichEdit2->Text = films.title" to display the title when I declared the structure using "movies films;". Once again, and most sincerely, thank you! |
| ||||
| Re: How to delete a data structure in Builder 6.0? Quote:
For example, loading a particular structure with information: why do you need dynamic allocation? Is the structure several thousand bytes in size? Is it part of a linked list? If the information is relatively small and the data is displayed (and discarded) as it is being read, then why bother? Quote:
Quote:
Quote:
For example, the following is a short snippet that uses a couple of functions that do very much the same thing in two different ways. The mywrite function uses dynamic allocation, the myread function uses automatic allocation. Both essentially do the same thing, but mywrite has excess overhead for the (in my opinion needless) dynamic allocation. #include <iostream> |
| ||
| Re: How to delete a data structure in Builder 6.0? Quote:
Many thanks to Mr. Sinkula for all of his assistance with this thread. The content provides valuable insight into the difference between stack based objects and dynamically allocated (heap associated) structures, as well as identifying variations in application between pointers and objects. I have, at this time, developed the functions that meet my goals in that they are now consistent with the C++ ANSI standard methods for creating and deleting structures contained within Builder 6.0. I couldn't have done it without the assistance that I have received at this site. |
| All times are GMT -4. The time now is 4:01 pm. |
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC