In terms of literal memory (re)allocations or everything that involves it?
variable declarations, (re)allocations, free(), stuff in functions, etc. Storing to it isn't needed (imho)
- KermMartian
- Site Admin (Posts: 64067)
- 03 May 2011 11:38:00 pm
- Last edited by KermMartian on 03 May 2011 11:47:44 pm; edited 1 time in total
Always be sure to check the result of malloc:
Code:
Code:
if (NULL == (myvar = malloc( size statement))) {
perror("Failed to allocate memory! Terminating.");
exit(-1);
}
Uh.... beyond the initial malloc I've got:
Code:
in a section of a function that executes whenever you run out of array elements. It's designed the way it is so the number of calls is some inverse proportion to the amount of elements you need (essentially, the more elements you need, the fewer times you should even have to execute this, sort of).
Beyond that, I've got some scanfs inputting into recordArray[current_element].[struct_var]s and some bubble sorting going on. Not sure what else you want me to post code for, sorry ^^;
@Kerm will do. For the record, I don't think there's a malloc error going on because the program hasn't gone all kablooie on me when I enter records into the array.
[edit] the program *would* go kablooie and segfault if you tried storing to a pointer when malloc failed, right?
Code:
max_entries *= 2;
recordArray = realloc( recordArray, max_entries*sizeof(recordArray) );
in a section of a function that executes whenever you run out of array elements. It's designed the way it is so the number of calls is some inverse proportion to the amount of elements you need (essentially, the more elements you need, the fewer times you should even have to execute this, sort of).
Beyond that, I've got some scanfs inputting into recordArray[current_element].[struct_var]s and some bubble sorting going on. Not sure what else you want me to post code for, sorry ^^;
@Kerm will do. For the record, I don't think there's a malloc error going on because the program hasn't gone all kablooie on me when I enter records into the array.
[edit] the program *would* go kablooie and segfault if you tried storing to a pointer when malloc failed, right?
On EFNet IRC: Whenever | Nick: Su-Hime | Channels: #Cemetech, #UnSS
TI-83+/84+ Projects
Yumé 2: Rika's Story - 25% Complete; Will finish to coincide with Y:tED
PC Projects
Yumé: The Eternal Dreams - rewriting environment system again
TIBiC/GO - 1%; compiling list of commands
"Alenda lux ubi orta libertas" - Let learning be cherished where liberty has arisen.
"Aut viam inveniam aut faciam" - Either I shall find a way, or I shall make one. - Hannibal
TI-83+/84+ Projects
Yumé 2: Rika's Story - 25% Complete; Will finish to coincide with Y:tED
PC Projects
Yumé: The Eternal Dreams - rewriting environment system again
TIBiC/GO - 1%; compiling list of commands
"Alenda lux ubi orta libertas" - Let learning be cherished where liberty has arisen.
"Aut viam inveniam aut faciam" - Either I shall find a way, or I shall make one. - Hannibal
*Storing to the memory that the pointer points at, yes. It sounds most likely to me that your bubble sorting flubs the pointer manipulation, no?
KermMartian wrote:
*Storing to the memory that the pointer points at, yes. It sounds most likely to me that your bubble sorting flubs the pointer manipulation, no?
Probably, but why did all of that blurt out on exit rather than right when bubble sorting started?
On EFNet IRC: Whenever | Nick: Su-Hime | Channels: #Cemetech, #UnSS
TI-83+/84+ Projects
Yumé 2: Rika's Story - 25% Complete; Will finish to coincide with Y:tED
PC Projects
Yumé: The Eternal Dreams - rewriting environment system again
TIBiC/GO - 1%; compiling list of commands
"Alenda lux ubi orta libertas" - Let learning be cherished where liberty has arisen.
"Aut viam inveniam aut faciam" - Either I shall find a way, or I shall make one. - Hannibal
TI-83+/84+ Projects
Yumé 2: Rika's Story - 25% Complete; Will finish to coincide with Y:tED
PC Projects
Yumé: The Eternal Dreams - rewriting environment system again
TIBiC/GO - 1%; compiling list of commands
"Alenda lux ubi orta libertas" - Let learning be cherished where liberty has arisen.
"Aut viam inveniam aut faciam" - Either I shall find a way, or I shall make one. - Hannibal
*bump*
So, say I have my array'o'structs and I want to swap two structs if one's ID element is larger (aka a bubble sort) Is this valid?
Code:
So, say I have my array'o'structs and I want to swap two structs if one's ID element is larger (aka a bubble sort) Is this valid?
Code:
struct studentRecord backup;
int i;
[a few other lines later]
for (i = 0; i < current; i++)
{
if (recordArray[i].ID > recordArray[i+1].ID)
{
backup = recordArray[i];
recordArray[i] = recordArray[i+1];
recordArray[i+1] = backup;
}
}
On EFNet IRC: Whenever | Nick: Su-Hime | Channels: #Cemetech, #UnSS
TI-83+/84+ Projects
Yumé 2: Rika's Story - 25% Complete; Will finish to coincide with Y:tED
PC Projects
Yumé: The Eternal Dreams - rewriting environment system again
TIBiC/GO - 1%; compiling list of commands
"Alenda lux ubi orta libertas" - Let learning be cherished where liberty has arisen.
"Aut viam inveniam aut faciam" - Either I shall find a way, or I shall make one. - Hannibal
TI-83+/84+ Projects
Yumé 2: Rika's Story - 25% Complete; Will finish to coincide with Y:tED
PC Projects
Yumé: The Eternal Dreams - rewriting environment system again
TIBiC/GO - 1%; compiling list of commands
"Alenda lux ubi orta libertas" - Let learning be cherished where liberty has arisen.
"Aut viam inveniam aut faciam" - Either I shall find a way, or I shall make one. - Hannibal
-
merthsoft
- Administrator Emeritus (Posts: 3837)
- 04 May 2011 01:22:51 pm
- Last edited by merthsoft on 04 May 2011 01:25:59 pm; edited 2 times in total
It looks like it should work. Is it not working? What's it doing? I wonder if maybe it'd be better to use pointer for this.
It works from what I can observe. I just wanted to check and make sure it's not one of those "works but will make things explode" methods. Still can't figure out why that gobbledegook gets thrown out when I end the program after sorting.
On EFNet IRC: Whenever | Nick: Su-Hime | Channels: #Cemetech, #UnSS
TI-83+/84+ Projects
Yumé 2: Rika's Story - 25% Complete; Will finish to coincide with Y:tED
PC Projects
Yumé: The Eternal Dreams - rewriting environment system again
TIBiC/GO - 1%; compiling list of commands
"Alenda lux ubi orta libertas" - Let learning be cherished where liberty has arisen.
"Aut viam inveniam aut faciam" - Either I shall find a way, or I shall make one. - Hannibal
TI-83+/84+ Projects
Yumé 2: Rika's Story - 25% Complete; Will finish to coincide with Y:tED
PC Projects
Yumé: The Eternal Dreams - rewriting environment system again
TIBiC/GO - 1%; compiling list of commands
"Alenda lux ubi orta libertas" - Let learning be cherished where liberty has arisen.
"Aut viam inveniam aut faciam" - Either I shall find a way, or I shall make one. - Hannibal
If I comment out the sort, it doesn't show up. If I leave the sort in but don't use the sort options, it doesn't show up. There's something up with the sort, apparently, and I'm not sure what.
On EFNet IRC: Whenever | Nick: Su-Hime | Channels: #Cemetech, #UnSS
TI-83+/84+ Projects
Yumé 2: Rika's Story - 25% Complete; Will finish to coincide with Y:tED
PC Projects
Yumé: The Eternal Dreams - rewriting environment system again
TIBiC/GO - 1%; compiling list of commands
"Alenda lux ubi orta libertas" - Let learning be cherished where liberty has arisen.
"Aut viam inveniam aut faciam" - Either I shall find a way, or I shall make one. - Hannibal
TI-83+/84+ Projects
Yumé 2: Rika's Story - 25% Complete; Will finish to coincide with Y:tED
PC Projects
Yumé: The Eternal Dreams - rewriting environment system again
TIBiC/GO - 1%; compiling list of commands
"Alenda lux ubi orta libertas" - Let learning be cherished where liberty has arisen.
"Aut viam inveniam aut faciam" - Either I shall find a way, or I shall make one. - Hannibal
Code:
void sortRecords ( void )
{
if (current > 0)
{
int choice = 0;
printf("Please type the number of your selection and press the enter key.\n");
printf("1 Student ID Number\n");
printf("2 Student First Name\n");
printf("3 Student Last Name\n");
printf("4 Student GPA\n");
printf("5 Exit to Main Menu\n\n");
scanf("%d", &choice);
struct studentRecord backup;
int i;
/*
if (choice == 1)
{
for (i = 0; i < current; i++)
{
if (recordArray[i].ID > recordArray[i+1].ID)
{
backup = recordArray[i];
recordArray[i] = recordArray[i+1];
recordArray[i+1] = backup;
}
}
}
else if (choice == 2)
{
for (i = 0; i < current; i++)
{
if (strcmp(recordArray[i].firstName, recordArray[i+1].firstName) > 0)
{
backup = recordArray[i];
recordArray[i] = recordArray[i+1];
recordArray[i+1] = backup;
}
}
}
else if (choice == 3)
{
for (i = 0; i < current; i++)
{
if (strcmp(recordArray[i].lastName, recordArray[i+1].lastName) > 0)
{
backup = recordArray[i];
recordArray[i] = recordArray[i+1];
recordArray[i+1] = backup;
}
}
}
else if (choice == 4)
{
for (i = 0; i < current; i++)
{
if (recordArray[i].GPA > recordArray[i+1].GPA)
{
backup = recordArray[i];
recordArray[i] = recordArray[i+1];
recordArray[i+1] = backup;
}
}
}
*/
printf("Display Newly Sorted Entries? YES = 1 NO = 0\n\n");
scanf("%d", &choice);
if (choice == 1)
{
for (i=0; i <= current; i++)
{
printf("Student ID: %d\n", recordArray[i].ID);
printf("Student First Name: %s\n", recordArray[i].firstName);
printf("Student Last Name: %s\n", recordArray[i].lastName);
printf("Student Major: %s\n", recordArray[i].major);
printf("Student GPA: %f\n\n", recordArray[i].GPA);
}
}
}
else
{
printf("There are not enough entries for the sorting process. Returning to main menu...\n\n");
}
}
All of the program related to bubblesort. Variable current is the maximum number of entered records. The first bit checks to see if you have at least 2 records to sort, otherwise it informs you of your lack of entries and quits to the main menu.
I left the sorting process commented to emphasize it.
On EFNet IRC: Whenever | Nick: Su-Hime | Channels: #Cemetech, #UnSS
TI-83+/84+ Projects
Yumé 2: Rika's Story - 25% Complete; Will finish to coincide with Y:tED
PC Projects
Yumé: The Eternal Dreams - rewriting environment system again
TIBiC/GO - 1%; compiling list of commands
"Alenda lux ubi orta libertas" - Let learning be cherished where liberty has arisen.
"Aut viam inveniam aut faciam" - Either I shall find a way, or I shall make one. - Hannibal
TI-83+/84+ Projects
Yumé 2: Rika's Story - 25% Complete; Will finish to coincide with Y:tED
PC Projects
Yumé: The Eternal Dreams - rewriting environment system again
TIBiC/GO - 1%; compiling list of commands
"Alenda lux ubi orta libertas" - Let learning be cherished where liberty has arisen.
"Aut viam inveniam aut faciam" - Either I shall find a way, or I shall make one. - Hannibal
merthsoft wrote:
Huh, I dunno. Other than the "bubble sort" only doing one pass on the data.
I wanted to make sure the bubble sort even worked before I made it iterate. `-`
I am concerned but I guess I'll just ignore it for now...
On EFNet IRC: Whenever | Nick: Su-Hime | Channels: #Cemetech, #UnSS
TI-83+/84+ Projects
Yumé 2: Rika's Story - 25% Complete; Will finish to coincide with Y:tED
PC Projects
Yumé: The Eternal Dreams - rewriting environment system again
TIBiC/GO - 1%; compiling list of commands
"Alenda lux ubi orta libertas" - Let learning be cherished where liberty has arisen.
"Aut viam inveniam aut faciam" - Either I shall find a way, or I shall make one. - Hannibal
TI-83+/84+ Projects
Yumé 2: Rika's Story - 25% Complete; Will finish to coincide with Y:tED
PC Projects
Yumé: The Eternal Dreams - rewriting environment system again
TIBiC/GO - 1%; compiling list of commands
"Alenda lux ubi orta libertas" - Let learning be cherished where liberty has arisen.
"Aut viam inveniam aut faciam" - Either I shall find a way, or I shall make one. - Hannibal
TsukasaZX wrote:
It works from what I can observe. I just wanted to check and make sure it's not one of those "works but will make things explode" methods. Still can't figure out why that gobbledegook gets thrown out when I end the program after sorting.
How about running a few thousand iterations using a bash script and making sure it operates properly every time?
I finished the program and, upon executing it and running through all of its functionality thoroughly, the gobbledegook disappeared. My professor marked me for full credit I believe.
At this point, I think I'll just forget about it unless a future program has a similar issue.
tl;dr: not learning bash right now, maybe later.
At this point, I think I'll just forget about it unless a future program has a similar issue.
tl;dr: not learning bash right now, maybe later.
On EFNet IRC: Whenever | Nick: Su-Hime | Channels: #Cemetech, #UnSS
TI-83+/84+ Projects
Yumé 2: Rika's Story - 25% Complete; Will finish to coincide with Y:tED
PC Projects
Yumé: The Eternal Dreams - rewriting environment system again
TIBiC/GO - 1%; compiling list of commands
"Alenda lux ubi orta libertas" - Let learning be cherished where liberty has arisen.
"Aut viam inveniam aut faciam" - Either I shall find a way, or I shall make one. - Hannibal
TI-83+/84+ Projects
Yumé 2: Rika's Story - 25% Complete; Will finish to coincide with Y:tED
PC Projects
Yumé: The Eternal Dreams - rewriting environment system again
TIBiC/GO - 1%; compiling list of commands
"Alenda lux ubi orta libertas" - Let learning be cherished where liberty has arisen.
"Aut viam inveniam aut faciam" - Either I shall find a way, or I shall make one. - Hannibal
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.
» Go to Registration page
» Go to Registration page
» Goto page Previous 1, 2, 3, 4, 5, 6
» View previous topic :: View next topic
» View previous topic :: View next topic
Page 6 of 6
» All times are UTC - 5 Hours
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Advertisement