I keep getting this error whenever I stop my program and I am wondering am I freeing memory right.

This is my structure and I created a global array named day.

typedef struct {
	int hour;
	int minute;
} Time;

typedef struct {
	Time startTime;
	Time endTime;
	char subject[20];
	char location[20];
} Appointment;

typedef struct {
	int day;
	int month;
	Appointment *appts[8];
	int apptCount;
} Day;

Day *day = NULL;

At the end of my main()

for (i = 0; i < daysize; ++i) {
		for (j = 0; j < day[i].apptCount; ++j) {
		free(day[i].appts[j]->location);
		free(day[i].appts[j]->subject);
		} // for j
	} // for i
	free(day);

daysize is a global variable that keeps track of the number of elements in the array. I also have a variable called allocate that keeps track of how much memory, that I have given to my array day. I don't think I will need that number because free(day) should also free unused memories in it.

I also have a question about the fgets() function. Does it prompt the user to input in a stream of data? I tried to input the code fgets(string, 100, stdin); into my program and whenever my program runs, it does not prompt me to type something. I had to put getchar() before the fgets. I am wondering am I supposed to do that?

Recommended Answers

All 6 Replies

Forgot to add that I use these codes to allocate memories:

day[daysize].appts[i] = (Appointment *) malloc(sizeof(Appointment));

day = (Day *) realloc(day, allocate * (sizeof(Day)));

You can only call free() on a pointer which must have been previously returned by malloc(), calloc() or realloc(). You did use malloc(), but not on the character arrays. You used it on the structure that those arrays are in, so that should be the only thing you free. As for your fgets() question

fgets(string, 100, stdin);

will not print anything to the screen, if that's what you're asking, it will just wait for input and read once you press enter. Hope this helps.

Wow, it works now heh.

About the fgets(string, 100, stdin) call, whenever I use it, the program does not wait for me to input a string. I need to put getchar() right before it. This might be because right before the fgets call, I have a scanf() call to obtain an int.

It is so weird that the fgets() call will consider the integer that I entered for the scanf() as the string instead of allowing me to enter a new string. Can someone explain to me why it is doing that?

Let me try to reword what it actually happen if I do not put getchar() on the line before the fgets(). I make the program prompt me to enter an integer with scanf() and afterward, I try to make the program prompt me to enter a string. However, the program seems to think that the integer that I entered before as the string.

scanf() reads the integer but leaves the `ENTER or RETURN' key you pressed, in the stdin buffer. That key is seen by fgets() as a newline and that's what it reads, not waiting for the user to interact. Inserting a getchar() between scanf() and fgets() does eat that left-over newline, allowing fgets() to `wait' for data to be entered in the stdin. No a particular great solution, because it doesn't address the problem, but rather patches it for that instance.

So what is the best solution to fix this kinda of issue? Is there a function that I can call to clear whatever is in istream?

So what is the best solution to fix this kinda of issue? Is there a function that I can call to clear whatever is in istream?

I am afraid not. Not the way you are probably thinking. The way of fixing the issue is to know the behaviour of each function that accepts stream input. After that, store, parse, and validate the data, and check that nothing was left in the input buffer.
Some points are given here by Dave Sinkula. Make sure you check as well the links for specific inputs; by same author.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.