By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,778 Members | 1,947 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,778 IT Pros & Developers. It's quick & easy.

x86 and C++

P: n/a
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?)
--

Best wishes,
Allen


Jul 19 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
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 :)
"Allen" <allen-terri-ng!@#att.net> wrote in message
news:v9***********************@bgtnsc04-news.ops.worldnet.att.net...
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?)
--

Best wishes,
Allen

Jul 19 '05 #2

P: n/a
Allen wrote:
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.

Jul 19 '05 #3

P: n/a
> 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)
Jul 19 '05 #4

P: n/a
"Gianni Mariani" <gi*******@mariani.ws> wrote in message
news:bp********@dispatch.concentric.net...
Allen wrote:
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.


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?
--

Best wishes,
Allen


Jul 19 '05 #5

P: n/a
On Mon, 17 Nov 2003 01:05:57 GMT, "Allen" <allen-terri-ng!@#att.net>
wrote in comp.lang.c++:
"Gianni Mariani" <gi*******@mariani.ws> wrote in message
news:bp********@dispatch.concentric.net...
Allen wrote:
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.


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
Jul 19 '05 #6

P: n/a
> 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?
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.536 / Virus Database: 331 - Release Date: 11/3/2003
Jul 19 '05 #7

P: n/a
Hi, see below

"Joe Estock" <je*****@nutextonline.com> wrote in message
news:vr************@corp.supernews.com...
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.

I hope that this answers your question :)
"Allen" <allen-terri-ng!@#att.net> wrote in message
news:v9***********************@bgtnsc04-news.ops.worldnet.att.net...
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?)
--

Best wishes,
Allen


Jul 19 '05 #8

P: n/a
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" <so*****@microsoft.com> wrote in message
news:bp**********@phys-news1.kolumbus.fi...
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)

Jul 19 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.