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
 
C

Carlos Martinez Garcia

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

m_pstrStartingPos will be NULL if you initialize it with 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?

every new must have a corresponding delete. C++ has no garbage collector.
 
G

Gavin Deane

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)));

There's a lot of non-standard stuff in there which means only people
using the same compiler as you have any chance of compiling it.
when i enter this code in a function first time then
m_pstrStartingPos is NULL

What does the function look like? You haven't shown the code.
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?

What do you initialise to NULL? There is no NULL in your code above.
There is no such thing as DELETE in C++. There is delete.
Using new
then set the variable to NULL


or should delete be used

General advice: Every new must have exactly one matching delete. Every
new [] must have eactly one matching delete [].

If you want more specific help, please post a minimal, complete
program, avoiding any non-standard extensions, so that we can copy
directly from your message and paste into a compiler to reproduce
exactly what you are seeing. See this link.

http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.8

Gavin Deane
 
I

Ivan Vecerina

: Hi i have the following code
:
: int length = _tcslen(pszSourceString);
: m_pstrStartingPos = new TCHAR[length + 1];
: memset(m_pstrStartingPos, 0, (length * sizeof(TCHAR)));

Note that it would be much easier to implement your code
by using std::vector.
Assuming that m_strStartingPos is declared as follows:
std::vector<TCHAR> m_pstrStartingPos;
You could replace the 3 previous lines with:
m_strStartingPos.assign( _tcslen(pszSourceString)+1, 0 );

And when you need a C-style pointer to the first element,
you write:
TCHAR* p = & m_strStartingPos[0];

: 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, you need to manually delete what you allocate with new[]
(probably in the destructor of your object, and also prior
to assigning a new memory block to your pointer if the
above function is called multiple times).

But if you use std::vector (or std::string etc) all this
memory management will be handled automatically.

:
: Using new
: then set the variable to NULL
:
:
: or should delete be used

Good C++ programmers rarely write 'delete' statements,
because they use collections and smart pointers to
automatically manage memory for them.


Ivan
 

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,950
Members
47,503
Latest member
supremedee

Latest Threads

Top