new & null

G

gopal

Hi i have the following code

int length = _tcslen(pszSourceString);
m_pstrStartingPos = new TCHAR[length + 1];
memset(m_pstrStartingPos, 0, (length * sizeof(TCHAR)));

when i enter this code in a function first time then
m_pstrStartingPos is NULL

next when i enter the function i just initialize to NULL, but i am not
using DELETE to delete the allocated memory, is this is the corect way?

Using new
then set the variable to NULL

or should delete be used

Regards
JK
 
D

David Resnick

gopal said:
Hi i have the following code

int length = _tcslen(pszSourceString);
m_pstrStartingPos = new TCHAR[length + 1];
memset(m_pstrStartingPos, 0, (length * sizeof(TCHAR)));

Try comp.lang.c++.

-David
 
V

Vladimir S. Oka

gopal said:
Hi i have the following code

int length = _tcslen(pszSourceString);
m_pstrStartingPos = new TCHAR[length + 1];
memset(m_pstrStartingPos, 0, (length * sizeof(TCHAR)));

when i enter this code in a function first time then
m_pstrStartingPos is NULL

next when i enter the function i just initialize to NULL, but i am not
using DELETE to delete the allocated memory, is this is the corect way?

Using new
then set the variable to NULL

or should delete be used

Regards
JK

This is a question for comp.lang.c++

Followups set...
 
J

John Bode

gopal said:
Hi i have the following code

int length = _tcslen(pszSourceString);
m_pstrStartingPos = new TCHAR[length + 1];
memset(m_pstrStartingPos, 0, (length * sizeof(TCHAR)));

when i enter this code in a function first time then
m_pstrStartingPos is NULL

next when i enter the function i just initialize to NULL, but i am not
using DELETE to delete the allocated memory, is this is the corect way?

No. If you do not deallocate the old memory before overwriting the
pointer, you will have a memory leak.
Using new
then set the variable to NULL

or should delete be used

Regards
JK

Change it to:

int length = _tcslen(pszSourceString);
if (m_pstrStartingPos != NULL)
{
delete m_pstrStartingPos;
}
m_pstrStartingPos = new TCHAR[length + 1];
memset(m_pstrStartingPos, 0, (length * sizeof(TCHAR)));

Please note that new and delete are C++ operators, not C operators, and
that this question should have been asked in comp.lang.c++.

Followups set.
 
M

Martin Ambuhl

gopal said:
Hi i have the following code

int length = _tcslen(pszSourceString);

_tcslen is a meaningless identifier, as is pszSourceString.
m_pstrStartingPos = new TCHAR[length + 1];

C++ is a different language from C. It has its own newsgroup
< Your question belongs there, if anywhere, but
before posting there check their FAQ and follow the newsgroup. You
failed to follow these simple guidelines before posting here.
 
K

Keith Thompson

John Bode said:
gopal said:
Hi i have the following code

int length = _tcslen(pszSourceString);
m_pstrStartingPos = new TCHAR[length + 1];
memset(m_pstrStartingPos, 0, (length * sizeof(TCHAR)));
[snip]
Change it to:
[snip]

Please note that new and delete are C++ operators, not C operators, and
that this question should have been asked in comp.lang.c++.

Followups set.

Your answer should also have been posted only to comp.lang.c++.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,176
Messages
2,570,947
Members
47,501
Latest member
Ledmyplace

Latest Threads

Top