Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jun 21st, 2008, 6:05 AM   #1
rsnd
Hobbyist Programmer
 
rsnd's Avatar
 
Join Date: Jun 2005
Location: Helltown
Posts: 162
Rep Power: 4 rsnd is on a distinguished road
c++ header standards

Ok this is one of those stupid questions but #include <memory> or #include <memory.h> in c++?

I try to compile with memory in dev c++ and it complains and then when I try in vc++ 2008 it complains about undefined functions with memory.h
__________________
Spread your wings and fly! Chicken!
rsnd is offline   Reply With Quote
Old Jun 21st, 2008, 6:54 AM   #2
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,222
Rep Power: 5 grumpy is on a distinguished road
Re: c++ header standards

Neither. There is no standard header named <memory.h> specified in either the C or C++ standards. The C++ standard also specifies no standard header named <memory> or <cmemory>.

If you are trying to access functions like memset(), the relevant C header is <string.h> (which is deprecated in C++ and replaced by <cstring> which has the functions living in namespace std).
grumpy is offline   Reply With Quote
Old Jun 21st, 2008, 11:13 PM   #3
rsnd
Hobbyist Programmer
 
rsnd's Avatar
 
Join Date: Jun 2005
Location: Helltown
Posts: 162
Rep Power: 4 rsnd is on a distinguished road
Re: c++ header standards

i c
thanks
what about functions like malloc/free/realloc?
__________________
Spread your wings and fly! Chicken!
rsnd is offline   Reply With Quote
Old Jun 22nd, 2008, 12:22 AM   #4
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,222
Rep Power: 5 grumpy is on a distinguished road
Re: c++ header standards

malloc(), calloc(), realloc(), and free() are declared within <stdlib.h> (C, deprecated in C++) and <cstdlib> (C++, also in namespace std).

Surely, your compiler documentation would have had some mention of those functions.
grumpy is offline   Reply With Quote
Old Jun 22nd, 2008, 6:51 AM   #5
Ancient Dragon
PFO God In Training
 
Ancient Dragon's Avatar
 
Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 544
Rep Power: 4 Ancient Dragon is on a distinguished road
Re: c++ header standards

Quote:
Originally Posted by grumpy View Post
Surely, your compiler documentation would have had some mention of those functions.
Maybe yes, and maybe no. For example, MSDN still documents use of the .h extension, such as studio.h. For malloc() and associated functions MSDN doesn't distinguish the difference between C and C++ header files.
__________________
True Terror is to wake up one morning and discover that your high school class is running the country - Kurt Vonnegut Jr.
Ancient Dragon is offline   Reply With Quote
Old Jun 22nd, 2008, 10:39 AM   #6
Jabo
Not a user?
 
Join Date: Sep 2007
Posts: 272
Rep Power: 2 Jabo is on a distinguished road
Re: c++ header standards

Quote:
Originally Posted by Ancient Dragon View Post
For malloc() and associated functions MSDN doesn't distinguish the difference between C and C++ header files.
In C++, malloc has been replaced with new hasn't it?
Jabo is offline   Reply With Quote
Old Jun 22nd, 2008, 11:15 AM   #7
Seif
Hobbyist Programmer
 
Seif's Avatar
 
Join Date: Jan 2006
Location: UK
Posts: 215
Rep Power: 3 Seif is on a distinguished road
Re: c++ header standards

No. Malloc and new are different, and both have their place in C++, there are a number of differnces between the two.

new is an operator, that constructs an object and is type safe.

malloc is a function, malloc just allocates memory, it doesn't call the objects constructer, and returns a void pointer to the memory which is required to be explicitly cast.
Seif is offline   Reply With Quote
Old Jun 23rd, 2008, 1:38 AM   #8
Jabo
Not a user?
 
Join Date: Sep 2007
Posts: 272
Rep Power: 2 Jabo is on a distinguished road
Re: c++ header standards

Just goes to show don't believe everything you hear...
Jabo is offline   Reply With Quote
Old Jun 23rd, 2008, 4:28 AM   #9
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,222
Rep Power: 5 grumpy is on a distinguished road
Re: c++ header standards

Quote:
Originally Posted by Seif View Post
No. Malloc and new are different, and both have their place in C++, there are a number of differnces between the two.

new is an operator, that constructs an object and is type safe.

malloc is a function, malloc just allocates memory, it doesn't call the objects constructer, and returns a void pointer to the memory which is required to be explicitly cast.
Allocating raw memory (which is all malloc() does) is one of the steps in dynamically creating an object (or multiple objects) with operator new. That step occurs before constructor(s) are invoked when dynamically creating object(s) with operator new.

The return value from malloc() does not require explicit casting (or conversion) in C, although it does in C++.
__________________
Dear God
So far today I've done all right. I haven't been grumpy yet. But in a few minutes, God, I'm going to get out of bed, and from then on I'm going to need a lot more help.
AMEN
grumpy is offline   Reply With Quote
Old Jun 23rd, 2008, 10:15 AM   #10
Narue
Professional Programmer
 
Narue's Avatar
 
Join Date: Sep 2005
Posts: 419
Rep Power: 4 Narue is on a distinguished road
Re: c++ header standards

>The C++ standard also specifies no standard header named <memory> or <cmemory>.
You might want to double check that. A header called <memory> does indeed exist. It's where you find such useful creatures as auto_ptr and allocator.
__________________
Even if the voices aren't real, they have some pretty good ideas.
Narue is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
C++ time.h header Jabo C++ 3 Jun 19th, 2008 8:23 PM
How do I silence header? stu7398 PHP 4 Jan 13th, 2008 6:15 PM
Coding standards without control? martinig Other Programming Languages 1 Dec 4th, 2006 7:04 AM
question about header files and prototyping functions linuxpimp20 C 13 Sep 7th, 2005 8:28 AM
TCP Header for Echo Communication AusTex C++ 0 Feb 27th, 2005 3:57 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 2:27 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC