Sponsored Content
Top Forums Programming fread: segementation fault(coredump) w/o stdlib.h Post 302286560 by quintet on Wednesday 11th of February 2009 01:00:09 PM
Old 02-11-2009
fread: segementation fault(coredump) w/o stdlib.h

Hello All,

I tried to test a sample fread example to read a complete file
and the code is

Code:
#include <stdio.h>
#include <stdlib.h>
int main () {
  FILE * pFile;
  long lSize;
  char * buffer;
  size_t result;

  pFile = fopen ( "test.xml" , "rb" );
  if (pFile==NULL) {fputs ("File error",stderr); exit (1);}

  fseek (pFile , 0 , SEEK_END);
  lSize = ftell (pFile);
  rewind (pFile);

  buffer = (char*) malloc (sizeof(char)*lSize);
  if (buffer == NULL) {fputs ("Memory error",stderr); exit (2);}

  result = fread (buffer,1,lSize,pFile);
  if (result != lSize) {fputs ("Reading error",stderr); exit (3);}

  /* the whole file is now loaded in the memory buffer. */

  fclose (pFile);
  printf(" sizeof char :%d \n", sizeof(char));
  printf( "File: %s ", buffer);
  free (buffer);
  return 0;
}

The program worked as expected and the got the correct output.

After i removed #include <stdlib.h> from the program, it got compiled successfully but failed with the following message: Memory fault(coredump)

It failed in the fread command (checked with dbx).

Can anyone explain this behaviour?

I use AIX ver 5.

rgrds,
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Coredump (memory fault)

We are running a SQR program on Unix Platform with Oracle RDBMS. It's an interfacing program to integrate data from foreign sites to PeopleSoft database, using a flat file input. After many hours of processing, the program stops with a coredump error (memory fault). With top command we noticed... (1 Reply)
Discussion started by: araziki
1 Replies

2. Programming

Memory fault(coredump)

Dear All, I made a program which do some simple jobs like reading data from other process's shared memory and writing messages to the queues of other process. what happens is my program works fine and do all the task as expected but then then program ends it give Memory fault(coredump). I... (0 Replies)
Discussion started by: ralo
0 Replies

3. Shell Programming and Scripting

Awk:Segementation Fault

I am using awk command to sort a large file (7MB) and compare two files of 7 MB. The code is working fine for smaller files. But it gives following error for larger files Segmentation fault (core dumped) I am doing this on cygwin Please help in this regard Thanks Sandeep (1 Reply)
Discussion started by: sandeep_hi
1 Replies

4. Programming

Passed the compiler,but segementation fault

To find a number if it is a prime number, I cannot find any wrong. #include <stdio.h> #include <stdlib.h> int isPrime(int valToCheck) { int i; int retVal = 1; for(i=1;i<valToCheck;i++) { if(valToCheck%i == 0 && i != 1) { retVal = 0; ... (1 Reply)
Discussion started by: zhshqzyc
1 Replies

5. AIX

Segmentation fault(coredump)

Hi All Can anybody help me? When ever am trying to run topas system gives me an error Segmentation fault(coredump) does anybody ahve solution for this? (4 Replies)
Discussion started by: vjm
4 Replies

6. Ubuntu

Memory fault(coredump)

Hey guys, I am new to the Linux world and have a question to post. When I ssh from a HP-UX machine to a ubuntu machine I get the following error message Memory fault(coredump) i.e. ssh 192.168.1.3 I get this message as shown below Memory fault(coredump) Can someone please explain... (2 Replies)
Discussion started by: fkaba81
2 Replies

7. Solaris

memory fault (coredump)

Getting memory fault (coredump) and segmentation fault(coredump) when i tried javac or java -version. what could be the problem? Regards Eswar (2 Replies)
Discussion started by: BhuvanEswar
2 Replies

8. Shell Programming and Scripting

Segmentation Fault(coredump)

I'm getting this error when trying to run a Acucobol program thru UNIX.. Segmentation Fault(coredump) Precompiler error prevents compilation of xxxxxx.co. Please help me in this case.. (1 Reply)
Discussion started by: Manish4
1 Replies

