Help answer threads with 0 replies.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 03-23-2010, 04:21 AM   #1
LQ Newbie
Registered: Mar 2010
Posts: 1

Rep: Reputation: 0
What is the difference allocate a memory with new global and local

Hi All,
I am confused why it happens?
Please solve the problem……..

I generate a assembly code for following C++ code:

Allocate memory with new local
Case 1:
CPP Code

int main()
float * fptr = new float;
return 1;
Corresponding Assembly Code for new and fptr
li %r3,4
bl _Znwj

Allocate memory with new Global
Case 2:
CPP Code

float * fptr = new float;
int main()
return 1;

Corresponding Assembly Code for new and fptr

.align 2
.type _Z41__static_initialization_and_destruction_0ii, @function
stwu %r1,-16(%r1)
mflr %r0
cmpwi %cr7,%r3,1
stw %r0,20(%r1)
beq- %cr7,.L9

lwz %r4,20(%r1)
addi %r1,%r1,16
mtlr %r4

xoris %r3,%r4,0xffff
cmpwi %cr0,%r3,-1
bne %cr0,.L7
li %r3,4
bl _Znwj
lwz %r4,20(%r1)
lis %r9,fptr@ha
addi %r1,%r1,16
stw %r3,fptr@l(%r9)
mtlr %r4
.size _Z41__static_initialization_and_destruction_0ii, .-_Z41__static_initialization_and_destruction_0ii

.align 2
.type _GLOBAL__I_fptr, @function
li %r4,0
li %r3,1
ori %r4,%r4,65535
b _Z41__static_initialization_and_destruction_0ii
.size _GLOBAL__I_fptr, .-_GLOBAL__I_fptr
.globl fptr
.section .sbss,"aw",@nobits
.align 2
.type fptr, @object
.size fptr, 4
.zero 4
Old 03-23-2010, 04:48 AM   #2
Registered: Oct 2009
Posts: 467

Rep: Reputation: 139Reputation: 139
I'm not familiar with that flavour of assembler, but there's still a "li %r3,4 ; bl _Znwj" set of instructions in both versions, the only differences are (i) when those instructions are executed and (ii) what's done with the pointer.

In the first version, the memory only gets allocated as part of the main() function, but in the second version the memory is allocated before main() is ever entered - gcc inserts code before (and after) your main() function to do this.

And when you call new in your main() function the returned pointer is (presumably) moved onto the stack, but if fptr is global, "lwz %r4,20(%r1)" moves the pointer to the allocated memory to where fptr is in memory.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
How much memory can all the processes allocate all together? Premraja Linux - Newbie 1 05-16-2009 05:38 PM
Cannot allocate memory simha Linux - Newbie 4 01-23-2008 09:19 AM
Difference between resident memory,shared memory and virtual memory in system monitor mathimca05 Linux - Newbie 1 11-11-2007 04:05 AM
Cannot allocate memory Terroth Linux - General 13 02-09-2005 05:22 AM
allocate more memory? sockknitter Debian 3 11-06-2004 05:23 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:35 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration