C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old May 7th, 2006, 06:35 PM
firestorm717 firestorm717 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2006
Posts: 2 firestorm717 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 16 m 56 sec
Reputation Power: 0
DMA Segmentation Faults

This is my first time using dynamic memory allocation, and I've been having some problems with segmentation faults being thrown in my current program when I start calling different functions repeatedly (it is a simple e-mail database that prints, search, adds, deletes, etc, but when I say...print, add, delete, and then call search, search will throw a fault, whereas if I call search from the beginning, it runs without a glitch). Anyone have any clue what the problem is? I think it might have something to do with passing the mail structure pointer or perhaps freeing memory (if I run several functions many times, the numbers stored in each entry in the mail pointer array start getting weird), but I've tried everything and am stumped.

In addition, I'm having trouble getting the sort function to work. I'm pretty sure the algorithm is correct, as it's simple bubblesort, and I've tested it with structures in main before, but for some reason, it refuses to sort correctly here. It doesn't seem to recognize and switch things correctly, though it loops all the way through. Again, any help would be appreciated. I'm a bit desperate at this point, as I've tried everything.

Reply With Quote
  #2  
Old May 7th, 2006, 06:36 PM
firestorm717 firestorm717 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2006
Posts: 2 firestorm717 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 16 m 56 sec
Reputation Power: 0
#include <stdio.h>
#include <string.h>

int size = 0;

// Mail structure to store each e-mail's information
struct mail {
int timeindex;
char month[4];
int date;
char *email;
int msgsize;
char *subject;
};

// Prints out e-mails in order currently stored in pointer
void print(struct mail * m1)
{
int i;
for(i = 0; i < size; i++)
{
printf("%3d ", i + 1);
printf("%s ", (m1 + i) -> month);
printf("%d ", (m1 + i) -> date);
printf("%s ", (m1 + i) -> email);
printf("%d ", (m1 + i) -> msgsize);
printf("%s\n", (m1 + i) -> subject);
}
}

// Sorts emails in ascending order by received time, sender, or message size
void sort(struct mail * m1)
{
int choice, i, pos;
struct mail key;

printf("***Sort Menu***\n");
printf("1. Sort by received time\n");
printf("2. Sort by sender\n");
printf("3. Sort by size\n");

// Read in user choice
do {
printf("Please enter your choice (1-3): ");
scanf("%d", &choice);
} while(choice < 1 || choice > 3);

// Sort in ascending order by time message was received
if(choice == 1)
{
for(i = 0; i < size; i++)
{
for(pos = i; pos < size; pos++)
{
if((m1 + i) -> timeindex > (m1 + pos) -> timeindex)
{
key = *(m1 + i);
*(m1 + i) = *(m1 + pos);
*(m1 + pos) = key;
}
}
}
}

// Sort alphabetically by sender e-mail
else if(choice == 2)
{
for(i = 0; i < size; i++)
{
for(i = 0; i < size; i++)
{
if(strcmp((m1 + i) -> email, (m1 + pos) -> email) > 0)
{
key = *(m1 + i);
*(m1 + i) = *(m1 + pos);
*(m1 + pos) = key;
}
}
}

}

// Sort in ascending order by message size
else if(choice == 3)
{
for(i = 0; i < size; i++)
{
for(pos = i; pos < size; pos++);
{
if((m1 + i) -> msgsize > (m1 + pos) -> msgsize)
{
key = *(m1 + i);
*(m1 + i) = *(m1 + pos);
*(m1 + pos) = key;
}
}
}
}
}

void add(struct mail * m1)
{
int i, space, check = 0;
int dtemp, stemp;
char ch, mtemp[4];
char *sendtemp = malloc(20 * sizeof(char));
char *subtemp = malloc(300 * sizeof(char));
struct mail * m1temp;

// Read in month (must be legal three-letter abbreviation)
do {
printf("the month the message is received (format: mmm): ");
scanf("%s", mtemp);
} while(strcmp(mtemp, "Jan") != 0 && strcmp(mtemp, "Feb") != 0 &&
strcmp(mtemp, "Mar") != 0 && strcmp(mtemp, "Apr") != 0 &&
strcmp(mtemp, "May") != 0 && strcmp(mtemp, "Jun") != 0 &&
strcmp(mtemp, "Jul") != 0 && strcmp(mtemp, "Aug") != 0 &&
strcmp(mtemp, "Sep") != 0 && strcmp(mtemp, "Oct") != 0 &&
strcmp(mtemp, "Nov") != 0 && strcmp(mtemp, "Dec") != 0);

// Read in date (must be 1 through 31)
do {
printf("the day the message is received (format: d or dd): ");
scanf("%d", &dtemp);
} while(dtemp < 1 || dtemp > 31);

// Read in size of message (must be positive integer)
do {
printf("the size of the message (format: integer): ");
scanf("%d", &stemp);
} while(stemp < 0);

// Read in sender e-mail (must be single string, no spaces)
do {
space = 0;
printf("the sender of the message (format: no spacing): ");
scanf("%s", sendtemp);

// Check to make sure entry is only one string
while(getchar() != '\n')
space++;
} while(space > 0);

// Read in subject title character by character into a pointer string
i = 0;
printf("the subject of the message (format: no restriction): ");
ch = getchar();
while(ch != '\n')
{
*(subtemp + i) = ch;
i++;
ch = getchar();
}

// Check to make sure entry is not identical to another entry in database
for(i = 0; i < size; i++)
{
if(strcmp(mtemp, (m1 + i) -> month) == 0 &&
dtemp == (m1 + i) -> date &&
stemp == (m1 + i) -> msgsize &&
strcmp(sendtemp, (m1 + i) -> email) == 0 &&
strcmp(subtemp, (m1 + i) -> subject) == 0)
check = 1;
}

// If entry is not identical, add to end of database and update size
if(check == 0)
{
m1 = (struct mail *) realloc(m1, (size + 1) * sizeof(m1temp));
(m1 + size) -> timeindex = size + 1;
for(i = 0; i < 4; i++)
(m1 + size) -> month[i] = mtemp[i];
(m1 + size) -> date = dtemp;
(m1 + size) -> msgsize = stemp;
(m1 + size) -> email = sendtemp;
(m1 + size) -> subject = subtemp;
size++;
printf("The message has been added successfully.\n");
}
// If entry already in database, print out failure message
else
printf("Message add fails. The message exists in the database.\n");
}

void delete(struct mail * m1)
{
int i, index;
// Read in index for deletion
do {
printf("Enter the index of the message to delete (1 to %d): ",
size);
scanf("%d", &index);
} while(index < 1 || index > size);

// Shift all entries after index to the left by one
for(i = index - 1; i < size - 1; i++)
*(m1 + i) = *(m1 + i + 1);

// Free last memory location and update size
free(m1 + size - 1);
size--;

printf("Message has been deleted successfully. ");
printf("There are %d messages left.\n", size);
}

void search(struct mail * m1)
{
char *s1;
int i, k, check, count = 0;
printf("Enter the keyword that you want to search: ");
scanf("%s", s1);

// Loop through entire e-mail database pointer array
for(i = 0; i < size; i++)
{
check = 0;
k = 0;

// While current entry's subject is not at end of string, compare each
// individual word to the keyword
while((m1 + i) -> subject[k] != '\0')
{
int j = 0;
// Increment indices while characters in both strings are equal
// and not at end of subject string
while(s1[j] == (m1 + i) -> subject[k] &&
(m1 + i) -> subject[k] != '\0')
{
j++;
k++;
}

// If length of keyword is equal to j (all characters of word
// match) and it is the end of the currently checked word in
// entry's subject string, increment check
if(strlen(s1) == j && ((m1 + i) -> subject[k] == ' ' ||
(m1 + i) -> subject[k] == '\0'))
check++;

// Increment subject index until beginning of next word in string
while((m1 + i) -> subject[k] != ' ' &&
(m1 + i) -> subject[k] != '\0')
k++;
if((m1 + i) -> subject[k] == ' ')
k++;
}

// If check is larger than 1, print out entry keyword was in
if(check > 0)
{
printf("%3d ", i + 1);
printf("%s ", (m1 + i) -> month);
printf("%d ", (m1 + i) -> date);
printf("%s ", (m1 + i) -> email);
printf("%d ", (m1 + i) -> msgsize);
printf("%s\n", (m1 + i) -> subject);
count++;
}
}

// If keyword not found in any entries, print out keyword not found
if(count == 0)
printf("Keyword %s not found.\n", s1);
}

