dynamic-memory-allocation

How to create a 2D array

对着背影说爱祢 提交于 2021-02-11 15:14:22
问题 Im stil a beginner in C programming and I need a little help writing a code for my C programming class . The prompt is: Input for this program is a two-dimensional array of floating point data located in a file named textfile94. The input array will contain 3 rows of data with each row containing 5 columns of data. I want you to use the two-subscript method of dynamic memory allocation. Use malloc to create an array that holds pointers. Each element of that array points at another array,

Allocate memory 2d array in function C++

六月ゝ 毕业季﹏ 提交于 2021-02-08 12:15:30
问题 I'm trying to dynamically allocate memory for a 2D array inside a function in C++. A question exactly like this has been asked except that it is written using malloc and dealloc, so I was wondering if you could help me convert it to use new and delete. Here is the other question: Allocate memory 2d array in function C I tried changing it to the following code, but I'm getting errors. void assign_memory_for_board(int ROWS, int COLS, int *** board) { *board = new int**[ROWS]; for (int i = 0; i

Array allocation in Fortran subroutine

情到浓时终转凉″ 提交于 2021-02-08 10:13:59
问题 My question is about array allocation in Fortran. I have a subroutine, say readParams , where I want to read some dynamically sized arrays from files. These are also used outside the subroutine. What is the best way to handle this? In F95 it seems to be impossible to allocate within the subroutine and pass the arrays, filled with values, back to the main program. But if I allocate it in the main program and use "intent(inout)" in the subroutine it also gets deallocated there. (I'm using F90

Can I delete a memory previously allocated dynamically, but with a different pointer?

纵然是瞬间 提交于 2021-02-08 07:22:08
问题 I was making a program for linked list in C++. To implement the concept, I created a pointer 'start' globally, pointing to the first element of the list. After completion of the program I tried to delete all memory allocated dynamically to prevent memory leaks, by accessing successive nodes using the start and another locally declared pointer 'p'. Here, I used a pointer pointing to the same correct addresses, but this pointer was not the one used for memory allocation, but was declared

c why realloc isn't working

≯℡__Kan透↙ 提交于 2021-01-28 08:10:43
问题 I have the following code void getPariceArray(Board board, treeNode *tn, Position *dst, int **prices, int *counter, int total) { if (tn == NULL) return NULL; if (tn->position[0] == dst[0][0] && tn->position[1] == dst[0][1]) { prices = (int **)realloc(prices, sizeof(prices) *4); prices[*counter] = (int *)malloc(sizeof(int)); printf("%d", sizeof(prices)); *prices[*counter] = total; *counter = *counter + 1; } int x = tn->position[1] - '1'; int y = tn->position[0] - 'A'; int cellPrice = board[x]

C++ How do streams allocate space for input?

佐手、 提交于 2021-01-28 03:18:16
问题 For example: // is type: std::istream // str type: std::string is >> str; How does this grow str to accommodate the input? It reads character by character and calls str.push_back() (or something similar)? Or does it have a mechanism for knowing the input size before reading the input? I realize that the standard most likely doesn't specify this details, but I am more interested in common implementations (e.g. gcc ). This question is a curiosity as in C you don't know beforehand how much to

What if size argument for std::vector::resize is equal to the current size?

限于喜欢 提交于 2021-01-28 01:59:36
问题 Reading the manual about vector::resize http://www.cplusplus.com/reference/vector/vector/resize/ It only says what happens if the size is greater or smaller, but does not say what happens if it's equal. Is it guaranteed that on equal size it will not reallocate the array and invalidate the iterators? I wanted to avoid one branch and handle only 2 cases (>= or <) instead of 3 (< or > or ==), but if resizing to same size is undefined, then i will have to check that case too. 回答1: This is

dynamic memory allocation in c , free some part of memory that is allocated before using malloc()

北城余情 提交于 2020-08-27 10:08:18
问题 Is there any way to free some part of memory you created by using malloc(); suppose:- int *temp; temp = ( int *) malloc ( 10 * sizeof(int)); free(temp); free() will release all 20 byte of memory but suppose i only need 10 bytes. Can i free last 10 bytes. 回答1: You should use the standard library function realloc . As the name suggests, it reallocates a block of memory. Its prototype is (contained in the header stdlib.h ) void *realloc(void *ptr, size_t size); The function changes the size of