473,609 Members | 2,296 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

faq why use p=NULL?

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.
Apr 18 '06 #1
35 2781

"fcvcnet" <fc*****@163.co m> skrev i meddelandet
news:e2******** **@news.cn99.co m...
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/
Apr 18 '06 #2
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".
Apr 18 '06 #3
Hi,
Can somebody introduce any book about c++ with memory, I have so many
question about this part.
Thank you very much.
Apr 18 '06 #4
fcvcnet wrote:
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 thisline
. 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.

Apr 18 '06 #5
fcvcnet wrote:
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?

--
Ian Collins.
Apr 18 '06 #6
yes, I reference from
http://users.actcom.co.il/~choo/lupg...x-memory.html#
c_alloc
char* p = malloc(10);
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.
sorry , I am wrong , I made a mistake of this.

Apr 18 '06 #7

"fcvcnet" <fc*****@163.co m> skrev i meddelandet
news:e2******** **@news.cn99.co m...
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/
Apr 18 '06 #8
Thanks.
Because you pass the pointer by value, so free() can't change the value
of the pointer.

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

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?
Apr 18 '06 #9
>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.

Apr 18 '06 #10

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

Similar topics

26
45401
by: Agoston Bejo | last post by:
I want to enforce such a constraint on a column that would ensure that the values be all unique, but this wouldn't apply to NULL values. (I.e. there may be more than one NULL value in the column.) How can I achieve this? I suppose I would get the most-hated "table/view is changing, trigger/function may not see it" error if I tried to write a trigger that checks the uniqueness of non-null values upon insert/update.
3
2112
by: iStrain | last post by:
Hiya. I'm _sure_ this is an FAQ, but Googling hasn't produced the answer in a way I can make sense out of. I know I should get this, but so far no way... I'm creating tables and doing queries in Perl, and Nulls have started to bother me greatly. The first issue is, as far as I understand it, a column should be NOT NULL if it is necessary (required) data. Now, if a column doesn't have to be NOT NULL; that is, it's not _required_ data,...
5
4237
by: Mike MacSween | last post by:
This as the row source for a combo: SELECT qryRole.RoleID, qryRole.Role FROM qryRole WHERE (((qryRole.RoleID) Not In (SELECT RoleID FROM qryRoleEvent INNER JOIN qryEvent ON qryRoleEvent.EventID = qryEvent.EventID WHERE ProdID = Forms!frmProductions!ProdID))) ORDER BY qryRole.Role; If there is just one RoleID with a null value in the subquery then the main
3
7645
by: sathyashrayan | last post by:
The standard confirms that the following initialization of a struct struct node { --- --- } struct node var = {NULL};
102
5960
by: junky_fellow | last post by:
Can 0x0 be a valid virtual address in the address space of an application ? If it is valid, then the location pointed by a NULL pointer is also valid and application should not receive "SIGSEGV" ( i am talking of unix machine ) while trying to read that location. Then how can i distinguish between a NULL pointer and an invalid location ? Is this essential that NULL pointer should not point to any of the location in the virtual address...
29
3734
by: Jason Curl | last post by:
I've been reading this newsgroup for some time and now I am thoroughly confused over what NULL means. I've read a NULL pointer is zero (or zero typecast as a void pointer), others say it's compiler dependent (and that NULL might be anything, but it is always NULL). The source snippet is below. The question is: - When I use calloc to allocate a block of memory, preinitialising it to zero, is this equivalent (and portable C) to...
5
24693
by: David Sworder | last post by:
Hi, I've created a UserControl-derived class called MyUserControl that is able to persist and subsequently reload its state. It exposes two methods as follows: public void Serialize(Stream s); public void Deserialize(Stream s); Within the MyUserControl class, there is a field of type MyInnerClass
64
3893
by: yossi.kreinin | last post by:
Hi! There is a system where 0x0 is a valid address, but 0xffffffff isn't. How can null pointers be treated by a compiler (besides the typical "solution" of still using 0x0 for "null")? - AFAIK C allows "null pointers" to be represented differently then "all bits 0". Is this correct? - AFAIK I can't `#define NULL 0x10000' since `void* p=0;' should work just like `void* p=NULL'. Is this correct?
0
5366
by: Aaron Morton | last post by:
I'm working on a IHttpModule that handles the PreSendRequestHeaders event from the HttpApplication, if the event is raised after EndRequest then HttpContext.Current is null. If it is raised before EndRequest (by turning response buffering off) then HttpContext.Current is set. To repo add the following to Global.asax protected void Application_PreSendRequestHeaders(object sender, EventArgs e) {...
46
3653
by: lovecreatesbea... | last post by:
Do you prefer malloc or calloc? p = malloc(size); Which of the following two is right to get same storage same as the above call? p = calloc(1, size); p = calloc(size, 1);
0
8126
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
8210
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8394
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6052
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5507
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4074
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2527
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 we have to send another system
1
1656
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1379
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.