Solved

strange: global stack

Posted on 2009-04-07
12
240 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
 
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use pointers 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.

747 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now