LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
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 03-25-2008, 02:02 AM   #1
return.c
LQ Newbie
 
Registered: Mar 2008
Distribution: Fedora
Posts: 11

Rep: Reputation: 0
Segmentation fault in accessing an unsigned long int


I am getting an "segmentation fault" inside the function "include_file", when I execute the line "digest.size = 0; " . what could be the problem ? If I comment the above line, there is no problem.

Error:
/bin/sh: line 1: 27058 Segmentation fault

the below is the code:


class ee_sha1class
{
private:
unsigned char *m_text;
SHA_CTX SHA1_state;
struct digest_info
{
const char *filename;
char *text;
unsigned char *mdigest;
unsigned long int size;
};
struct digest_info digest;

public:
int calculate();
int printdigest();
int input_file(const char *filename);
ee_sha1class();
};


ee_sha1class :: ee_sha1class()
{
digest.filename = (const char *) malloc(sizeof(char) * 10240);
digest.mdigest = (unsigned char *) malloc(sizeof(char) * (16*2 +1));

bzero((void*)digest.filename, 10240);
bzero(digest.mdigest, 20);
digest.size = 0;
}
int ee_sha1class :: calculate()
{
SHA1((unsigned char*)m_text, file_size, digest.mdigest);
}

int ee_sha1class :: input_file(const char *filename)
{
int fp, result = 512;
char *temp_char;
temp_char = (char*)malloc(sizeof(char) * 512);

digest.size = 0; // "Segmentation fault" error at this point

// calculate size

fp = open(filename, O_RDONLY);
printf("open: %d \n", fp);
while(result == 512)
{
result = read(fp, temp_char, 512);
*(temp_char + result) = '\0';
}
close(fp);
}
 
Old 03-25-2008, 03:16 AM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
I'm confused ... your code is something that looks like a C/C++
hybrid and you're seeing a segfault in a bash script?



Cheers,
Tink
 
Old 03-25-2008, 03:40 AM   #3
return.c
LQ Newbie
 
Registered: Mar 2008
Distribution: Fedora
Posts: 11

Original Poster
Rep: Reputation: 0
yup this is an C++ code and I am using KDevelop IDE for coding.
 
  


Reply



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
printf unsigned long long int? blackzone Programming 9 03-04-2008 12:41 PM
How to print unsigned long int alaios Programming 6 07-14-2005 10:27 AM
convert unsigned char * to unsigned long int linux_lover2005 Programming 3 04-26-2005 11:38 PM
64 bit CPU unsigned long int GodSendDeath Programming 8 03-29-2005 01:19 PM
int value larger then unsigned long jpc82 Programming 3 03-12-2004 03:54 PM

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

All times are GMT -5. The time now is 01:12 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