Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Memory Allocation

Name: Anonymous 2009-10-08 11:02

Hey /prog/riders,

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.


struct strct{
    bool b;
    char c;
}stct*;

int stct_length, stct_height, x, y;

char s[1024];
file = fopen(path, "rb");

for(y = 0;!feof(file);++y){
    fscanf(file, "%s", &s);
    for(x = 0;;++x){
        t = y * stct_length + x;
        switch(s[x]){
            case '0':
                stct[y * stct_length + x].b = 0;
                stct[y * stct_length + x].c = 'd';
                continue;
            case '1':
                stct[y * stct_length + x].b = 1;
                stct[y * stct_length + x].c = 'f';
                continue;
            case '\n':
                break;
        }
        break;
    }
    if(y == 0){
        stct_length = x;
    }
    else if(x != stct_length){
        printf("line %d is of irregular length", y)
        break;
    }
}

stct_height = y;

Name: Anonymous 2009-10-08 11:15

calloc

Now stop making these shitty threads.

Name: Anonymous 2009-10-08 11:23

>>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: Anonymous 2009-10-08 11:31


struct strct{
    bool b;
    char c;
}stct*;

WTF IS THIS SHIT?!

Name: Anonymous 2009-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?

Name: Anonymous 2009-10-08 11:33

shut the fuck up >>5 you're fucking useless

Name: Anonymous 2009-10-08 11:34

>>3
Oh thankyou also, didn't see you there. Thanks for the tips!

Name: Anonymous 2009-10-08 11:39

>>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.

Name: Anonymous 2009-10-08 11:41

>>8
Alright I've got it now. Fucking idiot I am, completely forgot about realloc. Everyone can feel better now, knowing they're better than me.

Name: Anonymous 2009-10-08 11:43

>>9
you suck

Name: Anonymous 2009-10-08 11:44


vararray = calloc
while (loop)
{
   if (x >= arrayLength)
   {
      // realloc
      arrayLength = newLength
   }
   vararray[x] = y
}


Please use sage in the future!

Name: Anonymous 2009-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’

Name: Anonymous 2009-10-08 12:17

malloc my anus

Name: Anonymous 2009-10-08 12:29

>>13
<stdin>:1: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘my’

Name: Anonymous 2009-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

Name: Anonymous 2009-10-08 13:36

>>12,14,15

Back to /g/, please!

Name: Anonymous 2009-10-08 13:44


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: Anonymous 2009-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]

Name: ​​​​​​​​​​ 2010-10-23 6:35

Name: Anonymous 2011-02-03 1:52

Name: Anonymous 2011-02-18 14:01

<-- that's cool and all, but check 'em
Don't change these.
Name: Email:
Entire Thread Thread List