LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-11-2010, 10:37 PM   #1
915086731
Member
 
Registered: Apr 2010
Posts: 144
Blog Entries: 6

Rep: Reputation: 2
Smile main() function returns a value, why does it cause a " core dump "?


Thanks for your help!
I compiled all the file. and ran it . The result is:

[saturn@localhost compile1]$ ./a.out
if data+92>0x3f then
data=data+01;
else
data=data-01;

scanfile.c exits
main.c exits
段错误 (core dumped)

Why does me get a "core dumped" ? Help!


My main.c is:
#include "compile1.h"

int main( int argc, char* argv[]){
char* pathname = "./a.txt";
scanfile( pathname );
printf("main.c exits \n" );
return 0; //program core dumped after return
}

scanfile( *path ) function is in scanfile.c .
scanfile.c is :

#include "compile1.h"

int scanfile(char* pathname){
char **mem;
long int size;
size = readfile( pathname, mem );
if( size < 0) {
printf("%s\n", error(ERROR));
}
printf( "%s", *mem );
free( *mem );
printf ("scanfile.c exits\n" );
return 0;
}

readfile( char *path, char** mem ) function is in filedeal.c
filedeal.c is:

#include "compile1.h"

/* read file specified by pathname, and store the
* content to the mem pointed by mem. Return the
* size after reading.
*/
long readfile( char* pathname, char** mem ){
long int size; //文件长度
char* buff;
if( access( pathname, W_OK) < 0 ){
ERROR = -1;
return -1;
}
FILE* fp;
fp = fopen( pathname, "r");
if( fp == NULL ){
ERROR = -2;
return -1;
}
fseek( fp, 0, SEEK_END );
size = ftell(fp); //取得长度
fseek( fp, 0, SEEK_SET );
buff = (char*) malloc(size);
if( fread( buff, 1, size, fp) != size ) {
ERROR = -3;
return -1;
}
*mem = buff;
return size;
}

a.txt is :

if data+92>0x3f then
data=data+01;
else
data=data-01;

Last edited by 915086731; 05-11-2010 at 10:44 PM.
 
Old 05-12-2010, 12:45 AM   #2
murugesan
Member
 
Registered: May 2003
Location: Bangalore ,Karnataka, India, Asia, Earth, Solar system, milky way galaxy, black hole
Distribution: murugesan openssl
Posts: 181

Rep: Reputation: 29
The main reason is because of the following codes:
char **mem; /* not initialized */


*mem = buff; /* Here it core dumps */
 
1 members found this post helpful.
Old 05-12-2010, 01:20 AM   #3
Aquarius_Girl
Senior Member
 
Registered: Dec 2008
Posts: 4,731
Blog Entries: 29

Rep: Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940
915086731

and kindly put some code tags and indent your code , i can't still read your code !!
 
Old 05-12-2010, 01:59 AM   #4
murugesan
Member
 
Registered: May 2003
Location: Bangalore ,Karnataka, India, Asia, Earth, Solar system, milky way galaxy, black hole
Distribution: murugesan openssl
Posts: 181

Rep: Reputation: 29
From 915086731:
Code:
$ ./a.out
if data+92>0x3f then
data=data+01;
else
data=data-01;

scanfile.c exits
main.c exits
段错误 (core dumped)

Why does me get a "core dumped" ? Help!


My main.c is:
#include "compile1.h"

int main( int argc, char* argv[]){
char* pathname = "./a.txt";
scanfile( pathname );
printf("main.c exits \n" );
return 0; //program core dumped after return
}

scanfile( *path ) function is in scanfile.c .
scanfile.c is :

#include "compile1.h"

int scanfile(char* pathname){
char **mem;
long int size;
size = readfile( pathname, mem );
if( size < 0) {
printf("%s\n", error(ERROR));
}
printf( "%s", *mem );
free( *mem );
printf ("scanfile.c exits\n" );
return 0;
}

readfile( char *path, char** mem ) function is in filedeal.c
filedeal.c is:

#include "compile1.h"

/* read file specified by pathname, and store the
* content to the mem pointed by mem. Return the
* size after reading.
*/
long readfile( char* pathname, char** mem ){
long int size; //文件长度
char* buff;
if( access( pathname, W_OK) < 0 ){
ERROR = -1;
return -1;
}
FILE* fp;
fp = fopen( pathname, "r");
if( fp == NULL ){
ERROR = -2;
return -1;
}
fseek( fp, 0, SEEK_END );
size = ftell(fp); //取得长度
fseek( fp, 0, SEEK_SET );
buff = (char*) malloc(size);
if( fread( buff, 1, size, fp) != size ) {
ERROR = -3;
return -1;
}
*mem = buff;
return size;
}

a.txt is :

if data+92>0x3f then
data=data+01;
else
data=data-01;

Reason on core:
Code:
char **mem; /* not initialized */
*mem = buff; /* Here it core dumps */

Example:
cat core.C
Code:
#include <stdio.h>
int main()
{
        char **coreDump ;
        printf( "Starting here") ;
        *coreDump ="Dump here" ;
        printf( "Not reached here") ;
        return 0 ;
}
Compilation and core file information:
Code:
/usr/bin/g++ -g core.C -o a.out
ulimit -c unlimited
file a.out
Code:
a.out: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), for GNU/Linux 2.2.5, dynamically linked (uses shared libs), not stripped
OUTPUT:
Code:
./a.out
Memory fault (core dumped)

