Thread: allocating space dynamically for an array of structures

  1. #46
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  2. #47
    Registered User
    Join Date
    Jan 2009
    Posts
    66

    removed else

    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;	
    }
    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?
    Last edited by cus; 01-08-2009 at 04:29 PM.

  3. #48
    Registered User
    Join Date
    Jan 2009
    Posts
    66
    I have tried to indent the code as best i can - I have even looked it up

  4. #49
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    are you sure that this will malloc memory for an object of type exec_time?
    Code:
    exec_array = malloc(n *sizeof(*exec_array));

  5. #50
    Registered User
    Join Date
    Jan 2009
    Posts
    66
    Quote Originally Posted by itCbitC View Post
    are you sure that this will malloc memory for an object of type exec_time?
    Code:
    exec_array = malloc(n *sizeof(*exec_array));
    i dont know i was recommended to do it.

  6. #51
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    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.

  7. #52
    Registered User
    Join Date
    Jan 2009
    Posts
    66
    Quote Originally Posted by itCbitC View Post
    perhaps you can try this and see what happens
    Code:
    exec_array = malloc(n * (sizeof exec_time));
    and why?

  8. #53
    Registered User
    Join Date
    Jan 2009
    Posts
    66
    I done it nontheless - it compiles with no errors- and has no effect. Output is the same.

  9. #54
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by cus View Post
    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!

    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;
    	}
    	
    }
    Is the exec_array not supposed to print any contents? - I am aware there is nothing there.
    You mean if the malloc succeeds, is the exec_array supposed to print something? Yes. More to the point, it does so:
    Code:
    $ ./structs
    Please enter a non-negative integer: 7
    0 0 0.000000
    I don't know how I got so lucky as to get all zeroes, but there you go.

  10. #55
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by cus View Post
    I done it nontheless - it compiles with no errors- and has no effect. Output is the same.
    if not what's hard-coded, what output are you expecting?

  11. #56
    Registered User
    Join Date
    Jan 2009
    Posts
    66
    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).

  12. #57
    Registered User
    Join Date
    Jan 2009
    Posts
    66
    Quote Originally Posted by itCbitC View Post
    if not what's hard-coded, what output are you expecting?
    I dont know - thats the point, I am fairly new to this you see.

  13. #58
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,345
    Quote Originally Posted by itCbitC
    are you sure that this will malloc memory for an object of type exec_time?
    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...

    Quote Originally Posted by cus
    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).
    I suppose you are talking about this:
    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;
        }
    }
    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."

    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).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #59
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by cus View Post
    I have tried to indent the code as best i can - I have even looked it up
    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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #60
    Registered User
    Join Date
    Jan 2009
    Posts
    66

    thanks again @all

    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 11-26-2008, 10:25 AM
  2. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  3. dynamically allocating a 2dim array
    By agerealm in forum C++ Programming
    Replies: 14
    Last Post: 03-10-2004, 02:40 PM
  4. 2d array of structures
    By spudtheimpaler in forum C Programming
    Replies: 2
    Last Post: 03-01-2004, 03:17 PM
  5. someone who is good at finding and fixing bugs?
    By elfjuice in forum C++ Programming
    Replies: 8
    Last Post: 06-07-2002, 03:59 PM