Solved

strange: global stack

Posted on 2009-04-07
12
245 Views
Last Modified: 2012-05-06
hi everyone,

has anyone occured this strangeness before..

i have a stack which i have declared as global. so, i dont need to pass it to functions..
 however, the value is not set. i get zeros...in order to set the values, i need to pass the address of the stack...

i always thought that if a stack is global, i dont need to pass the address...thanks for any explaination...
0
Comment
Question by:zizi21
  • 6
  • 3
  • 3
12 Comments
 
LVL 53

Expert Comment

by:Infinity08
ID: 24085674
I don't understand what you mean. Could you show your code ?
0
 

Author Comment

by:zizi21
ID: 24085788
it is my assignment....:-(

maybe, i could ask step by step questions which led me to this observation:

1) If i have a global stack such as extern stack s, does this mean that there is one stack for the whole program ?
2) If the stack is accessed globally, does this mean, that i dont need pass the address of the stack each time i need to access the stack ?

0
 

Author Comment

by:zizi21
ID: 24085799
for example, func1 needs to use the stack...

i only need to do func1() instead of funct1(Stack *s)

void func1()
{
s.num1=1;
s.num2=2;
}

instead of
void funct1(Stack *s)
{
s->num1=1;
s->num2=2;
}
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 84

Accepted Solution

by:
ozo earned 250 total points
ID: 24085801
You don't need to pass the address, since everyone can get the address globally.
If an object that has automatic storage duration is not initialized explicitly, its value is
indeterminate.
If an object that has static storage duration is not initialized explicitly,
then:
 if it has pointer type, it is initialized to a null pointer;
 if it has arithmetic type, it is initialized to (positive or unsigned) zero;
 if it is an aggregate, every member is initialized (recursively) according to these rules;
 if it is a union, the first named member is initialized (recursively) according to these
rules.
0
 
LVL 84

Expert Comment

by:ozo
ID: 24085822
Stack s;
void func1()
{
s.num1=1;
s.num2=2;
}

instead of
void funct1(Stack *s)
{
s->num1=1;
s->num2=2;
}
0
 
LVL 53

Assisted Solution

by:Infinity08
Infinity08 earned 250 total points
ID: 24085840
>> 1) If i have a global stack such as extern stack s, does this mean that there is one stack for the whole program ?

There will be one globally defined stack with that name, yes. And every part of the program will have access to that stack (assuming it's properly done, which is why I was asking to see the code).

If you can't post the exact code, then can you post a small sample program that has the qame problem ?


>> 2) If the stack is accessed globally, does this mean, that i dont need pass the address of the stack each time i need to access the stack ?

Indeed. Since it's global, you can just access it directly using its name. Ensure that the name is not "hidden" by a local variable with the same name though (for example).
0
 

Author Comment

by:zizi21
ID: 24086060
i can post a sample of the code which is similar..i change the variables..
typedef struct box
{      
int height;
int weight;
}BOX;

typedef struct stack
{
      BOX* boxes;
      int size;
      int    top;
}STACK;

extern STACK s;

and then, i call create() function which initialises it like this:
 
void create()
{
s.size = 200;
      
s.top = -1;
s.boxes = (BOX *)malloc(s.size*sizeof(BOX));
}

thanks ...
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 24086122
So, where is s defined ? You declared it as extern ... So in which compilation unit is it defined ?
0
 

Author Comment

by:zizi21
ID: 24103095
sorry for the late reply...

the externs are defined in another file...
0
 
LVL 84

Expert Comment

by:ozo
ID: 24103122
Do you initiialize
 s.boxes[0].height
 s.boxes[0].weight
 ...
 s.boxes[199].height
 s.boxes[199].weight
0
 

Author Comment

by:zizi21
ID: 24103392
the problem is solved...i dont know what it was but i recreated the file a fresh by having separate functions to create, push and tested and it works..i have done a mistake somewhere ..

i initalise it like this
s.boxes[s.top].height
 s.boxes[s.top].weight
0
 

Author Closing Comment

by:zizi21
ID: 31567422
thanks for looking into this..sory for wasting yoru time..
0

Featured Post

Easy, flexible multimedia distribution & control

Coming soon!  Ideal for large-scale A/V applications, ATEN's VM3200 Modular Matrix Switch is an all-in-one solution that simplifies video wall integration. Easily customize display layouts to see what you want, how you want it in 4k.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question