dynamic-memory-allocation

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

£可爱£侵袭症+ 提交于 2019-11-30 09:43:18
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... Well, you can dynamically allocate the size: #include <stdio.h> int main(int argc, char *argv[]) { int *array; int cnt; int i; /* In the real world, you should do a lot more error checking than this */ printf("enter the amount\n"); scanf("%d

What would realloc do if there is no sequential space of memory?

血红的双手。 提交于 2019-11-30 08:53:13
realloc is used to reallocate the memory dynamically. Suppose I have allocated 7 bytes using the malloc function and now I want to extend it to 30 bytes. What will happen in the background if there is no sequential (continously in a single row) space of 30 bytes in the memory? Is there any error or will memory be allocated in parts? realloc works behind the scenes roughly like this: If there is enough free space behind the current block to fulfill the request, extend the current block and return a pointer to the beginning of the block. Else if there is a large enough free block elsewhere, then

Dynamic memory allocation for pointer arrays

孤街醉人 提交于 2019-11-30 06:44: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]); In C a string is a char* . A dynamic array of type T is represented as a pointer to T , so for char* that would be char**

nothrow or exception?

天大地大妈咪最大 提交于 2019-11-30 03:10:57
问题 I am a student and I have small knowledge on C++, which I try to expand. This is more of a philosophical question.. I am not trying to implement something. Since #include <new> //... T * t = new (std::nothrow) T(); if(t) { //... } //... Will hide the Exception, and since dealing with Exceptions is heavier compared to a simple if(t) , why isn't the normal new T() not considered less good practice, considering we will have to use try-catch() to check if a simple allocation succeeded (and if we

new operator for memory allocation on heap

大城市里の小女人 提交于 2019-11-30 01:29:59
I was looking at the signature of new operator. Which is: void* operator new (std::size_t size) throw (std::bad_alloc); But when we use this operator, we never use a cast. i.e int *arr = new int; So, how does C++ convert a pointer of type void* to int* in this case. Because, even malloc returns a void* and we need to explicitly use a cast. There is a very subtle difference in C++ between operator new and the new operator. (Read that over again... the ordering is important!) The function operator new is the C++ analog of C's malloc function. It's a raw memory allocator whose responsibility is

Heap/dynamic vs. static memory allocation for C++ singleton class instance

爷,独闯天下 提交于 2019-11-29 17:34:17
问题 My specific question is that when implementing a singleton class in C++, is there any substantial differences between the two below codes regarding performance, side issues or something: class singleton { // ... static singleton& getInstance() { // allocating on heap static singleton* pInstance = new singleton(); return *pInstance; } // ... }; and this: class singleton { // ... static singleton& getInstance() { // using static variable static singleton instance; return instance; } // ... };

Does malloc reserve more space while allocating memory?

梦想与她 提交于 2019-11-29 15:58:45
问题 I am observing the following behavior in my test program: I am doing malloc() for 1 MB and then free() it after sleep(10) . I am doing this five times. I am observing memory consumption in top while the program is running. Once free() -d, I am expecting the program's virtual memory (VIRT) consumption to be down by 1 MB. But actually it isn't. It stays stable. What is the explanation for this behavior? Does malloc() do some reserve while allocating memory? 回答1: Once free() -d, I am expecting

Default capacity of std::string?

早过忘川 提交于 2019-11-29 13:57:23
When I create a std::string using the default constructor, is ANY memory allocated on the heap? I'm hoping the answer does not depend on the implementation and is standardized. Consider the following: std::string myString; Unfortunately, the answer is no according to N3290. Table 63 Page 643 says: data() a non-null pointer that is copyable and can have 0 added to it size() 0 capacity() an unspecified value The table is identical for C++03. It is implementation dependent. Some string implementations use a small amount of automatically allocated storage for small strings, and then dynamically

What would realloc do if there is no sequential space of memory?

对着背影说爱祢 提交于 2019-11-29 12:41:01
问题 realloc is used to reallocate the memory dynamically. Suppose I have allocated 7 bytes using the malloc function and now I want to extend it to 30 bytes. What will happen in the background if there is no sequential (continously in a single row) space of 30 bytes in the memory? Is there any error or will memory be allocated in parts? 回答1: realloc works behind the scenes roughly like this: If there is enough free space behind the current block to fulfill the request, extend the current block

how do I allocate one block of memory with new?

久未见 提交于 2019-11-29 12:22:16
I have a two dimensional array that I've allocated dynamically using new. The problem is I want to allocate the memory as one connected block instead of in separated pieces to increase processing speed. Does anyone know if it's possible to do this with new, or do I have to use malloc? Here's my code: A = new double*[m]; for (int i=0;i<m;i++) { A[i]= new double[n]; } This code causes a segmentation fault phi = new double**[xlength]; phi[0] = new double*[xlength*ylength]; phi[0][0] = new double[xlength*ylength*tlength]; for (int i=0;i<xlength;i++) { for (int j=0;j<ylength;j++) { phi[i][j] = phi