// Print leave message and exit out of program
void leave()
{
printf("Thanks for using the ENEE 114 Email Database.\n");
printf("Goodbye!\n");
exit(0);
}

int main(int argc, char *argv[])
{
int i, choice;
char ch, temp;
FILE *input;
struct mail *m1, m2;

// Allocate dynamic memory for mail pointer array
m1 = (struct mail *) malloc(sizeof(m2));

// If not enough arguments in command line, print out error
if(argc != 2)
{
printf("Usage: executable input_file\n");
exit(0);
}

input = fopen(argv[1], "r");

// Check to make sure input file opened correctly
if(input == NULL)
{
printf("File %s cannot open!\n", argv[1]);
exit(0);
}

// Store input file e-mail information into mail database pointer array
while(fscanf(input, "%c", &temp) != EOF)
{
i = 0;

// Store time index to indicate when message was added
(m1 + size) -> timeindex = size + 1;

// Store month abbreviation in character by character
(m1 + size) -> month[0] = temp;
fscanf(input, "%c", &((m1 + size) -> month[1]));
fscanf(input, "%c", &((m1 + size) -> month[2]));
(m1 + size) -> month[3] = '\0';

// Store date in
fscanf(input, "%d", &((m1 + size) -> date));

// Allocate memory for e-mail string and store string
(m1 + size) -> email = malloc(20 * sizeof(char));
fscanf(input, "%s", (m1 + size) -> email);

// Store message size in
fscanf(input, "%d", &((m1 + size) -> msgsize));

// Skip blank space
fscanf(input, "%c", &ch);

// Allocate memory for subject string and store in character by
// character until at end of current line
(m1 + size) -> subject = malloc(300 * sizeof(char));
while(fscanf(input, "%c", &ch) != EOF && ch != '\n')
{
*((m1 + size) -> subject + i) = ch;
i++;
}

// Update size and reallocate mail pointer to larger memory location
size++;
m1 = (struct mail *) realloc(m1, (size + 1) * sizeof(m2));
}

// Display menu and call appropriate function for entered choice
printf("Welcome to ENEE 114 Email Database\n");
do {
printf("***Main Menu***\n");
printf("1. Print\n");
printf("2. Sort\n");
printf("3. Add Message\n");
printf("4. Delete Message\n");
printf("5. Keyword Search\n");
printf("6. Exit\n");
printf("\nPlease enter your choice (1-6): ");
scanf("%d", &choice);

switch(choice)
{
case 1: print(m1); break;
case 2: sort(m1); break;
case 3: add(m1); break;
case 4: delete(m1); break;
case 5: search(m1); break;
default: leave(); break;
}
} while(choice != 6);

free(m1);
free(m2);
fclose(input);

return 0;
}

Reply With Quote
  #3  
Old May 7th, 2006, 07:50 PM
ubergeek ubergeek is offline
Contributing User
Dev Articles Novice (500 - 999 posts)
 
Join Date: Jan 2005
Posts: 600 ubergeek User rank is Private First Class (20 - 50 Reputation Level)ubergeek User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Days 22 h 40 m 27 sec
Reputation Power: 4
Send a message via AIM to ubergeek
I don't immediately see any problems with your code. In situations like this, have your code tell you what is wrong. Put more printf statements within the code, basically dumping your variables at points where you're not sure what is going wrong. Then you can pinpoint exactly where the problem is occurring.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > DMA Segmentation Faults


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

Request Your Free Technology Downloads!
 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

Request Your Free Technology Downloads!
 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

Request Your Free Technology Downloads!
 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

Request Your Free Technology Downloads!
 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

Request Your Free Technology Downloads!
 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
Stay green...Green IT