Program Crashing on Pointer Assignment

This is a discussion on Program Crashing on Pointer Assignment within the C Programming forums, part of the General Programming Boards category; Code: #include <stdlib.h> #include <math.h> int div32(long dividend, long divisor, long *quotient, long *rmdr) { *rmdr = dividend; *quotient = ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    3

    Program Crashing on Pointer Assignment

    Code:
    #include <stdlib.h>
    #include <math.h>
    
    int div32(long dividend, long divisor, long *quotient, long *rmdr)
    {
    	*rmdr = dividend;
    	*quotient = 0;
    	int shifts = 0;
    	
    	while ((divisor & 0x40) != 0x40)
    	{
    		divisor = divisor << 1;
    		shifts++;
    	}
    	int i = 0;
    	for (i; i <= shifts; i++)
    	{
    		if (*rmdr-divisor >= 0)
    		{
    			*rmdr -= divisor;
    			*quotient++;
    		}
    		
    		divisor = divisor >> 1;
    		if ( i!=shifts)
    			*quotient = *quotient << 1;
    	}
    	printf("&#37;l / %l = %l", dividend, divisor, *quotient);
    	
    	return 0;
    }
    
    void main (int argc, char *argv[])
    {
    	div32(50, 25, 0, 0);
    }
    crashes on line 6. even if line 6 is *rmdr=25

    if line 6 is commented out, it crashes on line 7


    WTF!??!?!
    Last edited by Salem; 01-29-2008 at 12:28 AM. Reason: Added code tags - this is the only time this will be done for you, learn to do it yourself

  2. #2
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    I usually use global variables if I can, and hence avoid stuff like this and the problems
    it can bring, so I am not used to all this parameter passing malarky


    However it appears you pass 0 to the routine in the place of *quotient.
    So you are probably then trying to put something into the address 0000
    when you do *quotient = 0.
    When you try to do this the computer thinks WTF! he's trying to overwrite
    the operating system and hence crashes in disgust, or something like that

    You could try changing the line to
    Code:
    printf("\n&#37;d",(int *)*quotient);
    or something like that, to see what value you were trying to overwrite.
    Last edited by esbo; 01-28-2008 at 07:45 PM.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,632
    Kindly post well indented code using code tags.

    Anyway, you are passing null pointers and then trying to dereference them, which is obviously a problem since they do not point to any object.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Chinese pâté foxman's Avatar
    Join Date
    Jul 2007
    Location
    Canada
    Posts
    404
    First of all, use code tags when posting more than one line of code.

    Second, it's totally normal it's crashing. You are passing 0 as argument value for your pointer. You can't acces memory at position 0. Note that your function call is all but semantically good (at least, for the last two arguments).

    By the way, writing "WTF!??!?!" won't give you answer any faster.

    I guess that what you wanted to do was more something like

    Code:
    // ...
    void main (int argc, char *argv[])
    {
       long quotient, rmdr;
       div32(50, 25, &quotient, &rmdr);
    }
    Also, before someone else says it, main returns int, see the FAQ.

    (arf, i'm so slow)

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,543
    Quote Originally Posted by esbo View Post
    I usually use global variables if I can, and hence avoid stuff like this and the problems
    it can bring, so I am not used to all this parameter passing malarky
    And don't listen to esbo's global variables mumbo-jumo. Global variables are bad practice.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    2,023
    Quote Originally Posted by foxman View Post
    Second, it's totally normal it's crashing. You are passing 0 as argument value for your pointer. You can't acces memory at position 0. Note that your function call is all but semantically good (at least, for the last two arguments).
    Technically, a null pointer does not have to point to position 0. But the rest is right. You can't dereference a null pointer.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  7. #7
    Registered User
    Join Date
    Jan 2008
    Posts
    7
    Pnevma,
    You can follow what foxman says. I would suggest one more way to avoid the crash. In div32(..) function definition, you can add the following line as 6th line:
    quotient = (long*) malloc(sizeof(long));
    Then 7th line can be:
    *quotient = 0;
    If you don't need the value of quotient outside the function div32(...), you can free within the function -
    free(quotient);

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by vsanandan View Post
    Pnevma,
    You can follow what foxman says. I would suggest one more way to avoid the crash. In div32(..) function definition, you can add the following line as 6th line:
    quotient = (long*) malloc(sizeof(long));
    Then 7th line can be:
    *quotient = 0;
    If you don't need the value of quotient outside the function div32(...), you can free within the function -
    free(quotient);
    Yes, that would avoid the crash, but if you don't actually want the quotient outside the function, you probably would not want to allocate, assign and free inside the function, but rather use a local variable for quotient calculation, and pass in the pointer as NULL, then check if the pointer is NOT NULL, then set the quotient from the local variable.

    Allocating memory could easily take as much time as the entire rest of the function.

    [Of course, the function won't know if the data is needed or not, but passing in NULL as a pointer can indicate it's not needed to the function].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 04-04-2009, 03:45 AM
  2. Program crashing on free() with an invalid pointer message
    By skreaminskull in forum C Programming
    Replies: 6
    Last Post: 01-23-2009, 04:10 AM
  3. need advice/hints for assignment, please help!!
    By dannyrabbittang in forum C Programming
    Replies: 1
    Last Post: 10-22-2006, 01:18 AM
  4. need help program crashing
    By tunerfreak in forum C++ Programming
    Replies: 14
    Last Post: 05-22-2006, 11:29 AM
  5. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21