faq why use p=NULL?

F

fcvcnet

Hi,
This is c code , but the question is same to c++ £¨new/delete£©.

char* p = malloc(10);
strcpy(p, "hello");
printf("%s\n", p);
free(p);
// free just add p to the free list of memory, and do nothing else.
// Is there any use of p? If we never use p why not just nullify p in
free()?
p = NULL; // If we do not use p after call free , we can delete this line
..

And someone write p = NULL is not a good idea for Unix.

Thanks.
 
P

Peter Jansson

fcvcnet said:
Hi,
This is c code , but the question is same to c++ £¨new/delete£©.

char* p = malloc(10);
strcpy(p, "hello");
printf("%s\n", p);
free(p);
// free just add p to the free list of memory, and do nothing else.
// Is there any use of p? If we never use p why not just nullify p in
free()?
p = NULL; // If we do not use p after call free , we can delete this line
.

And someone write p = NULL is not a good idea for Unix.

Thanks.

Hello,

This does not answer your question byt why replace the code with the
following?
printf("%s\n","hello");

Regards,
Peter Jansson
http://www.jansson.net/
 
F

fcvcnet

Hi,
Do you mean "byt why not replace the code with the following?" ?
If it is , I think use "p" instead of "hello" is simple.
And I think if use "hello" , the compiler will allocate a chunck of memory
to hold "hello".
 
F

fcvcnet

Hi,
Can somebody introduce any book about c++ with memory, I have so many
question about this part.
Thank you very much.
 
D

dan2online

fcvcnet said:
Hi,
This is c code , but the question is same to c++ £¨new/delete£©.

char* p = malloc(10);

malloc returns void * pointer, so the statement is changed into:
char *p = (char *)malloc(10);
free(p);
// free just add p to the free list of memory, and do nothing else.
// Is there any use of p? If we never use p why not just nullify p in
free()?
p = NULL; // If we do not use p after call free , we can delete this line
.
you can wrap the free function that has this ability, but the argument
is the reference to the pointer.

void free_wrapper (void **pp)
{
free ( *pp );
*pp = NULL;
}
And someone write p = NULL is not a good idea for Unix.

Why? I think NULL is a cleaner way to stand for Null pointer than 0 or
(void *)0 at least.
But for C++, it is a good point to introduce null object design
pattern instead of NULL.
 
I

Ian Collins

fcvcnet said:
Hi,
This is c code , but the question is same to c++ £¨new/delete£©.

char* p = malloc(10);
Don't use malloc, use new.
strcpy(p, "hello");
printf("%s\n", p);
Why not printf("hello\n"); ?
free(p);
// free just add p to the free list of memory, and do nothing else.
// Is there any use of p? If we never use p why not just nullify p in
free()?
Because you pass the pointer by value, so free() can't change the value
of the pointer.
p = NULL; // If we do not use p after call free , we can delete this line
..

And someone write p = NULL is not a good idea for Unix.
Who and where?
 
P

Peter Jansson

fcvcnet said:
Hi,
Do you mean "byt why not replace the code with the following?" ?
If it is , I think use "p" instead of "hello" is simple.
And I think if use "hello" , the compiler will allocate a chunck of memory
to hold "hello".

Hi,

Yes, I did mean "...but why not replace..."

The compiler will also allocate a chunck of memory to hold "hello" for the
following part of your code:
strcpy(p, "hello");

Regards,
Peter Jansson
http://www.jansson.net/
 
F

fcvcnet

Thanks.
Because you pass the pointer by value, so free() can't change the value
of the pointer. now I know.
Who and where?
sorry for that I made a mistake.

BTW, Can I edit/delete the news I have had posted for there is a mistake I
made and that bring much more misunderstand?
 
P

persenaama

BTW, Can I edit/delete the news I have had posted for there is a mistake I
made and that bring much more misunderstand?

No.

