Programming Forums
User Name Password Register
 

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

Reply
 
Thread Tools Display Modes
Old Apr 23rd, 2006, 11:49 AM   #11
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,260
Rep Power: 5 grumpy will become famous soon enough
This may seem circular, but raw memory is memory that your program is not treating as if it contains a particular type of data (eg an int, a pointer, a float, a struct, an enum, etc etc).

Let's say we have a type SomeType. SomeType may represent basic types (int, float, etc) or it may represent a class type that has a constructor. When you declare a SomeType, for example;
    SomeType x;
then a contiguous set of of sizeof(SomeType) bytes is found (or requested) from memory available to your program (which is usually provided by the operating system, which manages allocation of physical memory resources available to your hardware such as RAM). Once those bytes are found, your program subsequently acts as of that set of bytes represents a SomeType. That set of bytes is referred to by various names; one of them is raw memory.

Another example is when you are doing dynamic memory allocation using malloc(). For example;
    void *something = malloc(some_size);
    short *c = (short *)something;    // typecast not necessary in C
something is a pointer, and contains an address of a contiguous set of bytes. That set of bytes is another form of raw memory (with an additional property that the programmer requests it specifically, rather than letting the compiler implicitly request it from available resources). The second line tells the compiler that you wish c to be considered an array of shorts that uses the same area of memory (or, more accurately, it tells the compiler not to complain when you start doing operations, via the pointer c, on that memory that treat that memory as if it is a short).

In C++, a rough equivalent of malloc() is operator new. Operator new actually works in two phases: a call to a function named operator new() and then (for a type with a constructor) invoking a constructor (or, if creating an array of objects, invoking the constructor multiple times).

For example, by default
    SomeType *x = new SomeType;
actually works in two phases. The first phase is a call of
::operator new (sizeof SomeType)
by which your program requests raw memory to hold a SomeType. The second phase is invoking a constructor of SomeType to turn that raw memory into a valid (i.e. constructed) SomeType.
grumpy is offline   Reply With Quote
Old Apr 23rd, 2006, 12:27 PM   #12
InfoGeek
Professional Programmer
 
InfoGeek's Avatar
 
Join Date: Jun 2005
Location: India, The great.
Posts: 435
Rep Power: 4 InfoGeek is on a distinguished road
Quote:
Originally Posted by grumpy
Your first comment is correct, your second is not;
#include<iostream>
using namespace std;
class X
{
  private:
       int a;
  public:
       X(int b)
        {
          a=b;
          cout<<"X invoked"<<endl;
         }
       void show()
        {
          cout<<"a=="<<a<<endl;
         }
};

int main()
{
   X a(10);
   X b=X(20);
   a.show();
   b.show();
   return 0;
}
may be I was confused with this statement:
   X b=X(20);
I know It'd invoke the copy constructor. If no copy constructor is provided, it'd create a bitwise copy. Sorry for the confusion.
__________________
PFO - My daily dose of technology.
InfoGeek is offline   Reply With Quote
Old Apr 23rd, 2006, 12:39 PM   #13
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,260
Rep Power: 5 grumpy will become famous soon enough
Quote:
Originally Posted by InfoGeek
I know It'd invoke the copy constructor. If no copy constructor is provided, it'd create a bitwise copy. Sorry for the confusion.
Sorry, but you've introduced more confusion. If no copy constructor is provided, the compiler will create a publically accessible copy constructor. Subject to a few other conditions (eg all data members are basic types or classes with public copy constructors, and any base classes have publically accessible copy constructors).

The only circumstance in which a compiler generated copy constructor will create a bitwise copy is for classes that have no base classes and all data members are basic types (int, double, enums, pointers, etc).
grumpy is offline   Reply With Quote
Old Apr 23rd, 2006, 12:48 PM   #14
InfoGeek
Professional Programmer
 
InfoGeek's Avatar
 
Join Date: Jun 2005
Location: India, The great.
Posts: 435
Rep Power: 4 InfoGeek is on a distinguished road
Thanks for clearing up the confusion.
__________________
PFO - My daily dose of technology.
InfoGeek 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




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

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