static memory allocation like dynamic memory allocation

0 votes
int r, c;
cin >> r >> c;
int matrix[r][c];

I don't understand the concept of runtime allocation. 

The goal is to allocate memory during runtime, but we are doing the same thing in the above section of code.

When this section of code is executed, the input sizes are given at runtime, and the matrix is allocated memory based on the size of the rows and columns, so is it static or compile time allocation?

Jun 2 in C++ by Nicholas
• 2,460 points
18 views

1 answer to this question.

0 votes

This declaration

int r, c;
cin >> r >> c;
int matrix[r][c];

is a variable-length array declaration.

This array has a storage duration that is set automatically. 

And when the values of the variables r and c are known, the array is created at run-time.

Variable length arrays, on the other hand, are not a standard C++ feature.

You could use the standard container std::vector instead of the array.

All elements of the matrix will be zero-initialized if you use std::vector> matrix(r, std::vector(c)).

answered Jun 7 by Damon
• 3,580 points

Related Questions In C++

0 votes
0 answers

static memory allocation like dynamic memory allocation

int r, c; cin >> r >> c; int ...READ MORE

Jun 6 in C++ by Nicholas
• 2,460 points
19 views
0 votes
1 answer

setuptools: build shared libary from C++ code, then build Cython wrapper linked to shared libary

There is a seemingly undocumented feature of setup that ...READ MORE

answered Sep 11, 2018 in Python by Priyaj
• 58,080 points
190 views
0 votes
1 answer

setuptools: build shared libary from C++ code, then build Cython wrapper linked to shared libary

There is a seemingly undocumented feature of setup that ...READ MORE

answered Sep 21, 2018 in Python by Priyaj
• 58,080 points
1,372 views
0 votes
1 answer

How to pass large records to map/reduce tasks?

Hadoop is not designed for records about ...READ MORE

answered Sep 25, 2018 in Big Data Hadoop by Frankie
• 9,830 points
787 views
0 votes
1 answer

Cases of static and dynamic binding in C++

When an object's static type is used to associate it with a member function, this is known as static binding (understand the type of its class). When a pointer or reference is associated with a member function based on the dynamic type of the object, this is known as dynamic binding (understand the instance of the variable at runtime). Before continuing, keep in mind that dynamic binding only works with pointers, references, and virtual functions for the base class. Because everything needed to call the function is known at compile time, the first call is a static binding (also known as early binding). Derived1 d1(1, 10); d1.display_data(); You already know that the d1 instance is a Derived1 automatic variable, and that it will call the Derived1::display data method (). The first condition is incorrect: d1 is neither a pointer nor a reference. The second condition isn't acceptable:  There is no virtual Derived1::display data. The second call is for ...READ MORE

answered Jun 7 in C++ by Damon
• 3,580 points
14 views
0 votes
1 answer

The static keyword and its various uses in C++

Static variables exist during the "lifetime" of the translation unit in which they are declared, and: It cannot be accessible from any other translation unit if it is in a namespace scope (i.e. outside of functions and classes).  This is referred to as "internal linking" or "static storage lifetime."  (Except for constexpr, do not do this in headers; otherwise, you would wind up with a different variable in each translation unit, which is really confusing.) If it is a variable in a function, it, like any other local variable, cannot be accessed from outside the function.  (This is the mentioned local) Class members have no limited scope owing to static, but they may be referenced from both the class and an instance (like std::string::npos). locations as code: static std::string namespaceScope = "Hello"; void ...READ MORE

answered 1 day ago in C++ by Damon
• 3,580 points
6 views
0 votes
1 answer

Hive partitioning: Dynamic and static

Partitions are created depending on the end ...READ MORE

answered Feb 1, 2019 in Big Data Hadoop by Omkar
• 69,210 points
4,697 views
0 votes
1 answer
0 votes
1 answer

How to enable dynamic resource allocation in Spark?

To dynamically enable dynamic resource allocation, you ...READ MORE

answered Mar 12, 2019 in Apache Spark by veer
1,030 views
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP