Thread: Memory allocation problem with functions.

  1. #1
    Registered User
    Join Date
    Aug 2010
    Posts
    230

    Memory allocation problem with functions.

    I have a homework(actually i had 4 months ago but i try to solve this again) that will do these things like in the code, but i must also have to make a function in main that finds the struct of the minimum ages. How can i do this, when after the printf in init function the memory that is blocked is "now"empty and all the names and ages are gone...can anyone help?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <time.h>
    
    struct Employee{
    	char name[30];
    	int age; // [20-65]
    	int x;
    };
    
    void init(struct Employee *A, int n){
    	int i;
    	srand(time(NULL));
    	for(i=0; i<n; i++){
    		A[i].age = 20 + rand()%45;
    		A[i].x = i+1;
    		printf("Name:\n");
    		gets(A[i].name);
    	}
    
    	for(i=0; i<n; i++){
    		printf("%d] Mr/Mrs %s is %d years old,\n", A[i].x, A[i].name, A[i].age);
    	}
    }
    
    int main(int argc, char **argv){
    	struct Employee *A;
    	int size;
    		size = atoi(argv[1]);
    		printf("The size given is: %d\n", size);
    		
    		A = (struct Employee *)malloc(size*sizeof(struct Employee));
    		if(A == NULL){
    			printf("Error in allocating memory.\n");
    			exit(1);
    		}
    		init(A, size);
    		free(A);	
    return 0;
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    How can i do this, when after the printf in init function the memory that is blocked is "now"empty and all the names and ages are gone...can anyone help?
    What do you mean by 'memory that is blocked is now empty'??
    You shouldn't be using gets() use fgets() instead.

  3. #3
    Registered User
    Join Date
    Aug 2010
    Posts
    230
    i mean every function has memory after its call this specific memory is gone...deleted...

    as a solution you mean to use a text file and store the results there?

    Also my last task is to set a pointer to this struct (with the minimum age) and return to main and using this pointer to print the struct...i thought something like this but i am not so sure

    when i am inside the function
    Code:
    ptr = &A[2].age
    and i print this
    Code:
    	printf("min: %p\n", ptr);
    but it prints the address...now the age that i want....how can i fix this?
    Last edited by brack; 09-05-2010 at 04:12 PM.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by brack View Post
    i mean every function has memory after its call this specific memory is gone...deleted...

    is it correct?
    Only if you are creating the struct inside the function.

    If you create it on the open page (i.e. outside of any function) it will exist globally and be accessible by all functions... You could do this with a 1 word change in your code...

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <time.h>
    
    struct Employee
         { char name[30];
            int age; 
            int x; }
      Person;
    Now the variable Person which is an Employee struct, will be available globally throughout your program...

  5. #5
    Registered User
    Join Date
    Aug 2010
    Posts
    230
    Quote Originally Posted by CommonTater View Post
    Only if you are creating the struct inside the function.

    If you create it on the open page (i.e. outside of any function) it will exist globally and be accessible by all functions... You could do this with a 1 word change in your code...

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <time.h>
    
    struct Employee
         { char name[30];
            int age; 
            int x; }
      Person;
    Now the variable Person which is an Employee struct, will be available globally throughout your program...
    the names and the random ages will be also available globally throughout my program?

  6. #6
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    In your first post, A is allocated on heap. The memory is still there as long as you don't free it and of course don't lose the pointer!

  7. #7
    Registered User
    Join Date
    Aug 2010
    Posts
    230
    Quote Originally Posted by Bayint Naung View Post
    In your first post, A is allocated on heap. The memory is still there as long as you don't free it and of course don't lose the pointer!
    By the "end of a function"? Do i loose this memory or not?

  8. #8
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    No, I suggest you try to learn from a book/tutorial.

  9. #9
    Registered User
    Join Date
    Aug 2010
    Posts
    230
    Quote Originally Posted by Bayint Naung View Post
    In your first post, A is allocated on heap. The memory is still there as long as you don't free it and of course don't lose the pointer!
    How can i lose the pointer?
    and
    How can i free it?

    Thanks for help.....

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by brack View Post
    the names and the random ages will be also available globally throughout my program?
    The struct, and whatever is in it is globally available.

    If you need more than one at a time try this:
    Code:
    struct Employee
         { char name[30];
            int age; 
            int x; }
    
    Employee Person[1000];
    Now you have 1000 of them, all globally available all the time.

    Still another way, which doesn't embed this into your program image...
    Code:
    // create an EMPLOYEE type
    
    typedef struct tEMPLOYEE
         { char name[30];
            int age; 
            int x; }
         EMPLOYEE, *pEMPLOYEE;
    
    // set up a pointer;
    pEMPLOYEE Person;  // pointer to employee array
    Then at program entry you do...
    Code:
    .... Main....
    
    malloc(Person,10000 * sizeof(EMPLOYEE));
    And you have 10,000 employees globally available in your code.

    You access them as an array Person[161]->Name etc.

    What makes the difference is where you create the variable... If you do it outside of your Main function, it will be global. If you do it inside any function it will be local to that function only since it will be destroyed when the function exits.

    Grab any good C tutorial ("C in 21 days" is excellent) and it will explain "variable scope" to you... A little reading with a big benefit, my friend.







    Does that help?
    Last edited by CommonTater; 09-05-2010 at 07:23 PM.

  11. #11
    Registered User
    Join Date
    Aug 2010
    Posts
    230
    yes very much...thank you CommonTater i' ll grab an tutorial but i have exams in two days and i am trying to cover gaps from my notes...i do not have much time....sorry thanks for being patient with me...

  12. #12
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    I won't recommend 'Learn XXX in dd days' books.
    First chapter of these books would be 'Time dilation'.
    Teach Yourself Programming in Ten Years

  13. #13
    Registered User
    Join Date
    Aug 2010
    Posts
    230
    Actually i have a book. In greek it is called: "Πλήρες ενχειρίδιο της C" ~ Full C book or something like that...
    editor: Jones & Aitken...so this is my source...

  14. #14
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Bayint Naung View Post
    I won't recommend 'Learn XXX in dd days' books.
    First chapter of these books would be 'Time dilation'.
    Teach Yourself Programming in Ten Years
    LOL! Yes it should... but I learned from tutorials like that, experimentation and good old trial and error... It's been quite a trip... and yes, I did get the fundimentals in 21 days...

    But, as we all know the real learning starts after school. School just teaches us how to learn.


    Teach Yourself C in 21 Days -- Table of Contents

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    CommonTater, you realize that globals are bad, right?
    Furthermore, your pointer typedef only obfuscates the code,
    What's wrong with

    EMPLOYEE* Employee;

    Instead of

    pEmployee Employee;

    ?
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling C in Visual Studio 2005
    By emanresu in forum C Programming
    Replies: 3
    Last Post: 11-16-2009, 04:25 AM
  2. Memory Fragmentation with Dynamic FIFO Queue
    By fguy817817 in forum Linux Programming
    Replies: 17
    Last Post: 10-31-2009, 04:17 AM
  3. Mysterious memory allocation problem
    By TomServo1 in forum C Programming
    Replies: 7
    Last Post: 07-08-2007, 11:29 AM
  4. Memory allocation problem
    By Machado in forum C++ Programming
    Replies: 1
    Last Post: 11-03-2005, 11:11 PM
  5. Manipulating the Windows Clipboard
    By Johno in forum Windows Programming
    Replies: 2
    Last Post: 10-01-2002, 09:37 AM