Why this program is crashing ???

Discussion in 'C Programming' started by code break, Mar 3, 2006.

  1. code break

    code break Guest

    Can Any one tell me why this program is crashing .

    testFunc()
    {
    int a1[10], *ptr ;

    f=&a1[0];
    f=f+1;
    return 0;
    }
     
    code break, Mar 3, 2006
    #1
    1. Advertising

  2. code break wrote:
    > Can Any one tell me why this program is crashing .


    NO. For why, see below.

    >
    > testFunc()


    int testFunc(void) /* much better style */

    > {
    > int a1[10], *ptr ;
    >
    > f=&a1[0];
    > f=f+1;
    > return 0;
    > }


    Because it's not a program? Show as a compilable minimum C program that
    exhibits whatever problem you have. What you posted does not even
    compile as a separate compilation unit to be later linked into a proper
    program (e.g. you never define `f`).
     
    Vladimir S. Oka, Mar 3, 2006
    #2
    1. Advertising

  3. code break

    Jaspreet Guest

    code break wrote:
    > Can Any one tell me why this program is crashing .
    >
    > testFunc()
    > {
    > int a1[10], *ptr ;
    >
    > f=&a1[0];
    > f=f+1;
    > return 0;
    > }


    A minimal program code please. What is f ?
     
    Jaspreet, Mar 3, 2006
    #3
  4. code break

    Nikunj Shah Guest

    This is not crashing its working absolutely fine.


    Vladimir S. Oka wrote:
    > code break wrote:
    > > Can Any one tell me why this program is crashing .

    >
    > NO. For why, see below.
    >
    > >
    > > testFunc()

    >
    > int testFunc(void) /* much better style */
    >
    > > {
    > > int a1[10], *ptr ;
    > >
    > > f=&a1[0];
    > > f=f+1;
    > > return 0;
    > > }

    >
    > Because it's not a program? Show as a compilable minimum C program that
    > exhibits whatever problem you have. What you posted does not even
    > compile as a separate compilation unit to be later linked into a proper
    > program (e.g. you never define `f`).
     
    Nikunj Shah, Mar 3, 2006
    #4
  5. code break

    Nikunj Shah Guest

    This is not crashing its working absolutely fine.


    Vladimir S. Oka wrote:
    > code break wrote:
    > > Can Any one tell me why this program is crashing .

    >
    > NO. For why, see below.
    >
    > >
    > > testFunc()

    >
    > int testFunc(void) /* much better style */
    >
    > > {
    > > int a1[10], *ptr ;
    > >
    > > f=&a1[0];
    > > f=f+1;
    > > return 0;
    > > }

    >
    > Because it's not a program? Show as a compilable minimum C program that
    > exhibits whatever problem you have. What you posted does not even
    > compile as a separate compilation unit to be later linked into a proper
    > program (e.g. you never define `f`).
     
    Nikunj Shah, Mar 3, 2006
    #5
  6. Nikunj Shah wrote:
    > This is not crashing its working absolutely fine.


    Please don't top post.

    While you're at it, can you explain exactly how this works "absolutely
    fine"?

    > Vladimir S. Oka wrote:
    > > code break wrote:
    > > > Can Any one tell me why this program is crashing .

    > >
    > > NO. For why, see below.
    > >
    > > >
    > > > testFunc()

    > >
    > > int testFunc(void) /* much better style */
    > >
    > > > {
    > > > int a1[10], *ptr ;
    > > >
    > > > f=&a1[0];
    > > > f=f+1;
    > > > return 0;
    > > > }

    > >
    > > Because it's not a program? Show as a compilable minimum C program that
    > > exhibits whatever problem you have. What you posted does not even
    > > compile as a separate compilation unit to be later linked into a proper
    > > program (e.g. you never define `f`).
     
    Vladimir S. Oka, Mar 3, 2006
    #6
  7. code break

    Pedro Graca Guest

    code break wrote:
    > Can Any one tell me why this program is crashing .


    What program. I can't see a program in your post; all I see is a
    function.

    > testFunc()


    int testFunc(void)

    or

    int testFunc(...) /* this looks strange. I have to review variable
    argument list material */


    I prefer to specify return type and parameters explicitly rather than
    relying on the defaults.

    > {
    > int a1[10], *ptr ;
    >
    > f=&a1[0];


    There's no scope for `f' at this point (unless it's a global variable).
    Assuming it's been declared as int* or void*, I'd write that as

    f = a1;

    because the name of an array specifies the address of its first element,
    and, for a simple assignment to a pointer the redundant "&" and "[0]"
    actally make it less legible for me.

    > f=f+1;
    > return 0;


    ptr has been declared but has never been used. Delete its declaration.

    > }



    If you really have this function in some code of yours, as what it
    really does is just return 0 to the caller, I think you could make your
    program much simpler by removing the function and replacing it with a
    constant zero instead;

    Example (where foo has been defined as a int):
    if (foo == testFunc()) { /* do something */ }
    /* replace last line with */
    if (foo == 0) { /* do something */ }

    --
    If you're posting through Google read <http://cfaj.freeshell.org/google>
     
    Pedro Graca, Mar 3, 2006
    #7
  8. code break

    pemo Guest

    code break wrote:
    > Can Any one tell me why this program is crashing .
    >
    > testFunc()
    > {
    > int a1[10], *ptr ;
    >
    > f=&a1[0];
    > f=f+1;
    > return 0;
    > }


    What the f()***!

    --
    ==============
    *Not a pedant*
    ==============
     
    pemo, Mar 3, 2006
    #8
  9. Pedro Graca <> writes:
    > code break wrote:

    [...]
    >> testFunc()

    >
    > int testFunc(void)
    >
    > or
    >
    > int testFunc(...) /* this looks strange. I have to review variable
    > argument list material */


    A prototype with "..." requires at least one parameter before the "...".

    Since testFunc doesn't use va_arg, there's no reason for it to have a
    "..." in its prototype anyway.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Mar 3, 2006
    #9
    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. Mr. SweatyFinger

    why why why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    922
    Mark Rae
    Dec 21, 2006
  2. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,076
    Smokey Grindel
    Dec 2, 2006
  3. why this program is not crashing

    , Feb 11, 2005, in forum: C Programming
    Replies:
    20
    Views:
    680
    Dave Thompson
    Feb 21, 2005
  4. James

    why this program is crashing

    James, Apr 17, 2005, in forum: C Programming
    Replies:
    6
    Views:
    306
    Barry Schwarz
    Apr 18, 2005
  5. Pallav singh

    why this program is Crashing

    Pallav singh, Jul 14, 2009, in forum: C++
    Replies:
    5
    Views:
    365
    James Kanze
    Jul 16, 2009
Loading...

Share This Page