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)
Always be sure to check the result of malloc:


Code:
if (NULL == (myvar = malloc( size statement))) {
    perror("Failed to allocate memory! Terminating.");
    exit(-1);
}
Uh.... beyond the initial malloc I've got:

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?
*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?
*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:
      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;
            }
         }
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.
Does it only happen when you sort? When you comment out the sort does it still do it?
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.
Um. I have no idea then. Can you show more code?

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.
Huh, I dunno. Other than the "bubble sort" only doing one pass on the data.
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...
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? Smile
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.
  
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
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

 

Advertisement