Information from the core file:
Code:
$ file core.30699
core.30699: ELF 32-bit LSB core file Intel 80386, version 1 (SYSV), SVR4-style, from 'a.out'
Information obtained using gdb with the core file:
Code:
gdb a.out  core.30699
(gdb) where
#0  0x08048505 in main () at core.C:6
(gdb) list core.C:6
1       #include <stdio.h>
2       int main()
3       {
4               char **coreDump ;
5               printf( "Starting here") ;
6               *coreDump ="Dump here" ;
7               printf( "Not reached here") ;
8               return 0 ;
9       }
 
2 members found this post helpful.
Old 05-12-2010, 02:03 AM   #5
Aquarius_Girl
Senior Member
 
Registered: Dec 2008
Posts: 4,731
Blog Entries: 29

Rep: Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940
Thanks for bothering, Dinesh
 
Old 05-12-2010, 02:42 AM   #6
murugesan
Member
 
Registered: May 2003
Location: Bangalore ,Karnataka, India, Asia, Earth, Solar system, milky way galaxy, black hole
Distribution: murugesan openssl
Posts: 181

Rep: Reputation: 29
Welcome.
 
Old 05-12-2010, 06:27 AM   #7
915086731
Member
 
Registered: Apr 2010
Posts: 144

Original Poster
Blog Entries: 6

Rep: Reputation: 2
Smile how can I init buff?

char **buff
how can I init buff?
 
Old 05-12-2010, 06:34 AM   #8
Aquarius_Girl
Senior Member
 
Registered: Dec 2008
Posts: 4,731
Blog Entries: 29

Rep: Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940
See if the following code helps you somehow ?
Code:
buff = new char*[4];

for (int i=0; i<4; i++)
{
     buff [i]=new char[20];
}
new is used in c++ you can use malloc instead if working in c.
 
Old 05-12-2010, 06:40 AM   #9
Aquarius_Girl
Senior Member
 
Registered: Dec 2008
Posts: 4,731
Blog Entries: 29

Rep: Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940
Following is for integers:
Do read this: http://www.lysator.liu.se/c/c-faq/c-2.html
Code:
int **array1 = (int **)malloc(nrows * sizeof(int *));
	for(i = 0; i < nrows; i++)
		array1[i] = (int *)malloc(ncolumns * sizeof(int));
 
Old 05-12-2010, 07:27 AM   #10
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by 915086731 View Post
Code:
    char **mem;
...
    size = readfile( pathname, mem );        
...
    printf( "%s", *mem );
    free( *mem );
If I understand correctly, mem is meant to hold a single char* (not an array of char*).

So your code would be less confusing if you dropped down one level of indirection for all uses of mem in scanfile, while leaving readfile unchanged at its current level of indirection on mem. So the code I quoted above would change to:

Code:
    char *mem;
...
    size = readfile( pathname, &mem );        
...
    printf( "%s", mem );
    free( mem );

Alternately, if you don't want to change the level of indirection of mem, the quoted code should change to:

Code:
    char **mem = malloc( sizeof(char*) );
...
    size = readfile( pathname, mem );        
...
    printf( "%s", *mem );
    free( *mem );
    free( mem )
I prefer changing the level of indirection. But either way should work.

Last edited by johnsfine; 05-12-2010 at 07:32 AM.
 
1 members found this post helpful.
Old 05-12-2010, 09:45 PM   #11
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Moved to Programming
 
Old 05-12-2010, 10:03 PM   #12
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by 915086731 View Post
Thanks for your help!
I compiled all the file. and ran it . The result is:

[saturn@localhost compile1]$ ./a.out
if data+92>0x3f then
data=data+01;
else
data=data-01;

scanfile.c exits
main.c exits
段错误 (core dumped)

Why does me get a "core dumped" ? Help!


My main.c is:
#include "compile1.h"

int main( int argc, char* argv[]){
char* pathname = "./a.txt";
scanfile( pathname );
printf("main.c exits \n" );
return 0; //program core dumped after return
}

scanfile( *path ) function is in scanfile.c .
scanfile.c is :

#include "compile1.h"

int scanfile(char* pathname){
char **mem;
long int size;
size = readfile( pathname, mem );
if( size < 0) {
printf("%s\n", error(ERROR));
}
printf( "%s", *mem );
free( *mem );
printf ("scanfile.c exits\n" );
return 0;
}

readfile( char *path, char** mem ) function is in filedeal.c
filedeal.c is:

#include "compile1.h"

/* read file specified by pathname, and store the
* content to the mem pointed by mem. Return the
* size after reading.
*/
long readfile( char* pathname, char** mem ){
long int size; //文件长度
char* buff;
if( access( pathname, W_OK) < 0 ){
ERROR = -1;
return -1;
}
FILE* fp;
fp = fopen( pathname, "r");
if( fp == NULL ){
ERROR = -2;
return -1;
}
fseek( fp, 0, SEEK_END );
size = ftell(fp); //取得长度
fseek( fp, 0, SEEK_SET );
buff = (char*) malloc(size);
if( fread( buff, 1, size, fp) != size ) {
ERROR = -3;
return -1;
}
*mem = buff;
return size;
}

a.txt is :

if data+92>0x3f then
data=data+01;
else
data=data-01;
First and foremost: compile with

-Wall -Wextra

(if it's 'gcc').
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Command "mail" returns "panic: temporary file seek" kenneho Linux - Software 5 12-23-2008 03:27 AM
installing FreeBSD on Virtual Box error: "Cannot dump. No dump device defined" Valkyrie_of_valhalla *BSD 4 09-06-2007 04:02 AM
Exception in thread "main" java.lang.ClassFormatError: onmyown.Main (unrecognized cla zimboney Fedora 4 07-20-2007 01:00 PM
K3b: - Howto re-dock "Directories" and "Contents" windows back into the main window? hagies Linux - Software 4 04-26-2006 08:38 AM
"so" file + main function bianchi Programming 3 02-02-2006 01:52 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:25 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration