LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices

Reply
 
Search this Thread
Old 03-23-2010, 05:21 AM   #1
ajay.yadav
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
_Z41__static_initialization_and_destruction_0ii:
stwu %r1,-16(%r1)
mflr %r0
cmpwi %cr7,%r3,1
stw %r0,20(%r1)
beq- %cr7,.L9

.L7:
lwz %r4,20(%r1)
addi %r1,%r1,16
mtlr %r4
blr

.L9:
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
blr
.size _Z41__static_initialization_and_destruction_0ii, .-_Z41__static_initialization_and_destruction_0ii

.align 2
.type _GLOBAL__I_fptr, @function
_GLOBAL__I_fptr:
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
fptr:
.zero 4
 
Old 03-23-2010, 05:48 AM   #2
JohnGraham
Member
 
Registered: Oct 2009
Posts: 467

Rep: Reputation: 138Reputation: 138
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.
 
  


Reply


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 06:38 PM
Cannot allocate memory simha Linux - Newbie 4 01-23-2008 10:19 AM
Difference between resident memory,shared memory and virtual memory in system monitor mathimca05 Linux - Newbie 1 11-11-2007 05:05 AM
Cannot allocate memory Terroth Linux - General 13 02-09-2005 06:22 AM
allocate more memory? sockknitter Debian 3 11-06-2004 06:23 PM


All times are GMT -5. The time now is 02:54 PM.

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