Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 261
  • Last Modified:

strange: global stack

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
zizi21
Asked:
zizi21
  • 6
  • 3
  • 3
2 Solutions
 
Infinity08Commented:
I don't understand what you mean. Could you show your code ?
0
 
zizi21Author Commented:
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
 
zizi21Author Commented:
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
ozoCommented:
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
 
ozoCommented:
Stack s;
void func1()
{
s.num1=1;
s.num2=2;
}

instead of
void funct1(Stack *s)
{
s->num1=1;
s->num2=2;
}
0
 
Infinity08Commented:
>> 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
 
zizi21Author Commented:
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
 
Infinity08Commented:
So, where is s defined ? You declared it as extern ... So in which compilation unit is it defined ?
0
 
zizi21Author Commented:
sorry for the late reply...

the externs are defined in another file...
0
 
ozoCommented:
Do you initiialize
 s.boxes[0].height
 s.boxes[0].weight
 ...
 s.boxes[199].height
 s.boxes[199].weight
0
 
zizi21Author Commented:
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
 
zizi21Author Commented:
thanks for looking into this..sory for wasting yoru time..
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 6
  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now