Question on accessing structure

S

Sid

All,
See the below piece of C code snippet


typedef struct _a
{
int ia;
int ja;
}A;


typedef struct _b
{
int ib;
int jb;
}B;


typedef struct _x
{
A a;
B b;

}X;


void func(X *ptr)
{
ptr->b.ib = 10;
ptr->b.jb = 20;
ptr->a.ia = 48;
ptr->a.ja = 50;

}

main()
{
A *temp = (A*)malloc(sizeof(A));
func(temp);
printf("%d\n",((X*)temp)->b.ib);

}

why does not the above program result in segmentation error.
I am just allocating memory for structure A and typecasting it to
structure X and writing value to its members i.e Structure A and
structure B.
I compiled above program with MS VC++ and gcc ...it just gives me
warning and not runtime error as well.....and o/p is right.....

Output is
10
 
V

vippstar

All,
See the below piece of C code snippet

typedef struct _a
{
int ia;
int ja;

}A;

typedef struct _b
{
int ib;
int jb;

}B;

typedef struct _x
{
A a;
B b;

}X;

void func(X *ptr)
{
ptr->b.ib = 10;
ptr->b.jb = 20;
ptr->a.ia = 48;
ptr->a.ja = 50;

}

main()
{
A *temp = (A*)malloc(sizeof(A));
func(temp);
printf("%d\n",((X*)temp)->b.ib);

}

why does not the above program result in segmentation error.
Because it invokes undefined behavior, you pass a pointer to A where a
pointer to X is expected.
I am just allocating memory for structure A and typecasting it to
structure X and writing value to its members i.e Structure A and
structure B.
I compiled above program with MS VC++ and gcc ...it just gives me
warning and not runtime error as well.....and o/p is right.....
Your program most likely writes to memory you have access to, but as
far as C is concerned, you invoke undefined behavior and anything can
happend after that.
 
R

Richard Tobin

Sid said:
why does not the above program result in segmentation error.
I am just allocating memory for structure A and typecasting it to
structure X and writing value to its members i.e Structure A and
structure B.

Most general-purpose operating systems allocate memory in units of
"pages", which might be a few kilobytes long. You only get a
segmentation fault when you write to an unallocated page. The bytes
immediately after your malloc()ed space are probably within the same
page, and accessing them doesn't generate an error. Even if it
happened to be at the end of a page, it's quite likely that the
following page has already been allocated by the operating system,
even if malloc() hasn't used it for anything yet.

Keeping track of the exact bounds of malloc()ed memory would add a lot
of overhead and slow your program down. But there are tools that do
this, for finding bugs of the kind your program has. "Valgrind" is a
well-known example.

-- Richard
 
R

Richard

Default User said:
Sid wrote:



There is no defined behavior for undefined behavior.

Another wonderfully helpful reply from one of the clique. You must be
very proud of yourself.
 
C

CBFalconer

Sid said:
.... snip ...

main() {
A *temp = (A*)malloc(sizeof(A));

That heading should be "int main(void)". Don't cast the output of
malloc. If it gives you errors, fix them. They usually mean you
failed to #include <stdlib.h>. The approved syntax is:

if (!(temp = malloc(sizeof *temp))) errorexit();
 
S

Sid

Sid wrote:

... snip ...


That heading should be "int main(void)". Don't cast the output of
malloc. If it gives you errors, fix them. They usually mean you
failed to #include <stdlib.h>. The approved syntax is:

if (!(temp = malloc(sizeof *temp))) errorexit();

Folks,
Thanks a bunch for your responses....
When i tried compiling above program using armcc compiler it gave me
following error....

C:\C programs>armcc test1.c
"test1.c", line 38: Warning: C2218W: implicit 'int' return type for
'main' - 'void' intended?
"test1.c", line 40: Error: C3028E: <argument 1 to 'func'>: implicit
cast of pointer to non-equal pointer
test1.c: 1 warning, 1 error, 0 serious errors

So why does gcc and ms vc++ compiler allowing such code to compiler at
first.....where are armcc complains for the same....

thanks in advance...

Sid
 
F

Flash Gordon

Sid wrote, On 01/02/08 03:58:
Please don't quote peoples sigs, the bit typicaly after the "-- " unless
you are commenting on it. With an overly long double sig like Chuck's it
is even more important not to quote it than with sensible ones.
Folks,
Thanks a bunch for your responses....
When i tried compiling above program using armcc compiler it gave me
following error....

C:\C programs>armcc test1.c

You should investigate which options the compiler can take.
"test1.c", line 38: Warning: C2218W: implicit 'int' return type for
'main' - 'void' intended?

You should not use implicit int, i.e. you should always specify the
return type of functions and the type of variables. For hosted
(generally non-embedded) systems the two types for main in the standard are
int main(void)
int main(int argc,char *argv[])

For non-hosted compilers read the compilers documentation.
"test1.c", line 40: Error: C3028E: <argument 1 to 'func'>: implicit
cast of pointer to non-equal pointer

Since the code shown does not have a function called 'func' and the two
function calls shows don't have pointer arguments I don't know. Try
posting the exact code you fed to the compiler.
test1.c: 1 warning, 1 error, 0 serious errors

So why does gcc and ms vc++ compiler allowing such code to compiler at
first.....where are armcc complains for the same....

Also use sensible options with these compilers. A good starting point
with gcc is "-ansi -pedantic -Wall -Wextra" but you should read the
documentation.

Finally, make sure you are invoking armcc as a C compiler rather than as
a C++ compiler and that you have included all the required headers.
Complaints about pointer conversions normally means one of
- Invoking the compiler as a C++ compiler rather than a C compiler
- Failure to include a required header
- Having declared the pointer as the wrong type
- Having use the pointer incorrectly

The one thing the diagnostic almost never means is that you should add a
cast to the code.
 

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,777
Messages
2,569,604
Members
45,206
Latest member
SybilSchil

Latest Threads

Top