NO

Author Topic: something somewhere doesn't like the null pointer  (Read 6790 times)

smallholder

  • Guest
something somewhere doesn't like the null pointer
« on: August 23, 2008, 05:57:12 PM »
I am trying to execute a program that uses the null pointer. It compiles well, but the program crashes at execution.

I have fixed the problem by replacing the null pointer by a pointer to an empty variable, and the program has worked perfectly.

But I would like to know what is wrong with using the null pointer. Does anybody know?

JohnF

  • Guest
Re: something somewhere doesn't like the null pointer
« Reply #1 on: August 23, 2008, 06:32:19 PM »
You should post the code. Or the smallest compilable listing that exhibits the problem.

John

Synfire

  • Guest
Re: something somewhere doesn't like the null pointer
« Reply #2 on: August 24, 2008, 05:23:43 AM »
I am trying to execute a program that uses the null pointer. It compiles well, but the program crashes at execution.

I have fixed the problem by replacing the null pointer by a pointer to an empty variable, and the program has worked perfectly.

But I would like to know what is wrong with using the null pointer. Does anybody know?

Just off the top of my head, sounds like you have a place in your program where you are trying to access the contents of the pointer before you've allocated memory. Directing the pointer a variable would have fixed this, while pointing it to a NULL would cause a read/write exception. But as JohnF said, there isn't really much anyone can help you with unless we can see the code to verify what exactly is going on.

smallholder

  • Guest
Re: something somewhere doesn't like the null pointer
« Reply #3 on: August 31, 2008, 06:31:34 PM »
OK. Here's the code:

***********************************
#include <stdio.h>

struct entry {

   int value;
   struct entry *next; };


struct entry *findEntry (struct entry *initial, int toLookFor) {

        struct entry *pointer_to_result = initial;
   while (((*pointer_to_result).value != toLookFor) && (pointer_to_result !=0))
   {pointer_to_result = (*pointer_to_result). next;}
   return pointer_to_result;}


int main (void)   {

    struct entry a, b, c, d, e, f;
    printf("\nValue a:"); scanf("%i", &a.value);
    printf("\nValue b:"); scanf("%i", &b.value);
    printf("\nValue c:"); scanf("%i", &c.value);
    printf("\nValue d:"); scanf("%i", &d.value);
    printf("\nValue e:"); scanf("%i", &e.value);
    printf("\nValue f:");  scanf("%i", &f.value);

   struct entry *initial = &a, *result;

   
   a.next = &b;
   b.next = &c;
   c.next = &d;
   d.next = &e;
   e.next = &f;
   f.next = 0;

   int n, max;

       printf("\nHow many times do you want the program to run?\n");
   scanf("%i", &max);

   for (int i = 0; i < max; i++) {

   printf("\nPlease enter value to find:"); scanf ("%i", &n);

   result = findEntry (initial, n);

   if (result != 0) printf ("\nThe entry %i was found\n", (*result).value);
   else printf ("\nThe entry %i wasn't found\n", n); }

   return 0;       }
************************************************************


The program asks the user to enter a list of numbers. Then, the user is asked to enter any number, and the program is meant to find out whether the number is in the list. If the number is in the list, the program works fine and finds it. The crash occurs when the number entered is not in the list.

JohnF

  • Guest
Re: something somewhere doesn't like the null pointer
« Reply #4 on: August 31, 2008, 08:38:11 PM »
smallholder,

In the while loop when it runs out of 'next' structs, *pointer_to_result points nowhere, if you evaluate the expressions the other way around it will exit the loop before trying to dereference a NULL pointer. Dereferencing a NULL pointer will always create an exception.

Try this

Code: [Select]
while ((pointer_to_result != 0) && ((*pointer_to_result).value != toLookFor))
{
pointer_to_result = (*pointer_to_result).next;
}

John

smallholder

  • Guest
Re: something somewhere doesn't like the null pointer
« Reply #5 on: September 01, 2008, 08:03:32 PM »
Thanks, John. I've tried your suggestion, and it worked. But it was really finicky, and I would never have found out the solution myself!

JohnF

  • Guest
Re: something somewhere doesn't like the null pointer
« Reply #6 on: September 01, 2008, 09:07:15 PM »
That is the way C works - if the first expression does not evaluate to TRUE there is no point in evaluating the second expression because both expressions have to evaluate to TRUE for the while to continue.

John

dangan

  • Guest
Re: something somewhere doesn't like the null pointer
« Reply #7 on: September 21, 2008, 01:39:49 AM »
pointer_to _result->next; is the same as (*pointer_to _result).next;    I don't know what is more proper. ;)

severach

  • Guest
Re: something somewhere doesn't like the null pointer
« Reply #8 on: September 21, 2008, 08:00:50 AM »
They are functionally identical. pointer_to _result->next; is more readable. You might be forced in writing one form or the other when writing a macro.