Welcome, guest! Login / Register - Why register?
Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so just use oauth login instead. :)

Paste

Pasted as C by grouzen ( 17 years ago )
#include <stdio.h>
#include <stdlib.h>

#define ITER 10
#define SIZE 2

struct str {
    int x;
};

int main(void)
{
    struct str *ptr;
    int i, size_ptr;
    ptr = (struct str *) malloc(sizeof(struct str) * SIZE);
    size_ptr = SIZE;
    if(ptr == 0) {
        fprintf(stderr, "Couldn't initialize pointer
");
        exit(1);
    }

    for(i = 0; i < ITER; i++) {
        if(size_ptr <= i) {
            size_ptr += SIZE;
            ptr = (struct str *) realloc(ptr, size_ptr);
            if(ptr == 0) {
                fprintf(stderr, "realloc can't allocate memory");
                exit(1);
            }
            printf("size of ptr changed: %d
", size_ptr);
        }
        ptr[i].x = i;
        printf("ptr[%d].x = %d
", i, i);
    }

    free(ptr);
    return 0;

}

 

Revise this Paste

Children: 5896
Your Name: Code Language: