about allocation of the static pointers

Discussion in 'C Programming' started by parag_paul@hotmail.com, Nov 13, 2007.

  1. Guest

    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
     
    , Nov 13, 2007
    #1
    1. Advertising

  2. Richard Bos Guest

    "" <> wrote:

    > 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
     
    Richard Bos, Nov 13, 2007
    #2
    1. Advertising

  3. Mark Bluemel Guest

    wrote:
    > 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)
    {
    ...
    ...
    }
     
    Mark Bluemel, Nov 13, 2007
    #3
  4. santosh Guest

    In article <>,
    <> wrote on Tuesday 13 Nov
    2007 3:42 pm:

    > 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.
     
    santosh, Nov 13, 2007
    #4
  5. On 13 Nov, 10:12, "" <>
    wrote:

    > 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

    > j->a =1;


    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


    --
    Nick Keighley
     
    Nick Keighley, Nov 13, 2007
    #5
  6. Guest

    On Nov 13, 3:16 am, Nick Keighley <>
    wrote:
    > On 13 Nov, 10:12, "" <>
    > wrote:
    >
    > > 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
    >
    > > j->a =1;

    >
    > 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
    >
    > --
    > Nick Keighley


    Thank you all,
    That helps
     
    , Nov 13, 2007
    #6
  7. Thad Smith Guest

    wrote:
    > 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);
    }


    --
    Thad
     
    Thad Smith, Nov 14, 2007
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ken
    Replies:
    24
    Views:
    3,947
    Ben Bacarisse
    Nov 30, 2006
  2. chris
    Replies:
    6
    Views:
    1,031
    chris
    Oct 28, 2005
  3. Replies:
    5
    Views:
    655
    Chris Thomasson
    Aug 23, 2007
  4. Bjarke Hammersholt Roune
    Replies:
    14
    Views:
    1,222
    Bjarke Hammersholt Roune
    Mar 6, 2011
  5. cerr

    pointers, pointers, pointers...

    cerr, Apr 7, 2011, in forum: C Programming
    Replies:
    12
    Views:
    731
Loading...

Share This Page