954,498 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

malloc problem.

HMm... already frustrated at it... But is there something wrong in my malloc? Everytime I try to display the record, it gives me random things as answer(see code below)? Was it that I didn't free it or was I wrong in declaring malloc itself?

Thanks in advance.

#include <stdio.h>
#include <stdlib.h>

typedef struct{																																				//Structure for student profile
	char fname[100];
	char lname[100];
	char college[50];
	char course[50];
	int batch;																																					//Year Entered UP.
	int snum;																																					//Student Number
	int age;
	float gpa;
}student;

int search_snum();
void display_All();
void edit();
void menu();

main(){
	
	student s;
	int x;																																						//Choices for the menu, x for the counter-controlled events
	int size, choice, choice2;																													//"size" Deterrmines number of students for the memory allocation
	int id;
	student *s_record;																																//Record's array, to be created using malloc.
	
	system("cls");
	printf("Enter Number of Students: ");
	scanf("%d", &size);
	
	s_record = malloc(sizeof(student) * size);
	
	do{
	
		printf("Menu: \n\n1. Edit Student Information\n2. Search for a student\n3. Display All Students\n4. Quit\nChoice: ");
		scanf("%d",&choice);
		system("cls");
		
		switch( choice){
			
			case 1:
					
                 for( x = 0 ; x < size; x++ ){
					edit(x, s_record);                 
				}
				system("cls");
				printf("Filling of Student Information Complete.\n\n");
				break;
				
			case 2:					
							id = search_snum(s_record, size);	
							system("cls");
							
							printf("Student Information:\n\n");
							printf("Student Number: %d-%d\n",s_record[id].batch,s_record[id].snum);
							printf("Name: %s, %s\n",s_record[id].lname, s_record[id].fname);
							printf("College: %s\n",s_record[id].college);
							printf("Course: %s\n",s_record[id].course);
							printf("Age: %d\n",s_record[id].age);
							printf("GPA: %.2f\n",s_record[id].gpa);
							
							putchar('\n');
							
							break;
			
			case 3:
				
				printf("Students:\n\n" );
					
					for(x=0 ; x<size ; x++ ){
						printf("%3d: %4d %6d : %s, %6s %5s %s %.2f\n", x+1, s_record[x].batch, s_record[x].snum, s_record[x].lname, s_record[x].fname, s_record[x].college, s_record[x].course, s_record[x].gpa);
					}
				putchar('\n');
				
				break;
				
			case 4:
				break;
		}
		
		
	}while(choice!=4);
	
	free(s_record);

}

void edit(int x, student *s_record){
		
		system("cls");
		
		printf("Student Information:\n\n");
		
		printf("Enter Year Entered in the University: ");
		scanf("%d", &s_record[x].batch);
		
		printf("Enter Student Number: ");
		scanf("%d", &s_record[x].snum);
		
		printf("Enter Last Name: ");
		scanf("%s", &s_record[x].lname);
		
		printf("Enter Name First Name: ");
		scanf("%s", &s_record[x].fname);
		
		printf("Enter College: ");
		scanf("%s", &s_record[x].college);
		
		printf("Enter Course: ");
		scanf("%s", &s_record[x].course);

		printf("Enter Age: ");
		scanf("%d", &s_record[x].age);
		
		printf("Enter GPA: ");
		scanf("%f", &s_record[x].gpa);
}

int search_snum(student *s_record, int size){
		
	int ptr=0;
	int id;
	
		printf("Enter Student Number, without the 'year entered in the university' : ");
		scanf("%d",&id);
			
			while (ptr<size){
				if (s_record[ptr].snum != id){ 
					ptr++;	
				}else{ 
					return ptr; 
				}
			}
}
CodeAerial
Newbie Poster
14 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
 

I ran your code as is and entered 3 students worth of data in step 1, and then went to step 3 and the data displayed as I had entered them. What does your output look like (you can paste it right in with code tags).

