What is C++ version of realloc(), to allocate the new buffer and copy the contents from the old one?

There’s no new/delete equivalent of realloc in C++. From Bjarne Stroustrup’s FAQ : Why doesn’t C++ have an equivalent to realloc()? If you want to, you can of course use realloc(). However, realloc() is only guaranteed to work on arrays allocated by malloc() (and similar functions) containing objects without user-defined copy constructors. Also, please remember … Read more

Is it possible to use a C++ smart pointers together with C’s malloc?

If you are using shared_ptr or unique_ptr, you can specify a custom deleter. For example, struct free_delete { void operator()(void* x) { free(x); } }; This can be used with shared_ptr like so: std::shared_ptr<int> sp((int*)malloc(sizeof(int)), free_delete()); If you are using unique_ptr, the deleter is a part of the unique_ptr‘s type, so the deleter needs to … Read more

Is there a need to check for NULL after allocating memory, when kernel uses overcommit memory

Yes, you should still check for failures returned by malloc. In an environment that overcommits memory you will not be able to detect and recover from failures due to the environment running out of physical storage required when you write to parts of the address space that have been allocated to your program by a … Read more

Is a malloc() needed before a realloc()?

From Open Groups’ specifications: If ptr is a null pointer, realloc() shall be equivalent to malloc() for the specified size. If ptr does not match a pointer returned earlier by calloc(), malloc(), or realloc() or if the space has previously been deallocated by a call to free() or realloc(), the behavior is undefined.

How to initialize const in a struct in C (with malloc)

You need to cast away the const to initialize the fields of a malloc’ed structure: struct deneme *mydeneme = malloc(sizeof(struct deneme)); *(int *)&mydeneme->a = 15; *(int *)&mydeneme->b = 20; Alternately, you can create an initialized version of the struct and memcpy it: struct deneme deneme_init = { 15, 20 }; struct deneme *mydeneme = malloc(sizeof(struct … Read more

C – calloc() v. malloc() [duplicate]

From http://wiki.answers.com/Q/Is_it_better_to_use_malloc_or_calloc_to_allocate_memory malloc() is faster, since calloc() initializes the allocated memory to contain all zeros. Since you typically would want to use and initialize the memory yourself, this additional benefit of calloc() may not be necessary.

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)