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
• 5,300 points
31 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
• 4,960 points

Related Questions In C++

0 votes
0 answers

Static vs dynamic type checking in C++

I'm curious in the differences between static ...READ MORE

Jul 1 in C++ by Nicholas
• 5,300 points
11 views
0 votes
0 answers

memory allocation for objects

When we instantiate a variable in C++, ...READ MORE

Aug 11 in C++ by Nicholas
• 5,300 points
8 views
0 votes
0 answers

memory allocation for objects

In C++, a variable like int x ...READ MORE

2 days ago in C++ by Nicholas
• 5,300 points
2 views
0 votes
0 answers

How to access static members of a class?

I'm learning C++ and Qt, but even the simplest code that I copy and paste from a book produces problems. On Ubuntu 10.04, I'm using g++4.4.2 with the QtCreator IDE.  Is there a distinction between the syntax of the g++ compiler and those of other compilers?  When I try to access static members, for example, something always goes wrong. #include <iostream> using namespace std; class A { ...READ MORE

Jul 7 in C++ by Nicholas
• 5,300 points
12 views
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
• 5,300 points
29 views
0 votes
0 answers

Efficient way to return a std::vector in c++

When delivering a std::vector in a function, how much data is duplicated, and how much of an optimization will it be to place the std::vector in free-store (on the heap) and provide a pointer instead, i.e. is: std::vector *f() { std::vector *result = new ...READ MORE

Aug 11 in C++ by Nicholas
• 5,300 points
8 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
206 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,416 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
• 4,960 points
24 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 Jun 27 in C++ by Damon
• 4,960 points
20 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