basically set up an array of structures and then use malloc to allocate space dynamically for that structure. I know i have made things difficult but - i am a beginner and trying to teach myself.
Printable View
You have done, but what is the assignment for? What is it supposed to do?
its not supposed to do anything - other than that question i.e. set up an array of structures and then use malloc to allocate space dynamically for that structure. thats it thats all its supposed to do.
Frankly, the first thing that you made difficult for yourself was in not following the instructions. Some teachers force students to use unreadable names, but your teacher asked you to use descriptive names: "for a structure of arrays, that must have the integer elements execution_time and process_number and a float element called average_time". Further more, it is clear that the structure type is to be named exec_time, not time. As such, I would expect:Quote:
Originally Posted by cus
Now, when you want to allocate n exec_time structures, I would expect something like this:Code:typedef struct exec_time
{
int execution_time;
int process_number;
float average_time;
} exec_time;
(To be robust, you should check that the return value of malloc() is not NULL before using it, as would be the case if an allocation could not be performed.)Code:exec_time *exec_time_array;
size_t n;
/* Code to get the value of n */
exec_time_array = malloc(n * sizeof(*exec_time_array));
/* Code that uses the dynamically allocated array via exec_time_array */
free(exec_time_array);
Thank you for your patience lazerlight - and everyone else :) - I obviously tried to over complicate things by asking the user to specify how many integers they would like to reserve, and as a result forgot about the objective. Here is what I have done.
Code:#include <stdlib.h>
#include <stdio.h>
typedef struct exec_time
{
int execution_time;
int process_number;
float average_time;
}time;
time *exec_array;
size_t n;
int main()
{
exec_array = malloc(n *sizeof(exec_array));
(*exec_array).execution_time=10;
(*exec_array).process_number=9;
(*exec_array).average_time=9.15;
if(exec_array != NULL)
{
printf("%d %d %f\n",(*exec_array).execution_time,(*exec_array).process_number,(*exec_array).average_time);
free(exec_array);
return 0;
}
else
{
printf("\nMemory allocation failed - not enough memory.\n");
return 1;
}
>>sizeof(exec_array)
What is the size of a pointer? Not the size of the struct you want to allocate bytes for obviously!
>>(*exec_array).execution_time=10;
>>(*exec_array).process_number=9;
>>(*exec_array).average_time=9.15;
Hint: Try checking if the pointer is NULL BEFORE you assign or read anything from it.
And (*exec_array).average_time is the same as exec_array->average_time. Repeat for the others.
Well, that makes the dynamic allocation useful, otherwise if n is sufficiently small you would just make the array a fixed size array :)Quote:
Originally Posted by cus
Personally, I still think you should typedef the struct to exec_time, not time. More importantly, you are using malloc() pre-maturely. On the first line of the main() function, n is 0 since n has static storage duration. This means that you are allocating an array of zero size. You then access the first element of the array, but since the array has no elements, you are accessing the array out of bounds.Quote:
Originally Posted by cus
I would expect something along these lines:
Code:#include <stdlib.h>
#include <stdio.h>
typedef struct exec_time
{
int execution_time;
int process_number;
float average_time;
} exec_time;
int main()
{
exec_time *exec_array;
size_t n;
printf("Enter a non-negative integer: ");
/* Read in the input to n. */
exec_array = malloc(n * sizeof(*exec_array));
if (exec_array == NULL)
{
/* Print memory allocation failure error, and possibly exit immediately. */
}
/* Use exec_array */
free(exec_array);
return 0;
}
I followed the bits of advice up until use exec_array part, obviously there is nothing in there now, but is that what you intended..... ?Code:
#include <stdlib.h>
#include <stdio.h>
typedef struct exec_time
{
int execution_time;
int process_number;
float average_time;
}exec_time;
int main()
{
exec_time *exec_array;
size_t n;
printf("Please enter a non-negative integer: ");
scanf("%d", &n);
exec_array = malloc(n *sizeof(*exec_array));
if(exec_array == NULL)
{
printf("\nMemory allocation failed - not enough memory.\n");
}
printf("%d %d %f\n",exec_array->execution_time,exec_array->process_number,exec_array->average_time);
free(exec_array);
return 0;
}
Does not work if malloc fails.
And when are you going to learn indentation?
Think up a scenario if malloc fails. Then step through your code in your mind with this scenario and watch what happens.
You probably should end the program immediately after printing the error message. Another option is to ask the user to enter a smaller number, and try again.Quote:
Originally Posted by cus
Ok at this moment in time if malloc fails then the program will continue to execute and consequently run the following printf statement which we dont want.
So therefore, we should make the program exit - I am unsure of how to do this.. would it be:Can i do this in the printf statement? Or do i do it afterwards?Code:EXIT(0)
Indeed not. Quite correct.
exit works fine, but in main, you can also use return since the program quits after the main function has finished executing.Quote:
So therefore, we should make the program exit - I am unsure of how to do this.. would it be:Code:EXIT(0)
What's the point of doing it afterwards if we don't want the print statement to run if malloc fails?Quote:
Can i do this in the printf statement? Or do i do it afterwards?
Consequently, when writing the new code, use the same strategy to see if your logic is correct (works for small sections of code).
ok this is what I have now - I have simply added return 1; and an else statement to execute the following printf for when 'exec_array' does not equal NULL. I have done this with the following code: Apologies for the lack of correct indentation - I have not mastered it yet!
Is the exec_array not supposed to print any contents? - I am aware there is nothing there.Code:
#include <stdlib.h>
#include <stdio.h>
typedef struct exec_time
{
int execution_time;
int process_number;
float average_time;
}exec_time;
int main()
{
exec_time *exec_array;
size_t n;
printf("Please enter a non-negative integer: ");
scanf("%d", &n);
exec_array = malloc(n *sizeof(*exec_array));
if(exec_array == NULL)
{
printf("\nMemory allocation failed - not enough memory.\n");
return 1;
}
else {
printf("%d %d %f\n",exec_array->execution_time,exec_array->process_number,exec_array->average_time);
free(exec_array);
return 0;
}
}