Solved

C Null pointer

Posted on 1997-03-26
5
706 Views
Last Modified: 2010-08-05
I have written a game.  When I quit out of the game, Im left with a NULL POINTER ASSIGNMENT error.

I dont use pointers in the game at alll.

Any ideas.
Game downloadable from http://www.bit.net.au/~ghendry/mystuff.html
0
Comment
Question by:ghendry
  • 3
5 Comments
 
LVL 2

Expert Comment

by:morleys
ID: 1249765
You probably use pointers more often than you think. If you make any calls to, for example, printf, you are passing char pointers to output msg strings. It is very hard to write 'c' code w/o using pointers somewhere. What this msg indicates is that you have stored a value using a null pointer. because you are in a small model you have damaged a location in you local memory which is generaly left aside with guard bytes in it so as to diagnose this problem. Had you been working in DOS with a large memory model you would have trashed the interupt table in low memory and crashed the system (it would probably have halted when the access occured). You should be able to run the debugger with a memory watch on location zero and trap the access or try linting your code, it can catch this error a lot, or try using a bounds checker type program which will dynamically catch illegal memory references.
0
 
LVL 1

Accepted Solution

by:
Fordream earned 100 total points
ID: 1249766
The error can occur without any pointer use. Illegal memory using could make the error. About this, morleys said detailedly. What I can say to help, examine your source code with much attention. Wherever, there must be an error as my experience. I had examined the cause of "Null pointer assignment" error for over 5 hours in source code exceeds 3000 lines. At last, a little mistake was there. Maybe the code is not complex, isn't it? Try to find.
 Potent cause I think is usage of scanf. Beginners often mistake in using them. For example, let's see below example source code.

#include <stdio.h>
#include <stdlib.h>

void main()
{
  int num, inputed;
  num = random(1000); /* random function is not ANSI-C compatible as I know. However, it is not important */
  while (1) {
    printf("Input a number you guessed:");
    scanf("%d", inputed);
    if (inputed > num)
      printf("Correct number is larger than %d!\n", num);
    else if (inputed < num)
      printf("Coreect number is smaller than %d!\n", num);
    else break;
  }
  printf("Congulations! You inputed the correct number!\n");
}

 This code seems to have no errors for beginners. But there is an error in scanf using. Arguments to scanf function must be pointer value. That is, you must send address to the 'inputed' variable, not a value of variable to scanf function. So, the line should changed like below:
    scanf("%d", &inputed);

 Excepting this case, there is a lot of cases to cause "Null pointer assignment" error. If you still can't find the error, post the source code to me, or here. I'll examine the code and tell you where did you make an error.
 Bye!

0
 

Expert Comment

by:smediodia
ID: 1249767
Sorry I cant load down your code and revised it for you 'cause I dont have any time at all, but I can give a good trick.
If your programm is under Turbo C, add a watch and type *(char*)0,4m . Then run the program step by step and you will see in the watch that this become 00 00 00 00. When the Null pointer assigment occurs it changes and takes any value. So try to see the error int this place.
See you ghendry.
0
 

Expert Comment

by:smediodia
ID: 1249768
Sorry I cant load down your code and revised it for you 'cause I dont have any time at all, but I can give a good trick.
If your programm is under Turbo C, add a watch and type *(char*)0,4m . Then run the program step by step and you will see in the watch that this become 00 00 00 00. When the Null pointer assigment occurs it changes and takes any value. So try to see the error int this place.
See you ghendry.
0
 

Expert Comment

by:smediodia
ID: 1249769
Sorry I cant load down your code and revised it for you 'cause I dont have any time at all, but I can give a good trick.
If your programm is under Turbo C, add a watch and type *(char*)0,4m . Then run the program step by step and you will see in the watch that this become 00 00 00 00. When the Null pointer assigment occurs it changes and takes any value. So try to see the error int this place.
See you ghendry.
0

Featured Post

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question