473,395 Members | 1,937 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,395 software developers and data experts.

C++ Release memory problem

Hi all,

I have a problem on release memory.

If I delcare the following pointer to pointer char, how can I free the
allocated memory ?

let say "myVar = 20" <---actually it is not a constant, but now I set
it to 20.

char **tempStr=(char**)new char[sizeof(char*)*myVar];
...
....do something to set value to each char pointer
...

I try to delete it by the following statement.

for (int i=0; i< myVar; i++)
delete tempStr[i];

BUT it is violet my program.

Can anybody tell me how to release memory in such case.

Thank you very much.
Alan
Jul 22 '05 #1
5 4418
"alan" <ch***********@i-cable.com> wrote in message
news:7b**************************@posting.google.c om
Hi all,

I have a problem on release memory.

If I delcare the following pointer to pointer char, how can I free the
allocated memory ?

let say "myVar = 20" <---actually it is not a constant, but now I set
it to 20.

char **tempStr=(char**)new char[sizeof(char*)*myVar];
..
...do something to set value to each char pointer
You need to show how you do this.
I try to delete it by the following statement.

for (int i=0; i< myVar; i++)
delete tempStr[i];

BUT it is violet my program.

Can anybody tell me how to release memory in such case.


Do you allocate memory with new? You only delete memory allocated with new.
I suggest you show a complete compileable example illustrating the problem.
--
John Carson
1. To reply to email address, remove donald
2. Don't reply to email address (post here instead)

Jul 22 '05 #2
"alan" <ch***********@i-cable.com> wrote in message
news:7b**************************@posting.google.c om...
Hi all,

I have a problem on release memory.

If I delcare the following pointer to pointer char, how can I free the
allocated memory ?

let say "myVar = 20" <---actually it is not a constant, but now I set
it to 20.

char **tempStr=(char**)new char[sizeof(char*)*myVar];
..
...do something to set value to each char pointer


I feel like what you are doing is not what you want to do. If you want to
create an array of 20 char pointers on the free store you have to do:
char **tempstr=new char *[20];


Ioannis Vranos

Jul 22 '05 #3

[snip]
char **tempStr=(char**)new char[sizeof(char*)*myVar];
..
...do something to set value to each char pointer
Allocate like this -
char **tempStr=new char *[20];
I try to delete it by the following statement.

for (int i=0; i< myVar; i++)
delete tempStr[i];


Delete like this -
delete [] tempStr;

-Sharad
Jul 22 '05 #4

"alan" <ch***********@i-cable.com> wrote in message
news:7b**************************@posting.google.c om...
Hi all,

I have a problem on release memory.


Problems releasing memory are usually caused by a bug earlier in the
program, not by releasing the memory in the wrong way.

john
Jul 22 '05 #5
alan <ch***********@i-cable.com> spoke thus:
char **tempStr=(char**)new char[sizeof(char*)*myVar];
As others stated, this is wrong. It looks like you thought you were
using malloc:

char **tmpStr=(char**)malloc( sizeof(char*)*myVar );

Prefer the correct usage of new to the above, however.
..
...do something to set value to each char pointer
..
This "something" is actually important. For example, if the something
involves strdup() (just a wild guess)...
delete tempStr[i];
....this is also wrong. Delete things you've allocated with new;
free() things you've allocated with malloc() (such as strdup()'ed
strings). malloc/delete and new/free combinations should NEVER be
used.
Can anybody tell me how to release memory in such case.


If you post code that demonstrates the problem, I'm sure we can help.
On the other hand, why not just use std::vector<std::string>? Then
you don't have to worry about releasing memory at all.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Jul 22 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: gchandran | last post by:
Hi All, Problem: Call to DOMDocument::getElementsByTagName() in a loop causes growth in memory consumption? Consider the following lines of code which simulates the problem that I am facing....
1
by: spiff | last post by:
We are migrating from VC++ 6 to VC++ 2003. It is a plain, unmanaged application with both C and C++ source. When running the debug build, even outside the debugger, the memory allocation/deallocation...
10
by: babak | last post by:
Hi everybody I'm working with a project in embedded Visual Studio 4 and I have a general problem which I hope that somebody can help me with. My problem is the following: My project works fine in...
4
by: slaprade | last post by:
I am loading a weeks worth of web logs into a dataset using Imports Microsoft.Data.Odbc These are text - fixed length fields so I have written a schema for them. The adapter fill looks like this...
5
by: Rodrigue | last post by:
I have code where I populate a data table using visual basic.net code. I call the clear method on the table but it does not seem to release the memory. When I populate the table again, the memory...
2
by: ABC | last post by:
How should I to release the pop-up page object on memory when I close the page? I have main page, which will open a popup webpage when a button clicked. In the first opened pop-up page, it is...
2
by: Epetruk | last post by:
Hello, I have a problem where an application I am working on throws an OutOfMemoryException when I run it in Release mode, whereas it doesn't do this when I am running in Debug. The...
2
by: Dave Johansen | last post by:
I just converted a solution from Visual Studio 2003 to Visual Studio 2005 and the Debug mode seems to be running just fine, but the Release mode crashes on the following code: std::ifstream...
7
by: Dmitriy V'jukov | last post by:
On Jun 16, 3:09 pm, Anthony Williams <anthony....@gmail.comwrote: Yes, I've already read this. It's just GREAT! It's far more useful and intuitive. And it contains clear and simple binding...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.