Thread: calloc

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    51

    calloc

    I am writing this program and using calloc. My next step is to add realloc for every new entry added.
    But i do not understand why when I set
    Code:
    ent1 = (ent *)calloc(1, sizeof(ent));
    this, if I choose to add a new entry, I can. I thought the above code would only let me use enough memory(one array of my struct) for one of my typedef struct??
    I seem to be able to add new entries into the memory space i thought would only hold 1 type (ent).
    I must be confused about how calloc works, so if anyone can explain I would be grateful.

    Code:
    #include <stdio.h>
    #include<string.h>
    #include <stdlib.h>
    
    typedef struct entry{
    	char name[25];
    	int num;
    }ent;
    
    void addEntry(ent *);
    void printEntry(ent*);
    
    int entries = 0;
    
    int main(){
    	
    	ent *ent1;
    	ent *newEnt1;
    	int choice = 0;
    	
    	ent1 = (ent *)calloc(1, sizeof(ent));
    	
    	if (ent1 == NULL) {
    		return; //calloc is not successful
    	}//end if
    	
    	do {
    		printf("\n1\tAdd Entry.\n");
    		printf("2\tPrint list.\n");
    		printf("3\tQuit.\n");
    		printf("Enter choice : ");
    		scanf("%d", &choice);
    		
    		switch (choice) {
    			case 1:
    				
    				addEntry(ent1);
    				entries += 1;
    				break;
    			case 2:
    				printEntry(ent1);
    				break;
    				
    			default:
    				break;
    				
    		}//switch
    		
    		
    	} while (choice != 3);
    	
    	return 0;
    	free(ent1);
    	
    }//main
    
    void addEntry(ent * ent2){		//ent2 accepts ent1
    	
    	
    	printf("\nEnter name: \n");
    	scanf("%s", ent2[entries].name);
    	//fgets(ent2[entries].name, 25, stdin);
    	printf("Enter Phone number: \n");
    	scanf("%d", &ent2[entries].num);
    }//end func def add
    
    void printEntry (ent * ent2){
    	int x;
    	
    	
    	for (x=0; x<entries; x++) {
    		
    		printf("\nName: %s\n", ent2[x].name);
    		printf("Phone number: %d\n", ent2[x].num);
    	}//for 
    	
    }//end func def prlst

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Just because it lets you doesn't mean it's right. All that means is that you're writing on top of memory that belongs to someone else, which you will notice when that someone else tries to use it.

  3. #3
    Registered User
    Join Date
    Apr 2010
    Posts
    51
    Ok, for some reason I thought malloc/calloc assigned only a block size you requested, and wouldn't let the program go over. I was very wrong.
    The book I am learning from states "this program allows the user to enter UP TO an 80 character name, the amount of storage requested by malloc", I read this as not letting the program use any more than 80, but after 80 it will be using unrequested/unsafe memory. Thus the need for safe programming.
    I think I am right in believing that malloc/calloc sets aside what we request, and the pointer we define points to the first element. ??

  4. #4
    Registered User
    Join Date
    Apr 2010
    Posts
    51
    would you mind telling me if I am using calloc/realloc correctly.
    I am trying to add more memory each time a user adds a new entry.

    Code:
    #include <stdio.h>
    #include<string.h>
    #include <stdlib.h>
    
    typedef struct entry{
    	char name[25];
    	int num;
    }ent;
    
    void addEntry(ent *);
    void printEntry(ent*);
    
    int entries = 0;
    
    int main(){
    	
    	ent *ent1;
    	
    	ent *newEnt1;
    	int choice = 0;
    	int memory = 1; //mem starst as 1 because we want 1*size of struct ent
    					//to be allocated memory
    	int oldmem=0;
    	ent1 = (ent *)calloc(memory, sizeof(ent));
    	
    	
    	if (ent1 == NULL) {
    		return 0; //calloc is not successful
    	}//end if
    	
    	do {
    		printf("\n1\tAdd Entry.\n");
    		printf("2\tPrint list.\n");
    		printf("3\tQuit.\n");
    		printf("Enter choice : ");
    		scanf("%d", &choice);
    		
    		switch (choice) {
    			case 1:
    				
    				addEntry(ent1);
    				entries += 1;
    				
    			
    				
    				oldmem = memory; // transfer amount of arrays to old mem
    				newEnt1 = realloc(ent1, (memory+=1)*sizeof(ent));
    				
    					if (newEnt1 == NULL) {
    						printf("\nOut of memory\n");
    						return 0;
    					}//end if
    				
    					else{
    						ent1 = newEnt1;
    					}//else
    				printf("\nThe amount of memory allocated is %d * size of ent struct", memory);
    				
    				break;
    			case 2:
    				printEntry(ent1);
    				break;
    				
    			default:
    				break;
    				
    		}//switch
    		
    		
    	} while (choice != 3);
    	
    	return 0;
    	free(ent1);
    	
    }//main
    
    void addEntry(ent * ent2){		//ent2 accepts ent1
    	
    	
    	printf("\nEnter name: \n");
    	scanf("%s", ent2[entries].name);
    	
    	//fgets(ent2[entries].name, 25, stdin);
    	
    	printf("Enter Phone number: \n");
    	scanf("%d", &ent2[entries].num);
    	
    	
    	
    }//end func def add
    
    void printEntry (ent * ent2){
    	int x;
    	
    	
    	for (x=0; x<=entries-1; x++) {
    		
    		printf("\nName: %s\n", ent2[x].name);
    		printf("Phone number: %d\n", ent2[x].num);
    	}//for 
    	
    	
    }//end func def prlst

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    The program you have appears to be correct. oldmem is superfluous since you still have the ent1 pointer to the original memory. I would guess that the bit you quoted refers to fgets, since that limits the amount of input that can be entered.

  6. #6
    Registered User
    Join Date
    Apr 2010
    Posts
    51
    Yes, oldmem was taken from the test program (testing calloc/realloc), but isn't needed in this one.

    You are right in that the program the quoted text was referring to was using "gets", well spotted.
    Thanks for your help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem with qsort and array via calloc
    By Zimbobo in forum C Programming
    Replies: 2
    Last Post: 11-23-2009, 12:25 AM
  2. malloc, calloc from the FAQ
    By salvadoravi in forum C Programming
    Replies: 10
    Last Post: 01-21-2008, 03:29 AM
  3. Malloc and calloc problem!!
    By xxhimanshu in forum C Programming
    Replies: 19
    Last Post: 08-10-2005, 05:37 AM
  4. Why use calloc()?
    By dwks in forum C Programming
    Replies: 8
    Last Post: 07-20-2005, 08:22 AM
  5. difference between calloc and malloc
    By saravanan_ts in forum C Programming
    Replies: 4
    Last Post: 07-28-2003, 06:13 AM