Solved

C Null pointer

Posted on 1997-03-26
5
697 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Convert image to byte array 8 173
Change to the struct 72 191
Difference between struct tm and time_t 5 102
Details to do the search 56 141
Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
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 while-loops in the C programming language.

763 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now