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
Welcome to
LinuxQuestions.org , a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free.
Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please
contact us . If you need to reset your password,
click here .
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a
virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month.
Click here for more info.
05-11-2010, 10:37 PM
#1
Member
Registered: Apr 2010
Posts: 144
Rep:
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 .
05-12-2010, 12:45 AM
#2
Member
Registered: May 2003
Location: Bangalore ,Karnataka, India, Asia, Earth, Solar system, milky way galaxy, black hole
Distribution: murugesan openssl
Posts: 181
Rep:
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.
05-12-2010, 01:20 AM
#3
Senior Member
Registered: Dec 2008
Posts: 4,731
915086731
and kindly put some
code tags and indent your code , i can't still read your code !!
05-12-2010, 01:59 AM
#4
Member
Registered: May 2003
Location: Bangalore ,Karnataka, India, Asia, Earth, Solar system, milky way galaxy, black hole
Distribution: murugesan openssl
Posts: 181
Rep:
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.
05-12-2010, 02:03 AM
#5
Senior Member
Registered: Dec 2008
Posts: 4,731
Thanks for bothering, Dinesh
05-12-2010, 02:42 AM
#6
Member
Registered: May 2003
Location: Bangalore ,Karnataka, India, Asia, Earth, Solar system, milky way galaxy, black hole
Distribution: murugesan openssl
Posts: 181
Rep:
Welcome.
05-12-2010, 06:27 AM
#7
Member
Registered: Apr 2010
Posts: 144
Original Poster
Rep:
how can I init buff?
char **buff
how can I init buff?
05-12-2010, 06:34 AM
#8
Senior Member
Registered: Dec 2008
Posts: 4,731
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.
05-12-2010, 06:40 AM
#9
Senior Member
Registered: Dec 2008
Posts: 4,731
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));
05-12-2010, 07:27 AM
#10
LQ Guru
Registered: Dec 2007
Distribution: Centos
Posts: 5,286
Quote:
Originally Posted by
915086731
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.
05-12-2010, 09:45 PM
#11
LQ Veteran
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809
Moved to Programming
05-12-2010, 10:03 PM
#12
Senior Member
Registered: May 2005
Posts: 4,481
Quote:
Originally Posted by
915086731
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').
All times are GMT -5. The time now is 02:25 AM .
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know .
Latest Threads
LQ News