I don't think this is the root of it, but you might want to consider converting your scanf statements for the strings into fgets statements, that way your user can't input something that will overrun the char array.

jonsca
Quantitative Phrenologist
Team Colleague
5,621 posts since Sep 2009
Reputation Points: 1,165
Solved Threads: 581
 

I didn't inputted anything yet, for the fear that I might do something bad to my system... but the output goes like this:

[IMG]http://i247.photobucket.com/albums/gg129/jekzki098/Test1.jpg[/IMG]

Also, I tried it on another code, this time with a search... The results show something like "OCUMENT" something...

CodeAerial
Newbie Poster
14 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
 

Well, if you haven't added any records in, then don't try to display them. You've discovered that when you declare a variable in C, there could be any junk at all in it. You could add something to your code to make sure that there are records before you display them.

Also, why is the menu in that screenshot different than the one in your code?

jonsca
Quantitative Phrenologist
Team Colleague
5,621 posts since Sep 2009
Reputation Points: 1,165
Solved Threads: 581
 

Oh! Didn't noticed the code.

Here...

#include <stdio.h>
#include <stdlib.h>

typedef struct{
	char fname[100];																													//First Name
	char lname[100];																													//Last Name
	char college[100];																															
	char course[100];
	int batch;																																//Year entered the university
	int snum;																																//Student Number
	int age;
	float gpa;
}student;

void displayRecord();

main(){
	
	int size, choice, x;
	student *s_record;																													//Pointer for the dynamic record array
		
		system("cls");
		
		printf("Enter Number of Students: ");																					//Size input. Determines how big the record is for the malloc.
		scanf("%d", &size);
		
		s_record = malloc( sizeof( student ) * size );
		
		system("cls");
		
		do{
			
			printf("Menu\n\n");																												//Displays all possible commands.
			printf("[1] Display Record\n");
			printf("[2] Add Record\n");
			printf("[3] Remove Record\n");
			printf("[4] Update Record\n");
			printf("[5] Quit\n");
			printf("Choice: ");
			scanf("%d", &choice);
			system("cls");
			
			switch(choice){																												
				case 1:
					
					displayRecord(s_record,size);
					break;
				
				case 2:
					break;
				case 3:
					break;
				case 4:
					break;
				case 5:
					break;
				default:
					printf("Error. Please Enter valid number of choice.\n");
			}
			
		}while(choice != 5);
		
		free(s_record);
		
}

void displayRecord(student *s_record, int size){
		
	int x;
		
		printf("Student Information: \n\n");	
			for ( x=0 ; x<size ; x++){
				printf("%3d: %4d %6d : %s, %6s %5s %s %.2f \n", x+1, s_record[x].batch, s_record[x].snum, s_record[x].lname, s_record[x].fname, s_record[x].college, s_record[x].course, s_record[x].gpa);
			}
		putchar('\n');
}
CodeAerial
Newbie Poster
14 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
 

Okay. My sentiment is still the same, though, you can't display what you haven't put in yet... What do you expect will happen when you do that? (maybe I'm missing something here)

jonsca
Quantitative Phrenologist
Team Colleague
5,621 posts since Sep 2009
Reputation Points: 1,165
Solved Threads: 581
 

I was expecting it'd be blank all in all at the start of the program, but everytime I execute it, there's some garbage values at index 0 of the structure array.

Any ideas??? =/

CodeAerial
Newbie Poster
14 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
 

malloc sets aside a block of memory, it does nothing to initialize it whatsoever. You get whatever happens to be sitting there. You could possibly use memset to blank the char arrays out (I do not know enough about how it behaves or if there are any side effects/gotchas associated with that). I'd say just put a guard in place to prevent the user from displaying the data before there are data to be displayed if you are concerned about it.

jonsca
Quantitative Phrenologist
Team Colleague
5,621 posts since Sep 2009
Reputation Points: 1,165
Solved Threads: 581
 

Ok, will do. Thanks.

CodeAerial
Newbie Poster
14 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: