Jump to content

Several C Questions

- - - - -

  • Please log in to reply
1 reply to this topic

#1
SegmentationFault

SegmentationFault

    Newbie

  • Members
  • Pip
  • 1 posts
I'm a CS Major in his third year and right now I feel like a total and complete noob. :( I've done a lot of the stuff I'm doing now lots of times, and I can easily do most of it in Java, but my current class is in C, which is ironically enough, my first programming language and I can't seem to do this stuff right.

My assignment right now is actually in a computer graphics class, but I can't even get to that before I ran into several problems. Firstly, the program needs to parse an input file to get data about some output file name, some resolution numbers in floats and some in ints. There can also be blank lines and comments which are started by a "#". There are seemingly so many places I'm doing things wrong so I figured I'd try to get some help from programmers before I embarrass myself turning this in (even if it works) using really messy syntax and deprecated functions, etc.

Firstly I'm not sure what is considered the "best" way to reading file input. Right now I'm using a function that I wrote just to try to parse a valid piece of data, and I'm not even sure if what I'm using is correct in that:
char * getLine(FILE * fp )

{

	char line [80];

	while(fgets(line,80,fp) != NULL)

	{

		//Look for non-empty line

		if(line[0] != "\n" && line[0] != "#")

		{

			return line;

		}

		sscanf(line, "%s#", line);

	}

	//If at EOF, return null

	return NULL;

}

Here I'm using fgets, and not even sure if I'm using it correctly. :(


Next issue I'm having is that when I am having an int or float taken from my program, I'm trying to figure out the best way to parse that. I'm currently using atof or atoi, such as in this example:
shapes[i].r = atof(getLine(fp));

I've heard that using these are bad, though I've also found a lot of people suggesting using them while I was googling around. Which is it? And what exactly is the "correct" way to do this?


Also, for this I need to error check every line of input I get from the file to ensure it's valid. After each call to getLine, I put in an if statement to check for bad data, such as this:

if(shapes[i].r > 1.0 || shapes[i].r < 0.0)

{

		printf("Invalid red value in shape #%d\n", i);

		fclose(fp);

		freeData(shapes, nshapes);

		return 1;

}
But I'm wondering if there is an easier way to do this in C multiple times, rather than having to write one of these error check if statements for every line of input. I could check the bounds individually and write print statements, but it seems unnecessary to have to write out calls to fclose and return in every one. (And I shudder to think of the idea of a go to statement)


And for my final question, I seem to fail to remember how dynamic memory allocation and freeing works. If I have a struct which contains a pointer to a struct, both of which are dynamically allocated, do I have to go through an array of that first struct and free each sub-struct and then free the main struct array, or does freeing the array/array pointer also free things inside of the structs?


God I feel like such a noob. >_<

#2
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200
A few things I could suggest in my limited time.

Your getline function will return a pointer (char*) to something that will be deallocated after the function ends. Once a function goes out of its scope, variables on the stack will be flagged for deallocation.

You could fix this by accepting a reference to a string of which could hold it, or allocate memory in the heap, although these will not solve the overlaying problem. You seem to be repeating steps, could you not write a "parse file" function that will read the file all at once, with checks for comments and range within the while loop?

You could accept a reference to a shape structure, and write to that within the loop so that the final data will be available once the file parsing function ends.

Quote

Next issue I'm having is that when I am having an int or float taken from my program, I'm trying to figure out the best way to parse that. I'm currently using atof or atoi, such as in this example:

A useful function is sscanf, you could scan the line string for a token, such as %f or %lf (float or double) and check its return value, it will return the integer 1 for example if there is 1 valid specified token (float) within.

strtol and strtof are more appropriate than atoi and atof, and they may even point to the same function, however they do minimal error checking.

Quote

And for my final question, I seem to fail to remember how dynamic memory allocation and freeing works. If I have a struct which contains a pointer to a struct, both of which are dynamically allocated, do I have to go through an array of that first struct and free each sub-struct and then free the main struct array, or does freeing the array/array pointer also free things inside of the structs?

Yes you will, free is not a recursive function and will only work on things of which had been allocated with malloc/calloc, so you must be careful to ensure you are freeing a valid malloc'd data.

Alexander.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users