Monday, July 9, 2007

Write a program to check if the stack grows up or down.

Try noting down the address of a local variable. Call another function with a local variable declared in it and check the address of that local variable and compare!.


#include < stdio.h >
#include < stdlib.h >

void stack(int *local1);

int main()
{
int local1;
stack(&local1);
exit(0);
}

void stack(int *local1)
{
int local2;
printf("\nAddress of first local : [%u]", local1);
printf("\nAddress of second local : [%u]", &local2);
if(local1 < &local2)
{
printf("\nStack is growing downwards.\n");
}
else
{
printf("\nStack is growing upwards.\n");
}
printf("\n\n");
}

23 comments:

Anonymous said...

This is the wrong one. just reverse one is OK
if(local1 > &local2)
{
printf("\nStack is growing downwards.\n");
}
else
{
printf("\nStack is growing upwards.\n");
}
printf("\n\n");
}

Sekhar babu Karri said...

if(local1 < &local2)
{
printf " Stocking Growing to Higher address"
}
else{
printf " Stocking Growing to Lower address"
}

Anonymous said...

Someone pl clarify the doubt in this question

Anonymous said...

Shouldn't the program be as below? The earlier authors are comparing address of local2 with the address inside local1 passed from main.

if(&local1 > &local2)
{
printf("\nStack is growing downwards.\n");
}
else
{
printf("\nStack is growing upwards.\n");
}
printf("\n\n");
}

Anonymous said...

Is should not be: if(&local1 > &local2)

local1 is a pointer, so it's value is already the address of the local.

&local would give you the address of the pointer itself.

Anonymous said...

Cleonjoys:-

I don't understand the reason to fight on this. I would suggest to get into the memory layout of a process before discussing on whether stack grows up(any new stack frame residents getting incremental memory address) or down(decremental address). In UNIX, esp Solaris, Stacks begin @ Higher address and start downwards as new things getting inside.

Ex:

#include

int main(int argc, char **argv){
int a, b;

printf("Address of a is %d, and b is %d\n", &a, &b);

return 0;
}

O/P:

Address of a is 134511156, and b is 134511152

So the two automatic stack variable allocated on stack with incremental
address, so in respect to discussion its growing downwards.

Anonymous said...

Should declare the variables volatile. Otherwise the variables can be put in registers (and yes addresses thereof can be legitimately taken)

Anonymous said...

If we glance at the specification of the expression like, with terms of a captivating connection using an additional, but for a experiencing that is definitely engendered for those who have miltchmonkey an improved marriage on your own as well : and even to be a feeling of increased oneness family members or even mankind - that develops into much more extra ordinary that each one everyone is seeking in your daily course is actually really enjoy.

Anonymous said...

You actuallу make it ѕeem so еasy together ωith yοur ρrеsentation but I find this matter to be really оne thing whiсh I believe I'd never understand. It sort of feels too complicated and extremely large for me. I'm looking forωaгd for your
neхt submit, I will try to get the hang of it!


Alѕo visit my wеb ρage
Payday Loans
Also see my page > Payday Loans

Anonymous said...

