473,324 Members | 2,254 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

using new/delete in a called function

In my program I am calling a function using a reference to a structure:
struct stringy
{
char* str;
int ct;
}
void set(reference to stringy, c-string[ ])
Inside the function I want to create an array and copy the passed c-string into the new array. Then set ct = to the length of the string and str = the pointer to my new array. I have always been taught to use new with delete but if I do that then I erase what stringy.str is pointing to. Also it is my understanding that any variables created in a called function cease to exist when that called function isn't running, does this apply to new? Will the 'new' array that I am creating automatically disappear when the set() function is not running? Do I use delete at the end of the program?
Nov 12 '09 #1
1 1864
RRick
463 Expert 256MB
The information changed in the passed stringy object will exist after the set function has returned. The reason is because you pass the struct as a reference. A pointer would also work, but passing stringy by value, would not.

The char array created by new will last until it is deleted, but the str value in the stringy object might not last that long. You have to watch the stringy object very carefully and make sure you delete the str pointer value before the stringy object goes out of scope. Once out of scope, the values are lost and you'll end up with a memory leak.

You also have a problem with using the same stringy object for 2 or more calls to set. The each extra call to set will overwrite the preceeding pointer value and you'll have a memory leak.


One way to fix this is to create a constructor and destructor for the stringy struct. The constructor creates the char array and sets the str and and ct value. The destructor will delete the str value. What's nice about the constructor and destructor is that the compiler automatically calls them when an object is created, newed, deleted, or goes out of scope.

Better yet, use the STL string object. This does all the work for you.
Nov 13 '09 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Daveyk0 | last post by:
Hello there, I have a front end database that I have recently made very many changes to to allow off-line use. I keep copies of the databases on my hard drive and link to them rather than the...
2
by: VR | last post by:
I have a form with a list view control, a button control and a main menu with menuDelete item. Any time I am handling a button click event or menuDelete click event I call DeleteItem() function...
10
by: Neelesh Bodas | last post by:
Hi all, suppose the ctor of the class throws. Thus, at this point in time, some memory has been allocated but the object couldnot be constructed. Also, the memory is not yet freed. Does the c++...
1
by: Robert Fitzpatrick | last post by:
I am running PostgreSQL 7.4.5 and have a trigger on a table called tblriskassessors which inserts, updates or delete a corresponding record in tblinspectors by lookup of a contact id and license...
31
by: anongroupaccount | last post by:
I have an ABC with a protected member that is a reference to an object of an ABC type: class ABC { public: virtual ~ABC() = 0; protected: AnotherABC& _member; }
9
by: Bern McCarty | last post by:
I am porting stuff from MEC++ syntax to the new C++/CLI syntax. Something that we did in the old syntax that proved to be very valuable was to make sure that the finalizer would purposefully...
31
by: Anamika | last post by:
Hello friends.... can anyone tell me what will happen when we do..."delete this"...
19
by: Daniel Pitts | last post by:
I have std::vector<Base *bases; I'd like to do something like: std::for_each(bases.begin(), bases.end(), operator delete); Is it possible without writing an adapter? Is there a better way? Is...
9
by: itdevries | last post by:
Hi, I've ran into some trouble with an overloaded + operator, maybe someone can give me some hints what to look out for. I've got my own custom vector class, as a part of that I've overloaded...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.