9. Homework & Coursework Questions

Memory fault(coredump)

I am writing a program that copies a program and prints the program with a line count. this is the program I wrote: #include <stdio.h> main() { int c; int nl_cnt = 0; while((c = getchar()) != EOF){ if(c = '\n'){ nl_cnt++;... (3 Replies)
Discussion started by: heywoodfloyd
3 Replies

10. UNIX for Advanced & Expert Users

Memory fault(Coredump)

Hi, In my application we have one job which is used to process the files. But that job is failing with memory fault while processing a file or while shutting down the job. Sometime it generates the coredump and sometimes not. When I analysed the core dump I got below code snippet where it... (3 Replies)
Discussion started by: shilpa_20
3 Replies
GETLINE(3)						     Linux Programmer's Manual							GETLINE(3)

NAME
getline, getdelim - delimited string input SYNOPSIS
#include <stdio.h> ssize_t getline(char **lineptr, size_t *n, FILE *stream); ssize_t getdelim(char **lineptr, size_t *n, int delim, FILE *stream); Feature Test Macro Requirements for glibc (see feature_test_macros(7)): getline(), getdelim(): Since glibc 2.10: _POSIX_C_SOURCE >= 200809L Before glibc 2.10: _GNU_SOURCE DESCRIPTION
getline() reads an entire line from stream, storing the address of the buffer containing the text into *lineptr. The buffer is null-termi- nated and includes the newline character, if one was found. If *lineptr is set to NULL and *n is set 0 before the call, then getline() will allocate a buffer for storing the line. This buffer should be freed by the user program even if getline() failed. Alternatively, before calling getline(), *lineptr can contain a pointer to a malloc(3)-allocated buffer *n bytes in size. If the buffer is not large enough to hold the line, getline() resizes it with realloc(3), updating *lineptr and *n as necessary. In either case, on a successful call, *lineptr and *n will be updated to reflect the buffer address and allocated size respectively. getdelim() works like getline(), except that a line delimiter other than newline can be specified as the delimiter argument. As with get- line(), a delimiter character is not added if one was not present in the input before end of file was reached. RETURN VALUE
On success, getline() and getdelim() return the number of characters read, including the delimiter character, but not including the termi- nating null byte (''). This value can be used to handle embedded null bytes in the line read. Both functions return -1 on failure to read a line (including end-of-file condition). In the event of an error, errno is set to indicate the cause. ERRORS
EINVAL Bad arguments (n or lineptr is NULL, or stream is not valid). ENOMEM Allocation or reallocation of the line buffer failed. ATTRIBUTES
For an explanation of the terms used in this section, see attributes(7). +----------------------+---------------+---------+ |Interface | Attribute | Value | +----------------------+---------------+---------+ |getline(), getdelim() | Thread safety | MT-Safe | +----------------------+---------------+---------+ CONFORMING TO
Both getline() and getdelim() were originally GNU extensions. They were standardized in POSIX.1-2008. EXAMPLE
#define _GNU_SOURCE #include <stdio.h> #include <stdlib.h> int main(int argc, char *argv[]) { FILE *stream; char *line = NULL; size_t len = 0; ssize_t nread; if (argc != 2) { fprintf(stderr, "Usage: %s <file> ", argv[0]); exit(EXIT_FAILURE); } stream = fopen(argv[1], "r"); if (stream == NULL) { perror("fopen"); exit(EXIT_FAILURE); } while ((nread = getline(&line, &len, stream)) != -1) { printf("Retrieved line of length %zu: ", nread); fwrite(line, nread, 1, stdout); } free(line); fclose(stream); exit(EXIT_SUCCESS); } SEE ALSO
read(2), fgets(3), fopen(3), fread(3), scanf(3) COLOPHON
This page is part of release 4.15 of the Linux man-pages project. A description of the project, information about reporting bugs, and the latest version of this page, can be found at https://www.kernel.org/doc/man-pages/. GNU
2017-09-15 GETLINE(3)
All times are GMT -4. The time now is 07:30 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy