static VALUE initialize(VALUE self, VALUE io) { struct readerdata *reader; Data_Get_Struct(self, struct readerdata, reader); if (reader->info) { png_destroy_read_struct(&reader->png, &reader->info, NULL); allocate2(reader); reader->locked = 0; } reader->source_io = io; png_set_read_fn(reader->png, (void*)io, read_data); png_read_info(reader->png, reader->info); png_set_packing(reader->png); png_set_strip_16(reader->png); png_set_expand(reader->png); png_read_update_info(reader->png, reader->info); reader->scale_width = png_get_image_width(reader->png, reader->info); reader->scale_height = png_get_image_height(reader->png, reader->info); return self; }
static VALUE allocate(VALUE klass) { struct readerdata *reader; VALUE self; self = Data_Make_Struct(klass, struct readerdata, mark, deallocate, reader); allocate2(reader); return self; }
int main(void) { char * b = NULL; // This fails because allocate1 changes the _value_ of the passed parameter, // not the value pointed by the passed parameter. allocate1(b); printf("Allocate 1: %s\n", b); b = NULL; // This is OK, because the allocate2 changes the value _pointed_ by the passed parameter. allocate2(&b); printf("Allocate 2: %s\n", b); b = NULL; }
int main (int argc, char const *argv[]) { int * ptr1; int * ptr2; bool allocate1Success = false; bool allocate2Success = false; try { allocate1(10, ptr1); allocate1Success = true; } catch(std::bad_alloc &e) { } if (allocate1Success) delete [] ptr1; allocate2(10, ptr1, ptr2); /* code */ return 0; }