I'm fairly new to C, I'm working with a piece of code that reads from a file to populate an array. My problem is the size of the array is not static, and I have no idea how large the end result is going to be.
Here's something I hacked up for an example, I haven't started working on the piece of code yet, I'm just wondering how I should handle the memory allocation for stct.
>>1 malloc if you plan on writing to the entire buffer, or don't care that it might contain random data. calloc if you need a zero'ed out buffer, it's also considered nicer as it lets you separate the amount of items and each item size's, instead of writing data=malloc(count*sizeof(SOMETHING));memset(data,0,count*sizeof(SOMETHING));
Don't forget to free your buffer once you're done with it.
Name:
Anonymous2009-10-08 11:31
struct strct{
bool b;
char c;
}stct*;
WTF IS THIS SHIT?!
Name:
Anonymous2009-10-08 11:32
Sorry, yes it is a shitty thread and I know about malloc and calloc. Just do I do them incrementally? Or at the end when I know the exact size I need?
>>5
You can realloc your data, but that can be costly. Other people may prefer doubling the buffer's capacity by 2 each time you hit it, which would be less costly than reallocing each time, while others prefer using some form of linked list. Linked lists are probably the nicest way to deal with it, but it's less comfortable to work with them in C than in more high-level languages(most high-level language make it trivial enough, but Lisp likely offers the easiest way to manage lists - so easy that I've even seen people greenspun Lisp's list handling functions in C). There's some other aproaches, but they usually end up being similar to the previous two, or a combination of them.
vararray = calloc
while (loop)
{
if (x >= arrayLength)
{
// realloc
arrayLength = newLength
}
vararray[x] = y
}
Please use sage in the future!
Name:
Anonymous2009-10-08 11:57
>>11 <stdin>:1: error: ‘vararray’ undeclared (first use in this function)
<stdin>:1: error: (Each undeclared identifier is reported only once
<stdin>:1: error: for each function it appears in.)
<stdin>:1: error: ‘calloc’ undeclared (first use in this function)
<stdin>:2: error: expected ‘;’ before ‘while’
>>13 <stdin>:1: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘my’
Name:
Anonymous2009-10-08 12:56
>>14
error: expected identifier or ‘(’ before ‘<’ token
error: stray ‘\342’ in program
error: stray ‘\200’ in program
error: stray ‘\230’ in program
error: stray ‘\342’ in program
error: stray ‘\200’ in program
error: stray ‘\231’ in program
error: stray ‘\342’ in program
error: stray ‘\200’ in program
error: stray ‘\230’ in program
error: stray ‘\342’ in program
error: stray ‘\200’ in program
error: stray ‘\231’ in program
error: stray ‘\342’ in program
error: stray ‘\200’ in program
error: stray ‘\230’ in program
error: stray ‘\342’ in program
error: stray ‘\200’ in program
error: stray ‘\231’ in program
error: expected identifier or ‘(’ before ‘,’ token
error: stray ‘\342’ in program
error: stray ‘\200’ in program
error: stray ‘\230’ in program
error: stray ‘\342’ in program
error: stray ‘\200’ in program
error: stray ‘\231’ in program
error: stray ‘\342’ in program
error: stray ‘\200’ in program
error: stray ‘\230’ in program
error: stray ‘\342’ in program
error: stray ‘\200’ in program
error: stray ‘\231’ in program
error: stray ‘\342’ in program
error: stray ‘\200’ in program
error: stray ‘\230’ in program
error: stray ‘\342’ in program
error: stray ‘\200’ in program
error: stray ‘\231’ in program
Microsoft has patented the idea that "a user may discover and navigate among hyperlinks through the use of a keyboard." Even if you thought the CIA and FBI are completely and utterly incompetent, you have to grant that U.S. Patent Office takes first prize. There is a rather significant missing reference to prior art in the application: the Lynx character based web browser. For obvious reasons (no mouse) this browser has always navigated among links using the keyboard - using in fact the same tab key cited in the Microsoft Patent. Two seconds research with Google (do you suppose the U.S. Patent Office owns a web browser and has heard of Google?) turns up the following history of Lynx: it was released in March 1993. Now the first web browser was released only in March 1991, while Internet Explorer was first released on August 23rd, 1995. So it seems mighty unlikely that Microsoft submitted this patent application prior to the introduction (and widespread usage of) Lynx. Shouldn't this intentional overlooking of widely available prior art be considered a form of patent fraud?
Name:
Anonymous2009-10-08 14:07
>>15 [code]<stdin>:1: error: expected identifier or ‘(’ before ‘>>’ token
<stdin>:3: error: stray ‘\342’ in progr[b]Thanks for your contribution, but it was too large.[/b]