There is no need for the else. If malloc fails, it quits the program and does not continue, so...
And you have never filled in any values for exec_array, so it will print gibberish.
And lastly, of course, you still need that indentation.
The resulting output of this is 10, 9 and 9.15, - but when i enter an integer from the question "Please enter a non-negative integer: ", am I right in saying this allocates the memory correctly but apart from that does nothing?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; } (*exec_array).execution_time=10; (*exec_array).process_number=9; (*exec_array).average_time=9.15; printf("%d %d %f\n",exec_array->execution_time,exec_array->process_number,exec_array->average_time); free(exec_array); return 0; }
Last edited by cus; 01-08-2009 at 04:29 PM.
I have tried to indent the code as best i can - I have even looked it up
are you sure that this will malloc memory for an object of type exec_time?
Code:exec_array = malloc(n *sizeof(*exec_array));
perhaps you can try this and see what happens
Code:exec_array = malloc(n * (sizeof(exec_time)));
Last edited by itCbitC; 01-08-2009 at 05:19 PM.
I done it nontheless - it compiles with no errors- and has no effect. Output is the same.
thank you tabstop for essentially answering the question that I asked - I have no more hair to be pulled out. So all I need to know is the program does what its set out to do? Please say yes - or the cat is getting it (only kidding offcourse).
No, it will do more than that. It will (assuming that it succeeds) allocate memory for n objects of type exec_time, assuming that *exec_array is of type exec_time. However, it appears that we may have misread the rubric...Originally Posted by itCbitC
I suppose you are talking about this:Originally Posted by cus
Take a look at your problem specification again: "Set up an array of structures and show how to access individual elements of the structures within the array. Then finally, use malloc to allocate space dynamically for a structure of the type structure exec_time."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; } }
I originally interpreted that as "use malloc to allocate space dynamically for an array of exec_time structures", but if you posted the problem correctly, then that is wrong. The problem is divided into three parts:
- Define the structure exec_time
- Create an array of structures and access the members of the elements of the array
- Allocate space dynamically for one structure
You have done the first part correctly. The second part does not require dynamic memory allocation using malloc(). You can use a fixed size array. The intention is most probably to test you on how you access the members of the elements of the array. The third part merely requires you to allocate (and presumably deallocate) space for one exec_time structure. You are not required to do anything with the structure (but I suppose you could for bonus marks... unless your teacher is a computer security professional).
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
http://apps.sourceforge.net/mediawik...le=Indentation
Also note how laserlight's code looks compared to yours and look how laserlight has done vs yours.
Thanks for the indentation link Elysia- and yes i can see the difference between laserlight's code and my own - I agree its much easier to read and wont post again without it.
Thanks