Regarding the p=NULL issue, if the pointer object goes out of scope it
indeed is redundant. The language doesn't guard against this kind of
stuff: you have to keep track manually what is freed and what isn't
(likewise, what is allocated and what isn't). This is why it's a good
idea generally to zero pointers to memory that is already freed (to
avoid freeing the same memory twice or more).

But, if the pointer goes out of scope, knock yourself out and not zero
it.
 
P

persenaama

One more thing, goes without saying but saying it anyway. If you zero
pointer to memory that is freed, you easily propagate the information
to other parts of your program that the pointer doesn't point to valid
memory. (there must be someway to do this, and zeroing the pointer is
the obvious way).
 
R

Rolf Magnus

fcvcnet said:
Hi,
Do you mean "byt why not replace the code with the following?" ?
If it is , I think use "p" instead of "hello" is simple.

The write:

char* p = "hello";
And I think if use "hello" , the compiler will allocate a chunck of memory
to hold "hello".

Yes, but it will do that either way. Somewhere in memory, there has to be
the "hello". Your version just dynamically allocates a second chunk of
memory and then copies the "hello" over to that. Later, it deallcates the
memory again. So you do much more work than needed.
 
R

Rolf Magnus

dan2online said:
Why? I think NULL is a cleaner way to stand for Null pointer than 0 or
(void *)0 at least.

Maybe it would. However, NULL is not a null pointer. It's an integer
constant (actually a macro that is replaced by one).
But for C++, it is a good point to introduce null object design
pattern instead of NULL.

AFAIK, the next version of the C++ standard will address this issue.
 
R

Rolf Magnus

fcvcnet said:
Thanks.

sorry for that I made a mistake.

BTW, Can I edit/delete the news I have had posted for there is a mistake
I made and that bring much more misunderstand?

Yes. Newsreaders usually offer functionality to cancel a posting as well as
supersede one. However, some usenet servers don't seem to handle it
correctly, so your message might not be canceled completely.
 
P

peter koch

Rolf said:
The write:

char* p = "hello";


Yes, but it will do that either way. Somewhere in memory, there has to be
the "hello". Your version just dynamically allocates a second chunk of
memory and then copies the "hello" over to that. Later, it deallcates the
memory again. So you do much more work than needed.

My understanding of the OP's question was why you after delete p should
set p to 0. The silly code between new and delete (well - malloc and
free, but the op explains the problem is the same in both cases) was
just more or less random bits of code.

/Peter
 
D

Dervish

Maybe it would. However, NULL is not a null pointer. It's an integer
constant (actually a macro that is replaced by one).
Better to say that in C it can be macro. In C++ it should be #defined
as just 0. With no type. AFAIK Stroustrup votes for 0 and not NULL
since once can accidentally include file with invalid or old style NULL
definition.
 
R

Rolf Magnus

Dervish said:
Better to say that in C it can be macro.

I didn't talk about C, since we're in a C++ newsgroup.
In C++ it should be #defined as just 0. With no type.

First of all, 0 does have a type. Any expression has a type. The type of
literal 0 is int. Second, NULL can be defined as anything that resolves to
a constant integral expression with the value 0, so

#define NULL (4 + 3 - 7)

would also be ok.
AFAIK Stroustrup votes for 0 and not NULL since once can accidentally
include file with invalid or old style NULL definition.

He votes for 0, because NULL is supposed to be only used in pointer context,
but still is an integer, which can sometimes lead to unexpected results.
When using 0 directly, the fact that it's an integer is not hidden.
Consider:

#include <iostream>

void f(int)
{
std::cout << "f(int) called\n";
}

void f(char*)
{
std::cout << "f(char*) called\n";
}

int main()
{
f(NULL); // oops, calls f(int)
}

You would have to cast NULL to char* to get the correct result, which is
less obvious than the fact that you need to cast literal 0.

Another problem (though more relevant to C than C++) is that you have to be
careful with functions with variable argument lists, because you have to
cast NULL to a pointer here too if you actually want to pass a pointer.

If a function like

void f(const char* p, ...);

expects pointers to char until there is a null pointer, you would have to
write:

f("hello", "world", "this", "is", "a", "test", (char*)NULL);

If you use NULL without the cast, the behavior is undefined because you pass
the wrong type.
 
C

Cy Edmunds

fcvcnet said:
Hi,
This is c code , but the question is same to c++ £¨new/delete£©.

char* p = malloc(10);
strcpy(p, "hello");
printf("%s\n", p);
free(p);
// free just add p to the free list of memory, and do nothing else.
// Is there any use of p? If we never use p why not just nullify p in
free()?
p = NULL; // If we do not use p after call free , we can delete this
line
.

And someone write p = NULL is not a good idea for Unix.

Thanks.

Here is the answer from the perspective of the C++ delete operator:

http://public.research.att.com/~bs/bs_faq2.html#delete-zero

I would say the same considerations apply to malloc.

Cy
 

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
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top