Solved

Null pointer assignment

Posted on 1998-09-24
1
378 Views
Last Modified: 2012-05-04

/***** Program Merge many files to one file *****/

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<io.h>
#include<dos.h>
#include<string.h>
#include <fcntl.h>
#include <sys\stat.h>

#define BUFSIZE 54

int main(int argc,char *argv[])
{
  extern int errno;
  void *point;
  FILE *fpw,*fpr;
  int  count;
  int  handle;
  int  errchk;
  long  filesize;

  for(count=1;count<argc-1;++count)
  {
    free(point);
    handle = open(argv[count],O_RDONLY|O_TEXT);
    filesize = filelength(handle)-3;
    printf("\n%d----%d\n",handle,filelength(handle));
    if((point = malloc(filelength(handle))) == NULL)
    {
      printf("Not enough memory to allocate buffer\n");
      exit(1);  /* terminate program if out of memory */
    }


    /****** READ FROM SOURCE FILE ******/
    if((errchk = read(handle,point,filelength(handle))) == -1)
    {
      printf("Read Failed.\n ----%d",errno);
      exit(1);
    }
    else
    {
      close(handle);
    }


 /****** WRITE TO TARGET FILE ******/

    if((fpw = fopen(argv[argc-1],"a+t")) == NULL)
    {
      fprintf(stderr, "Cannot open input file.\n");
      return(1);
    }
    else
    {
      fwrite(point,filesize,1,fpw);
      fclose(fpw);
      free(point);
    }
  }
  return(0);
}
 Above is my merging program source code for merge detail in many files to one file.It work completely but after close target file appear message "Null pointer assignment" on screen.Can you correct this error message or any error may have on source code ?.

0
Comment
Question by:dissathep
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 2

Accepted Solution

by:
JYoungman earned 50 total points
ID: 1253025
You've made several mistakes.  The "null pointer assignment" is just the most obvious symtom of the bugs.  

You do free(point) twice with the same value.  Doing this a second time is a bug.  The first time round your main loop, you do free(point) without initialising point to anything other than its default random value.   This will be what causes that message.

There are other problems with this code, too.

You didn't check the return value of filelength() for a failure status (it can fail, can't it?)

>> printf("\n%d----%d\n",handle,filelength(handle));

If filelength() returns a long, the second format should be %ld.
If it does return a long and a negative value means failure,
you should not pass this value directly to malloc(), because the argument of malloc() is an unsigned quantity.

You do not need to include <conio.h>, nor <io.h>, and probably not <dos.h>.

>>  printf("\n%d----%d\n",handle,filelength(handle));

Use perror() instead.

Lastly, there is no reason to malloc() a buffer big enough to read the whole input file into, you could just read it in piecewise, appending data to the output file as you go. There is no need to reopen the output file once each time round the loop.  You could just open it once at the start and close it at the end.

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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 opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

749 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