dynamic-allocation

Should I free memory before exit?

自作多情 提交于 2019-11-27 01:51:22
Should I free all my mallocated memory when I am exiting program in the due of error? something = (char**) malloc (x * sizeof(char*)); for (i = 0; i < x; i++) something[i] = (char*) malloc (y + 1); ... if (anything == NULL) { printf("Your input is wrong!"); // should I free memory of every mallocated entity now? exit(1); } else { // work with mallocated entities ... free(something); // it must be here system("pause); } It depends on the OS. Best practice I'd say you should explicitly free it. It also makes using tools like valgrind a PITA if you have memory not freed all over the place and I

Should I free memory before exit?

旧街凉风 提交于 2019-11-26 09:45:34
问题 Should I free all my mallocated memory when I am exiting program in the due of error? something = (char**) malloc (x * sizeof(char*)); for (i = 0; i < x; i++) something[i] = (char*) malloc (y + 1); ... if (anything == NULL) { printf(\"Your input is wrong!\"); // should I free memory of every mallocated entity now? exit(1); } else { // work with mallocated entities ... free(something); // it must be here system(\"pause); } 回答1: It depends on the OS. Best practice I'd say you should explicitly

Dynamica allocation of an unknown matrix in C

孤街浪徒 提交于 2019-11-26 00:49:06
问题 I need to take a file that is inputted by the user and multiply it by another file. That much I know how to do. The problem is one file is an array and the other is a matrix. I need to scan in the first line of the matrix to find the size of the matrix and I then need to dynamically allocate the matrix and array from the files. This is what I have so far: #include <stdio.h> #include <stdlib.h> #include <math.h> #include <string.h> int main() { int row1, col1; //These values need to be pulled

Is it good practice to NULL a pointer after deleting it?

时光怂恿深爱的人放手 提交于 2019-11-26 00:19:14
问题 I\'ll start out by saying, use smart pointers and you\'ll never have to worry about this. What are the problems with the following code? Foo * p = new Foo; // (use p) delete p; p = NULL; This was sparked by an answer and comments to another question. One comment from Neil Butterworth generated a few upvotes: Setting pointers to NULL following delete is not universal good practice in C++. There are times when it is a good thing to do, and times when it is pointless and can hide errors. There

Correctly allocating multi-dimensional arrays

大憨熊 提交于 2019-11-25 22:52:52
问题 The intent of this question is to provide a reference about how to correctly allocate multi-dimensional arrays dynamically in C. This is a topic often misunderstood and poorly explained even in some C programming books. Therefore even seasoned C programmers struggle to get it right. I have been taught from my programming teacher/book/tutorial that the correct way to dynamically allocate a multi-dimensional array is by using pointer-to-pointers. However, several high rep users on SO now tell

How do I declare a 2d array in C++ using new?

ⅰ亾dé卋堺 提交于 2019-11-25 22:13:14
问题 How do i declare a 2d array using new? Like, for a \"normal\" array I would: int* ary = new int[Size] but int** ary = new int[sizeY][sizeX] a) doesn\'t work/compile and b) doesn\'t accomplish what: int ary[sizeY][sizeX] does. 回答1: A dynamic 2D array is basically an array of pointers to arrays . You can initialize it using a loop, like this: int** a = new int*[rowCount]; for(int i = 0; i < rowCount; ++i) a[i] = new int[colCount]; The above, for colCount= 5 and rowCount = 4 , would produce the