|
|
|
#1 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
Sid wrote:
> why does not the above program result in segmentation error. There is no defined behavior for undefined behavior. Brian |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
"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. |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#8 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#9 |
|
Messages: n/a
Hébergeur: |
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 |
|
![]() |
| Outils de la discussion | |
|
|