Click here to Skip to main content
15,887,267 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I have to parse fixed length data from a file. It's about 80 fields per line, mostly ints, doubles and short strings. So it is read line from file, parse line by lengths, load to Db.

What seems practical to me is to make a struct that has all the fields I need to parse from the line of data. I would use a struct rather than a class to cut down on boxing and unboxing. There are thousands of records. The thing is that what I have read is don't use structs larger than very few bites (we're talking 24 bytes). That doesn't make much sense to me. The stack is big enough. Is it because it is FIFO? Any ideas about this would be interesting. It is not a problem to make it a class rather than a structure.
Heck, while I'm at it, I have the field parse done, but it's sort of brute force. Any ideas on a more elegant method?
Thanks much, Mike
Posted
Comments
Fredrik Bornander 23-Feb-12 9:47am    
Cool, glad I could help.

I think you should decide this based on the value semantics you want rather than where you think the data structure might be allocated, because that is really what is different between a class and a struct.

Yes, often it is the case that a struct is allocated on the stack but not always and it shouldn't be what dictates your design decision.

If you can show through relevant profiling that a one type of data structure gives you a performance increase (for example) then that might help you decide, but that should only be a consideration if performance is a proven problem.

My point is, stack or heap are not the important aspects of classes and structs, the value semantics are.

This guy explains it a million times better than I can http://blogs.msdn.com/b/ericlippert/archive/2010/09/30/the-truth-about-value-types.aspx[^].

Hope this helps,
Fredrik
 
Share this answer
 
Thanks. Good explanation there and it makes sense.
I think it also clearly says that my data would be on the heap either way so I should take advantage of the class
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900