dynamic-allocation

c++ Object parameters: polymorphism, value semantics, object lifetimes?

爱⌒轻易说出口 提交于 2019-12-12 15:15:49
问题 As I make the transition from C# to C++ I get a lot of recommendations to use value semantics where possible. It's pretty much guaranteed that if I post a question with a pointer anywhere someone will come along and suggest that it should be a value instead. I'm starting to see the light and I have found a lot of places in my code where I could replace dynamic allocation and pointers with stack allocated variables (and usually references). So I think I have a grasp on using stack allocated

Dynamic array in Fortran 77

瘦欲@ 提交于 2019-12-12 03:05:06
问题 I have to write a subroutine in Fortran 77(i'm using Intel Fortran), which reads the measured values from a text file and stores them in a matrix. Since the number of measured values is always variable, I must dynamically allocate the matrix. I know that the dynamic allocation is only possible from Fortran 90, but at that time people had the same problems, so it is also possible. How would you proceed? I do not want to reserve too much space for the matrix because the method is impractical

resource acquisition failure handling [closed]

馋奶兔 提交于 2019-12-11 23:23:39
问题 Closed . This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed 5 years ago . After years of programming, I havent had a situation where reasonable malloc or new would fail (maybe because my mallocs are trully reasonable), though I always check for it. In my case, apps should gracefully (i hope) close with an appropriate log entry. What would you do in

Why calloc wasn't intended to assign arbitrary values?

南笙酒味 提交于 2019-12-11 22:18:24
问题 As per Why malloc+memset is slower than calloc? malloc + memset is slower than calloc under certain conditions. Why wasn't calloc written in such a way that it can take an extra value argument ( like memset ) to override default assignment by zero? What would have been the effect of that if it were done? 回答1: These calloc or memset initializations operate on a byte level, so even memset with a value different from 0 is not that usefull. At least I don't remember having it used with different

error C2512: no appropriate default constructor available (not classes)

不羁岁月 提交于 2019-12-11 13:48:31
问题 I'm starting out with structures, and I'm having problems dynamically allocating my structure array. I'm doing what I see in my book and on the internet, but I can't get it right. Here's both full error messages: C2512: 'Record' : no appropriate default constructor available IntelliSense: no default constructor exists for class "Record" #include <iostream> #include <string> using namespace std; const int NG = 4; // number of scores struct Record { string name; // student name int scores[NG];

Allocation memory error with use struct for c

六眼飞鱼酱① 提交于 2019-12-11 11:17:11
问题 I wrote a code for managing a library; the compilation is done but during the simulation I obtained an Allocation error (case2) and I don't know why. The first case works correctly but if I entered more than one name in the first case, the second case doesn't work. What did I do wrong? I hope I was clear enough. typedef struct { char name[80]; char **books; int books_num; } Subscription; int main() { // Variables declaration: int option = 0, subs_num = 0, i = 0, books_num = 0; Subscription

Subroutine not returning correct numerical values in assumed shape array due to index renaming in the main program

风格不统一 提交于 2019-12-11 08:28:24
问题 The argument of my fortran 95 subroutine is an assumed shape array with intent inout: the_subroutine(my_argument) real, dimension(:,:), intent(inout) :: my_argument (...) In the main program, I have an allocatable array. I allocate it and also rename indexes . Then I call the subroutine and pass that (correctly allocated) array to the subroutine: allocate(the_array( 5:1005 , 5:1005 )) call the_subroutine(my_argument=the_array) The subroutine does certain calculations and fills the array with

Trouble copying contents of an array into another array…getting weird numbers

£可爱£侵袭症+ 提交于 2019-12-11 08:21:40
问题 My program starts with a dynamically allocated (DA) array. It then prompts the user to enter in a size. If the size entered is within a certain threshold, a new DA array is created, the contents of the old is copied into the new, and the new array is then displayed. I am having trouble copying contents from one dynamically DA array into the other dynamically allocated array. Through each step of the reallocation process I have "print tests" that display the array after each process. I test

Correctly allocating multi-dimensional arrays

为君一笑 提交于 2019-12-11 08:06:49
问题 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

Trying to create copy function in a vector class

╄→尐↘猪︶ㄣ 提交于 2019-12-11 02:45:30
问题 I am working on implementing a vector class but cannot figure out how to write a function to copy one vector into another. template <class T> class Vec { public: //TYPEDEFS typedef T* iterator; typedef const T* const_iterator; typedef unsigned int size_type; //CONSTRUCTOS, ASSIGNMENT OPERATOR, & DESTRUCTOR Vec() {this->create(); } Vec(size_type n, const T& t = T()) { this->create(n, t); } Vec(const Vec& v) { copy(v); } Vec& operator=(const Vec& v); ~Vec() { delete [] m_data; } //MEMBER