about allocation of the static pointers

P

parag_paul

Suppose I have a struct

typedef struct atype{
int a;
int b;
} at;


main(){
static at* j= 0;

j->a =1;
j->b = 2;

pin(j);
}


void* pin(at* b)
{
...
...
}


Now how are static pointers to a struct allocated, In a 32 bit
machine, I expect the that static at* j should keep 4 bytes in the
global space, But I will require 8 byte or the size of the structure
to use the above program safely,

Does the compiler figure out the type and the size at compile time and
allocates 8 bytes in the global space for this static pointer, since
each call will be changing this data I need to double sure about
this,
Any help
 
R

Richard Bos

main(){
static at* j= 0;

j->a =1;

*Bang* You've just written through a null pointer. Don't do that.
Now how are static pointers to a struct allocated,

The same way as any other static object. But that is not your problem,
and system-specific mucking about with randomly guessed amounts of bytes
is even more irrelevant. What you're failing to do is allocate memory
for the pointer to point _at_.
The rest, all that farrago about 32-bits, 4-bytes, 8-bytes, 9-bit-bytes,
12-octets and 48k, you should forget _right now_, and not return to it
until you understand why you have made a fundamental error in the above
code. And by preference not even then.

Richard
 
M

Mark Bluemel

Suppose I have a struct

typedef struct atype{
int a;
int b;
} at;


main(){
static at* j= 0;

So j is a null pointer.
j->a =1;
j->b = 2;

Now you try to dereference the null pointer. Not a good plan.



Now how are static pointers to a struct allocated, In a 32 bit
machine, I expect the that static at* j should keep 4 bytes in the
global space, But I will require 8 byte or the size of the structure
to use the above program safely,

No - you need two sets of space. One set the size of a pointer to
"struct at", which is what j occupies and one set, which in your case
you don't have, which is the size of a "struct at", which the pointer
should point at.
Does the compiler figure out the type and the size at compile time and
allocates 8 bytes in the global space for this static pointer,

No - it allocates as much space as a pointer needs. The size of the
structure is irrelevant.
> since
each call will be changing this data I need to double sure about
this,

How about spending a bit more time with your C reference text(s) and the
FAQ (http://www.c-faq.com) and getting an understanding of what pointers
do, and how to use them properly?

A pointer needs to point at something. In your case, the pointer j needs
to point to (contain the address of) a "struct at". There are two
obvious ways of achieving this :-
1) struct at *j;
struct at k;
j = &k;

2) struct at *j;
j = malloc(sizeof *j); /* error handling omitted */

On the other hand, you could do better simply with :-

typedef struct atype{
int a;
int b;
} at;


int main(void ){
static at j;

j.a =1;
j.b = 2;

pin(&j);
}


void* pin(at* b)
{
...
...
}
 
S

santosh

Suppose I have a struct

typedef struct atype{
int a;
int b;
} at;


main(){
static at* j= 0;

j->a =1;
j->b = 2;

Where have you allocated space for an instance of 'at' and set 'j' to
point to it?
pin(j);
}


void* pin(at* b)
{
..
..
}


Now how are static pointers to a struct allocated,

In the C abstract machine they are created at the start of the execution
of the program (for file scope static objects) or the first time the
relevant scope is entered (for function scope and block scope static
objects) and thereafter retain their values and persist until program
termination. Note that this is true for all static objects, not just
pointers.
In a 32 bit
machine, I expect the that static at* j should keep 4 bytes in the
global space, But I will require 8 byte or the size of the structure
to use the above program safely,

This is platform specific and C has nothing to say about it. Generally
you can let your implementation worry about this.
Does the compiler figure out the type and the size at compile time and
allocates 8 bytes in the global space for this static pointer, since
each call will be changing this data I need to double sure about
this,

The compiler will do whatever is necessary to make it work. Whether it
makes sense in the context of your program is something only you can
know.
 
N

Nick Keighley

Suppose I have a struct

typedef struct atype{
int a;
int b;

} at;

main(){

int main (void)
static at* j= 0;

no memory allocated for structure

undefined behaviour
j->b = 2;

pin(j);

call to function without declaration

return 0;
}

void* pin(at* b)
{
..
..

}

Now how are static pointers to a struct allocated, In a 32 bit
machine,

it's generally a bad idea to start thinking about the number of bits
in an int/address when you're tying to figure things out.

I expect the that static at* j should keep 4 bytes in the
global space, But I will require 8 byte or the size of the structure
to use the above program safely,

it doesn't allocate anything for the struct so it *isn't* safe.

Does the compiler figure out the type and the size at compile time and
allocates 8 bytes in the global space for this static pointer,
no


since each call will be changing this data I need to double sure about
this,
Any help

your code is borken
 
P

parag_paul

int main (void)


no memory allocated for structure


undefined behaviour



call to function without declaration

return 0;



it's generally a bad idea to start thinking about the number of bits
in an int/address when you're tying to figure things out.


it doesn't allocate anything for the struct so it *isn't* safe.


your code is borken

Thank you all,
That helps
 
T

Thad Smith

Suppose I have a struct

typedef struct atype{
int a;
int b;
} at;


main(){
static at* j= 0;

j->a =1;
j->b = 2;

pin(j);
}


void* pin(at* b)
{
..
..
}

Others have mentioned the problem with your code. Here is one
alternative which directly allocates a struct atype object:

void* pin(at* b);
int main(void){
static at j;

j.a =1;
j.b = 2;

pin(&j);
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top