x86 and C++

A

Allen

Hi all,

Been reading Paul Carter's /PC Assembly Language/ e-book (google for
it). Very nice intro to x86. It really helped me to see how/why some of
the things in C++ are the way they are. It doesn't discuss dynamic memory
allocation however. Could someone give a brief overview of how commands
like "new" work? (the pseudo-assembly?)
 
J

Joe Estock

Not sure if this helps or not, but this is my understanding of the new and
delete operators:

int *pInt = new int[10];

this will allocate an array of 10 items. it works something like the c
equivalent (malloc) from what I understand. the return from new is a pointer
(in this case a pointer to an int);

to free() the memory allocated by new, simply perform a delete on it before
it goes out of scope:

delete pInt;

I hope that this answers your question :)
 
G

Gianni Mariani

Allen said:
Hi all,

Been reading Paul Carter's /PC Assembly Language/ e-book (google for
it). Very nice intro to x86. It really helped me to see how/why some of
the things in C++ are the way they are. It doesn't discuss dynamic memory
allocation however. Could someone give a brief overview of how commands
like "new" work? (the pseudo-assembly?)


new is a C++ operator and can be overridden by the programmer.

However, 99% of the time, operator new "allocates" memory from an unused
pool and calls a "constructor" method.

The way it allocates memory is not specified in the standardm however,
early implementations used "malloc" and "free" - there are dozens of
implementations of malloc and free you can look at. The whole space of
general purpose allocators is huge. Unless you want to write one or are
having particular issues with one, all you really need to know is the
interface and how to use one.
 
W

wogston

int *pInt = new int[10];
this will allocate an array of 10 items. it works something like the c
equivalent (malloc) from what I understand. the return from new is a pointer
(in this case a pointer to an int);

to free() the memory allocated by new, simply perform a delete on it before
it goes out of scope:

delete pInt;

You should delete[] what you allocate with new[]:

delete[] pInt;

I hope that this answers your question :)

It should bear mentioning, that constructor and destructor are called for
the created objects when using new/delete new[]/delete[].


p.s. shivering if I made any errors above, since I always get slapped here.
8)
 
A

Allen

Gianni Mariani said:
new is a C++ operator and can be overridden by the programmer.

However, 99% of the time, operator new "allocates" memory from an unused
pool and calls a "constructor" method.

The way it allocates memory is not specified in the standardm however,
early implementations used "malloc" and "free" - there are dozens of
implementations of malloc and free you can look at. The whole space of
general purpose allocators is huge. Unless you want to write one or are
having particular issues with one, all you really need to know is the
interface and how to use one.

What I'm really interested in is simply the learning. After I posted
here, I decided to re-post to the x86 board. I couldn't decide which to
post to; my question seemed a little OT in both groups.

What I'm looking for is basically a description of "new" in terms of
assembly. Does it simply use resx instructions? If so, is that all there
is to it or does it do anything else?
 
J

Jack Klein

What I'm really interested in is simply the learning. After I posted
here, I decided to re-post to the x86 board. I couldn't decide which to
post to; my question seemed a little OT in both groups.

What I'm looking for is basically a description of "new" in terms of
assembly. Does it simply use resx instructions? If so, is that all there
is to it or does it do anything else?

Anything is assembly is off-topic here.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
 
A

Alex Lyman

What I'm looking for is basically a description of "new" in terms of
assembly. Does it simply use resx instructions? If so, is that all there
is to it or does it do anything else?

"new" (not the [] variant) in c++ is translated into one or two function
calls:
#1) either __stdcall or __cdecl, depending on implimentation, to a memory
allocation routine, called "operator new" (alot of current-day
implimentations of "new" just turn and call malloc(), including MSVC++).
#2) only if a ctor is availiable: a thiscall, with the ECX register set to
the newly aquired memory, to the specified ctor function, if any.
#2b) not many compilers do this still (the old DOS Turbo C++ 3.0 does), but
if no ctor is availiable, some will instead call a memset()-like function
that 0's out the memory.

new[] is very similar: it allocates for step #1 (operator new[]), then steps
through all the objects and calls thier ctors (thiscall's, again).

delete is a pure function call. it thiscall's the dtor of the memory, and
then free's it (many implimentations use free() here)
delete[] is also a pure function call. It thiscall's the dtor of each
object (how it knows how many objects there are is implimentation specific),
then free's the entire block.

About the different call styles:
__cdecl: pushes arguments in reverse order (right to left). The caller is
required to clean the stack afterwards. Required for stdargs.
__stddecl: pushes arguments in reverse order (right to left). The called
function pops it's own arguments off the stack.
thisdecl: the "this" pointer is stored in register ECX, otherwise identical
to __cdecl.

p.s. if it seems like I know a bit too much about this, it's just because I
just spent 7 days trial-and-erroring this crud. See my thread "Calling
Dynamically?" from 11/10/2003

p.p.s. Jack: It's kinda/sorta OT, IMHO. I mean, who else but comp.lang.c++
would know about the internals of how new and new[] worked?
 
C

Catalin Pitis

Hi, see below

Joe Estock said:
Not sure if this helps or not, but this is my understanding of the new and
delete operators:

int *pInt = new int[10];

this will allocate an array of 10 items. it works something like the c
equivalent (malloc) from what I understand. the return from new is a pointer
(in this case a pointer to an int);

It also calls default constructor for each of the ten objects in the array.
For int type and built-in type, it is no problem :). If you are trying to do
the same thing to a class with no default constructor, you will receive a
compiler error.
to free() the memory allocated by new, simply perform a delete on it before
it goes out of scope:

delete pInt;

delete[] pInt;

The destructor for each element in the array is called here.
 
J

Joe Estock

ahh, i make that same mistake in almost every program i write and i never
catch it until i stress test it :))

anyway, thanks for the correction :)

wogston said:
int *pInt = new int[10];

this will allocate an array of 10 items. it works something like the c
equivalent (malloc) from what I understand. the return from new is a pointer
(in this case a pointer to an int);

to free() the memory allocated by new, simply perform a delete on it before
it goes out of scope:

delete pInt;

You should delete[] what you allocate with new[]:

delete[] pInt;

I hope that this answers your question :)

It should bear mentioning, that constructor and destructor are called for
the created objects when using new/delete new[]/delete[].


p.s. shivering if I made any errors above, since I always get slapped here.
8)
 

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,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top