PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.c > Question on accessing structure
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Question on accessing structure

Réponse
 
LinkBack Outils de la discussion
Vieux 31/01/2008, 11h52   #1
Sid
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Question on accessing structure

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


  Réponse avec citation
Vieux 31/01/2008, 11h58   #2
vippstar@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Question on accessing structure

On Jan 31, 1:52 pm, Sid <Sid4C...@gmail.com> wrote:
> 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éponse avec citation
Vieux 31/01/2008, 12h31   #3
Richard Tobin
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Question on accessing structure

In article <147788b5-f183-4043-8f51-c36115f5a5e0@k39g2000hsf.googlegroups.com>,
Sid <Sid4Comp@gmail.com> wrote:

>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
--
:wq
  Réponse avec citation
Vieux 31/01/2008, 18h38   #4
Default User
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Question on accessing structure

Sid wrote:


> why does not the above program result in segmentation error.


There is no defined behavior for undefined behavior.





Brian
  Réponse avec citation
Vieux 31/01/2008, 23h13   #5
Richard
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Question on accessing structure

"Default User" <defaultuserbr@yahoo.com> writes:

> Sid wrote:
>
>
>> why does not the above program result in segmentation error.

>
> There is no defined behavior for undefined behavior.


Another wonderfully ful reply from one of the clique. You must be
very proud of yourself.
  Réponse avec citation
Vieux 01/02/2008, 00h36   #6
CBFalconer
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Question on accessing structure

Sid wrote:
>

.... 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();

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.



--
Posted via a free Usenet account from http://www.teranews.com

  Réponse avec citation
Vieux 01/02/2008, 01h23   #7
CBFalconer
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Question on accessing structure

Richard wrote:
> "Default User" <defaultuserbr@yahoo.com> writes:
>> Sid wrote:
>>
>>> why does not the above program result in segmentation error.

>>
>> There is no defined behavior for undefined behavior.

>
> Another wonderfully ful reply from one of the clique. You
> must be very proud of yourself.


I guess it nymshifted. PLONK

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.



--
Posted via a free Usenet account from http://www.teranews.com

  Réponse avec citation
Vieux 01/02/2008, 03h58   #8
Sid
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Question on accessing structure

On Feb 1, 5:36 am, CBFalconer <cbfalco...@yahoo.com> wrote:
> Sid wrote:
>
> ... 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();
>
> --
> [mail]: Chuck F (cbfalconer at maineline dot net)
> [page]: <http://cbfalconer.home.att.net>
> Try the download section.
>
> --
> Posted via a free Usenet account fromhttp://www.teranews.com


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
  Réponse avec citation
Vieux 01/02/2008, 08h25   #9
Flash Gordon
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Question on accessing structure

Sid wrote, On 01/02/08 03:58:
> On Feb 1, 5:36 am, CBFalconer <cbfalco...@yahoo.com> wrote:
>> Sid wrote:
>>
>> ... 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();
>>
>> --
>> [mail]: Chuck F (cbfalconer at maineline dot net)
>> [page]: <http://cbfalconer.home.att.net>
>> Try the download section.
>>
>> --
>> Posted via a free Usenet account fromhttp://www.teranews.com


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.
--
Flash Gordon
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 22h39.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,20075 seconds with 17 queries