Experiencing not any credit scores are sometimes tremendous obstacle if looking for a payday loan. Creditors should have enough advice to manufacture a decision. Scarcity of credit ranking can be described as losing piece while in the problem. It means the particular customer has not beforehand took out profit, had very little bank cards, basically no recommendations that could confirm his/her debt capabilities. Developing zero credit scores [url=http://pozyczkibezbiktop.net.pl]pożyczki bez bik[/url] is not only problematical on the subject of acquiring a personal loan, but it also can be a difficulties with areas of everyday living, as an example if looking to get a high-rise apartment (landlords test consumer credit plus references via many other landlords). Then again, all of us acquired not any credit standing in the beginning. You will want to beginning to formulate ones own credit scores sooner or later around your life. There would be wise to become at the first try pertaining to all. The good thing is, you will find loan providers so, who be aware that basically no credit scores may be a recent get started in as well as a prospective brand new shopper [url=http://szybkapozyczka247.net.pl]pożyczki prywatne tychy[/url] meant for a considerably long time. Whilst mortgage lenders together with debit card enterprises experience rigorous loans necessities, increasing credit rating isn’t hopeless, however it would be complicated. There are plenty of strategies for getting rid of basically no consumer credit rating, as an illustration eliminating a good fitting up loans using a secured personal or perhaps co-signer, trying to find a fabulous secured visa or mastercard, always keeping active data balanced, etc.

Cash loans may not be the biggest instrument for gathering credit history. Like a situation with point, they can put together credit scores just involving fast cash providers, although implement very little regarding normal loan companies similar to banking companies.

A number of concerning cash advance loans, nevertheless, will be [url=http://pozyczkiprywatnebezbik.org.pl]http://pozyczkiprywatnebezbik.org.pl[/url] the advantage that very little loan companies perfectly examine an individual's credit report, so it doesn’t quite topic for those who have basically no credit scores and also undesirable credit history when you however take a great potential for receiving sanctioned for just a loan product you will want. Virtually no consumer credit test is usually a problem solver in case that a desperate state of affairs develops any time cash is necessary speedy and the other doesn't have consumer credit and poor credit ratings and simply doesn't have time for it to use developing greetings credit rating. Fast cash banks you should not request difficult requests along with they do not necessitate guarantee [url=http://kredytbezbikuoferty.org.pl]kredyt bez bik[/url] or even a co-signer nevertheless give on the spot fast payday loans on a single moment of application form most often. They've also been fairly extremely fast and additionally practical. What is more, these are to choose from 24/7 for example weekends and annual vacations. Yet, payday advances are short-term and also small-dollar lending products along with high interest levels as well as investment expenses. Hence, cash loans will be for individuals that are usually absolutely certain they're going to easily afford to repay that loan product to come back only when it's expected.

Anonymous said...

What a stuff of un-ambiguity and preserveness of valuable know-how on
the topic of unexpected feelings.

Here is my homepage: research a review on web site cedar finance
My web page :: Learning Forex Trading

Anonymous said...

http://www.cafb29b24.org/docs/buyativan/#91634 buy ativan online pharmacy - ativan liver

Anonymous said...

I like to all a touch of shade when dealing with Christmas card
"envelopes". Is it hard to add an easy "clip craft" sweet walking cane or Christmas Bells
or Minature Christmas tree when making use of tags?
Believe I will certainly begin making use of tags this
year.
Many thanks!
Ellen.

Feel free to surf to my web site xerox phaser 8560dn

Anonymous said...

Great goods from you, man. I have understand your
stuff previous to and you are just extremely excellent.
I actually like what you've acquired here, really like what you are stating and the way in which you say it. You make it entertaining and you still care for to keep it wise. I can't wait to read much more from you.
This is really a great site.

my weblog :: ABC Personal Financing

Anonymous said...

Hey I am so glad I found your blog, I really found you by mistake, while I
was searching on Yahoo for something else, Nonetheless I am here now and would just like to say thanks a lot for a remarkable
post and a all round exciting blog (I also love the theme/design), I don't have time to browse it all at the moment but I have bookmarked it and also included your RSS feeds, so when I have time I will be back to read a great deal more, Please do keep up the superb work.

my web site ... xerox 8560mfp driver

Anonymous said...

Hello this is kinda of off topic but I was wondering if
blogs use WYSIWYG editors or if you have to manually code with HTML.
I'm starting a blog soon but have no coding expertise so I wanted to get advice from someone with experience. Any help would be enormously appreciated!

Look into my website - payday loan lenders

Anonymous said...

Hurrah! At last I got a web site from where
I know how to genuinely get helpful information concerning
my study and knowledge.

My page ... xerox 8560 ram error

Anonymous said...

I am really inspired with your writing skills and
also with the structure for your weblog. Is this a paid
theme or did you customize it yourself? Anyway keep up the excellent high quality writing, it's uncommon to peer a great blog like this one these days..

my blog post ... cedar finance 60 seconds

Anonymous said...

Many thanks match, worked like a beauty.

This has actually been doing my head in for days !
!

Here is my homepage: xerox phaser 8560 toner :: ::

Anonymous said...

Sаved as а favoгite, Ι like
your web site!

mу wеb blog Facebook marketing Strategies

Anonymous said...

Amazing blog! Do you have any tips for aspiring writers?
I'm hoping to start my own website soon but I'm a little lost on everything.
Would you recommend starting with a free platform like Wordpress or go for a paid option?

There are so many choices out there that I'm totally confused .. Any recommendations? Thanks a lot!

Also visit my blog ... acne

Anonymous said...

Many thanks for the answer. This functioned wonderful !
!!

My page ... xerox phaser 8560 ink []

Anonymous said...

Stack grows downwards means the stack pointer grows towards 0x00000000 if its a 32-bit.When a local variable is declared and called another function,then this local varaible will be pushed on to stack,means this will get higher adrress
so if local1>local2=>down
else =>upwards.
pls change the program as so..