dynamic-memory-allocation

Using zlib deflateBound() dynamically

瘦欲@ 提交于 2019-12-23 04:59:10
问题 I have been looking at how to use deflateBound() dynamically and have not found exactly what I am looking for. I looked at the manual for zlip, examples included in library and found these here: Compression Libraries for ARM Cortex M3/4 Determine compressed/uncompressed buffer size for Z-lib in C zlib, deflate: How much memory to allocate? What I am missing is how to allocate after calling deflateBound(). E.g. this looks like it will cause problems z_stream defstream; uint8_t *outBuf=NULL;

Are there any other semi/portable ways to dynamically allocate memory?

心已入冬 提交于 2019-12-22 12:21:42
问题 this: char *buf = NULL; scanf("%ms", &buf); will get a dynamically allocated char buffer. I know this is limited to code compiled with Gcc (and specifically version 2.7 of glibc). I am also aware that the "correct" and portable way is to just use malloc() (and friends) to get the memory. I'm curious however, are there any other portable or semi-portable * implementations out there for getting dynamically allocated memory? I find tricks and tips of memory allocation in C a hard topic to

using malloc over array

时间秒杀一切 提交于 2019-12-22 06:33:16
问题 May be similar question found on SO. But, I didn't found that, here is the scenario Case 1 void main() { char g[10]; char a[10]; scanf("%[^\n] %[^\n]",a,g); swap(a,g); printf("%s %s",a,g); } Case 2 void main() { char *g=malloc(sizeof(char)*10); char *a=malloc(sizeof(char)*10); scanf("%[^\n] %[^\n]",a,g); swap(a,g); printf("%s %s",a,g); } I'm getting same output in both case. So, my question is when should I prefer malloc() instead of array or vice-verse and why ?? I found common definition,

Why is dynamically allocated memory always 16 bytes aligned?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-22 06:29:09
问题 I wrote a simple example: #include <iostream> int main() { void* byte1 = ::operator new(1); void* byte2 = ::operator new(1); void* byte3 = malloc(1); std::cout << "byte1: " << byte1 << std::endl; std::cout << "byte2: " << byte2 << std::endl; std::cout << "byte3: " << byte3 << std::endl; return 0; } Running the example, I get the following results: byte1: 0x1f53e70 byte2: 0x1f53e90 byte3: 0x1f53eb0 Each time I allocate a single byte of memory, it's always 16 bytes aligned. Why does this happen

Why is dynamically allocated memory always 16 bytes aligned?

和自甴很熟 提交于 2019-12-22 06:28:11
问题 I wrote a simple example: #include <iostream> int main() { void* byte1 = ::operator new(1); void* byte2 = ::operator new(1); void* byte3 = malloc(1); std::cout << "byte1: " << byte1 << std::endl; std::cout << "byte2: " << byte2 << std::endl; std::cout << "byte3: " << byte3 << std::endl; return 0; } Running the example, I get the following results: byte1: 0x1f53e70 byte2: 0x1f53e90 byte3: 0x1f53eb0 Each time I allocate a single byte of memory, it's always 16 bytes aligned. Why does this happen

Can you define the size of an array at runtime in C

时间秒杀一切 提交于 2019-12-18 16:48:21
问题 New to C, thanks a lot for help. Is it possible to define an array in C without either specifying its size or initializing it. For example, can I prompt a user to enter numbers and store them in an int array ? I won't know how many numbers they will enter beforehand. The only way I can think of now is to define a max size, which is not an ideal solution... 回答1: Well, you can dynamically allocate the size: #include <stdio.h> int main(int argc, char *argv[]) { int *array; int cnt; int i; /* In

Does ::operator new(size_t) use malloc()?

好久不见. 提交于 2019-12-18 13:01:44
问题 Does ::operator new(size_t) call malloc() internally, or does it use system calls / OS-specific library calls directly? What does the C++ standard say? In this answer it says that: malloc() is guaranteed to return an address aligned for any standard type. ::operator new(n) is only guaranteed to return an address aligned for any standard type no larger than n , and if T isn't a character type then new T[n] is only required to return an address aligned for T . And that suggests that new()

Dynamic memory allocation for pointer arrays

本秂侑毒 提交于 2019-12-18 12:12:57
问题 I'm am trying to write a program that reads in a series of strings from a text file and stores these in an array of strings, dynamically allocating memory for each element. My plan was to store each string in an array using a pointer and then grow the array size as more were read in. I am having trouble to understand why my test code below is not working. Is this a workable idea? char *aPtr; aPtr =(char*)malloc(sizeof(char)); aPtr[0]="This is a test"; printf("%s",aPtr[0]); 回答1: In C a string

Mixing operator new[] and placement new with ordinary delete[]

☆樱花仙子☆ 提交于 2019-12-18 05:38:32
问题 Just out of curiosity, is the following legal? X* p = static_cast<X*>(operator new[](3 * sizeof(X))); new(p + 0) X(); new(p + 1) X(); new(p + 2) X(); delete[] p; // Am I allowed to use delete[] here? Or is it undefined behavior? Similarly: X* q = new X[3](); (q + 2)->~X(); (q + 1)->~X(); (q + 0)->~X(); operator delete[](q); 回答1: I'm pretty sure both give UB. §5.3.4/12 says the array form of a new expression may add some arbitrary amount of overhead to the amount of memory allocated. The array

Pimpl idiom without using dynamic memory allocation

爱⌒轻易说出口 提交于 2019-12-17 21:47:19
问题 we want to use pimpl idiom for certain parts of our project. These parts of the project also happen to be parts where dynamic memory allocation is forbidden and this decision is not in our control. So what i am asking is, is there a clean and nice way of implementing pimpl idiom without dynamic memory allocation? Edit Here are some other limitations: Embedded platform, Standard C++98, no external libraries, no templates. 回答1: Warning: the code here only showcases the storage aspect, it is a