473,513 Members | 2,678 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Delete the dynamic allocated using new char[]

2 New Member
Please help me whether the following method to delete the memory which is created dynamically in another function is correct or not

Expand|Select|Wrap|Line Numbers
  1.  
  2. long newValues ( char** pszValue)
  3. {
  4.     *pszValue = new char[ 128 ];
  5.     strcpy( *pszValue, "newvalue");
  6.     return 0;
  7. }
  8.  
  9.  
  10.  
  11. int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,  LPSTR     lpCmdLine,  int  nCmdShow)
  12. {
  13.     char*  curValue;
  14.     newValues ( &curValue);
  15.     delete curValue;
  16.  
  17.     return 0;
  18. }
  19.  
  20.  
Thanks
Sep 21 '07 #1
3 2728
ilikepython
844 Recognized Expert Contributor
Please help me whether the following method to delete the memory which is created dynamically in another function is correct or not

Expand|Select|Wrap|Line Numbers
  1.  
  2. long newValues ( char** pszValue)
  3. {
  4.     *pszValue = new char[ 128 ];
  5.     strcpy( *pszValue, "newvalue");
  6.     return 0;
  7. }
  8.  
  9.  
  10.  
  11. int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,  LPSTR     lpCmdLine,  int  nCmdShow)
  12. {
  13.     char*  curValue;
  14.     newValues ( &curValue);
  15.     delete curValue;
  16.  
  17.     return 0;
  18. }
  19.  
  20.  
Thanks
I believe you need to delete with the brackets since you allocated an array.
Sep 21 '07 #2
weaknessforcats
9,208 Recognized Expert Moderator Expert
You have hidden your new inside a function named newValues. This is a good idea. It's also a good idea to hide your delete. I suggest you write a deleteValues that deletes what newValues has allocated.

It's never good to have new/delete scattered all over the code.
Sep 21 '07 #3
mohidsheik
2 New Member
You have hidden your new inside a function named newValues. This is a good idea. It's also a good idea to hide your delete. I suggest you write a deleteValues that deletes what newValues has allocated.

It's never good to have new/delete scattered all over the code.
Thanks for the advice.The doubt what i have is, If I delete the newvalue inside the function, how the value will get returned to the calling method.
Sep 21 '07 #4

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

Similar topics

2
6685
by: Chris E. Yoon | last post by:
I just want to hear people's opinions on this subject. My application has lots and lots of short-lived objects that use dynamic allocation/deallocation. After implementing its functionality, I...
16
1999
by: cppaddict | last post by:
Hi, I am deleting some objects created by new in my class destructor, and it is causing my application to error at runtime. The code below compiles ok, and also runs fine if I remove the body...
5
2459
by: lixiaoyao | last post by:
hi all I use matrix & vector function to allocate the space myself in c, typedef struct matrix_array newdata; struct matrix_array{ float **sy,*sxx; }; newdata ndata;//new data struct...
11
2259
by: mufasa | last post by:
i believe deleting an already delete pointer should give me a segmentation fault! Why, then, is the following working? I am using gcc version 3.0.2. #include<stdio.h> #include <string> using...
13
1647
by: Franklin Li | last post by:
Hi All, I find one example codes as below: int send() { char msg; ... delete msg; ..
7
7007
by: AB | last post by:
Hi all, A thought crossed my mind.... if I allocate memory for an array at runtime using.... int* arr = new int ; what happens when I then de-allocate memory using
12
3351
by: yufufi | last post by:
Hello, How does delete know how much memory to deallocate from the given pointer? AFAIK this informations is put there by new. new puts the size of the allocated memory before the just before...
15
4991
by: LuB | last post by:
I am constantly creating and destroying a singular object used within a class I wrote. To save a bit of time, I am considering using 'placement new'. I guess we could also debate this decision -...
20
6504
by: sirsnorklingtayo | last post by:
hi guys please help about Linked List, I'm having trouble freeing the allocated memory of a single linked list node with a dynamic char* fields, it doesn't freed up if I use the FREE()...
0
7257
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7157
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7379
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
4745
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3232
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3221
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1591
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
798
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
455
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.