Hello, I'm trying to create a graphic element in program with dynamic memory allocation, I just finished a semester learning about C++, but we didn't cover this topic and I would like some advice on the best way to do this.
Thank you for your response, I checked your link and it seems very interesting but I already have the actual graphic elements at my disposal,but I can't seem to create them in program.
This was just a test I did to see if I remembered how to do dynamic memory allocation, and it works.
//Insert confirmation message
test = "creation successful";
//Display string
EditBox->Text = test;
//Delete
delete[] test;
}
But what I would like to do is create a editing box when I push a button in program. I tried to do it with the code below, but it didn't work. If anybody can show me my mistakes it would help me alot.
This reminds me of Turbo Pascal's TurboVision: TButton, TView, etc. It was a GREAT library for creating Windows-like DOS applications. Had mouse support and everything.
Please use [code]//Code goes here [/code].
You are leaking memory by creating a new char array of 50 chars and then just losing the pointer. If you are not dynamically creating the string, don't allocate memory. Just go EditBox->Text ="creation successful";, provided it is the only error. I am also lost as to which library this is.
I really can't tell you where these things come from (TEdit or TObject) we weren't given any detailed explanation of the RAD we were using ( Borland C++ Builder 6 ), just how to use it. To insert a graphic element all we were told to do is select the element we want and place it in the interface, all the inclusions are automatically done by the RAD itself.
I know that I have to delete whatever I create, I was just giving a example. The string I inserted was just to show me if the allocation was successful.
I know that I have to delete whatever I create, I was just giving a example. The string I inserted was just to show me if the allocation was successful.
But you weren't inserting anything. You just replaced the address that was returned by new[] with the start address of the string literal, thus losing the previous address and creating a memory leak.
You then used delete[] on the string literal address, which isn't allowed and results in undefined behavior.