473,405 Members | 2,160 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

about allocation of the static pointers

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 '07 #1
6 2094
"pa********@hotmail.com" <pa********@hotmail.comwrote:
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
Nov 13 '07 #2
pa********@hotmail.com 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)
{
...
...
}
Nov 13 '07 #3
In article <11*********************@y27g2000pre.googlegroups. com>,
pa********@hotmail.com <pa********@hotmail.comwrote 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.

Nov 13 '07 #4
On 13 Nov, 10:12, "parag_p...@hotmail.com" <parag_p...@hotmail.com>
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

Nov 13 '07 #5
On Nov 13, 3:16 am, Nick Keighley <nick_keighley_nos...@hotmail.com>
wrote:
On 13 Nov, 10:12, "parag_p...@hotmail.com" <parag_p...@hotmail.com>
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 '07 #6
pa********@hotmail.com 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
Nov 14 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: hall | last post by:
I have a question regarding where memory is allocated when arrays are created. I'll illustrate this by example. I may be wrong on some details, do feel free to correct me. The code piece: int...
19
by: regisser | last post by:
I have a quastion for the C++ professionals and members of the C++ standartization commetee. As i know C++ standart requires an allocator to be a templated parameter in every STL container....
36
by: Bhalchandra Thatte | last post by:
I am allocating a block of memory using malloc. I want to use it to store a "header" structure followed by structs in my application. How to calculate the alignment without making any assumption...
24
by: Ken | last post by:
In C programming, I want to know in what situations we should use static memory allocation instead of dynamic memory allocation. My understanding is that static memory allocation like using array...
23
by: TefJlives | last post by:
Hi all, I'm learning a bit about C, and I have a few questions. I'm not trying to insult C or anything with these questions, they're just honestly things I don't get. It seems like pointers...
1
by: Peterwkc | last post by:
Hello all expert, i have two program which make me desperate bu after i have noticed the forum, my future is become brightness back. By the way, my problem is like this i the first program was...
4
by: Jess | last post by:
Hello, I tried several books to find out the details of object initialization. Unfortunately, I'm still confused by two specific concepts, namely default-initialization and...
3
by: ranjeetasharma81 | last post by:
Hi all, I have a big C-cod, in which there are lots of dynamic memory allocation used. I want to replace dynamic memroy allocation by static arrays. The following are the problems that i am...
4
by: Josefo | last post by:
Hello, is someone so kind to tell me why I am getting the following errors ? vector_static_function.c:20: error: expected constructor, destructor, or type conversion before '.' token...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.