+ 55
The short answer is that this is just how C++ was designed. There's no technical limitation; there are languages out there that are able to do what you're suggesting. Generally you've got two ways to allocate memory-- The stack, and the heap. Stack memory is given to your program by the operating system when it first starts. Because it has a chunk of memory to work with up-front, working with it is very quick (the stack is fast for additional reasons, but that's part of it). This is where the benefit of knowing exactly how much space your program needs comes in. Heap memory, on the other hand, can be requested at run-time. Instead of receiving the memory up-front, the program can request memory as it needs it. It sounds like what you want is a language that does this for everything. Allocating memory at run-time is very slow in comparison to using the stack, however. The OS needs to scan the memory for a contiguous section in which it can store the type you want to work with. C++ is focused on speed, so it chooses to use a stack in addition to the heap. If you wanted, you could define your own stack structure and store that on the heap, and dynamically allocate every variable you want to use. You're going to suffer for it, though. As I mentioned earlier, there are languages where types don't need to be specified (e.g., Ruby, PHP). Some languages use what's called dynamic typing, where types are determined at the run-time and can change over the course of the program. This, of course, will almost always require dynamic memory allocation. Finally, C++11 introduced the "auto" keyword which can infer a type from context. For example: auto x = 5; // x is an int This is a very big topic, but one other thing I'll mention is that you lose the safety of compile-time checks with dynamic typing. There's no way to know if your program is valid until you run it.
9th Apr 2017, 8:49 PM
Squidy
Squidy - avatar
+ 15
Some languages do have a var keyword that selects a type once something is assigned (sometimes it has to be assigned immediately), or that can change its type by being assigned something else, but those come with their own problems. Specifically, how do you know what type it's storing at any given moment? Or how do you know what var turned out to be with its given value (an int of what size, a float or a double, etc.)?
9th Apr 2017, 8:39 PM
Tamra
Tamra - avatar
+ 11
honestly i prefer specifying the type rather than using var or something. its easier for reading and maintaining code
9th Apr 2017, 9:11 PM
Edward
+ 11
in addition to @Squidy 's answer (because really it can't get any better than that), you can in a way do abstract data types. This requires the use of void* but you still have to change the type when trying to use it or the program won't know what to do with it.
10th Apr 2017, 12:25 AM
SoraKatadzuma
SoraKatadzuma - avatar
+ 8
C++ requires that variables have a type. You could use the auto keyword as a workaround.
14th Apr 2017, 9:56 AM
AtK
+ 7
We specify the variable type to tell the compiler how much chunk of memory space shall it reserve. We do this for memory space management. Imagine declaring a char variable and reserving for it a memory space similar to long double's, which is a total waste of precious memory space. More over, if you imagine the size memory space available in older version of computers, it is way too much smaller than the machines of this age and languages like C++ are from the bible age of computation. On another aspect, specifying the variable type during code writing helps speed up program execution since there is an early, compile time, binding of data type unlike other languages which perform binding at execution time, which slightly delays program execution time.
9th Apr 2017, 8:44 PM
Zemichael
Zemichael - avatar
+ 6
Some languages do let you specify a variable that can hold anything (VBA has a variant type for instance) and although they can be very useful they usually also take up more memory and are slower. One of the problems is that as well as holding a value it also has to know what type of value it's holding so that it doesn't do anything that shouldn't be done such as trying to divide 17 by a filename for example. Also when you declare a variable as a specific type it means that you can't accidentally assign an incorrect value to it. If x is an integer it will tell you that something is wrong if you accidentally try to assign x = "a12" whereas having a universal variable wouldn't give an error and it could be very hard spotting a bug like this in a large project.
9th Apr 2017, 8:35 PM
Shane
+ 5
c++ and some other block-designed languages (like c and java) are like this, they are compiled, so they need to know how much space each function or object needs before compilation, while other languages you may have worked with are scripting languages, like python, javascript or ruby, they dont compile, they are interpreted line by line and every variable is considered a string. Ans thats why a concept like bignum is so easy in python but a headache in c++.
9th Apr 2017, 10:38 PM
Mohammad Ganji
Mohammad Ganji - avatar
+ 5
i agree with you! theres some surious profiling going on in coding! (being silly)
24th Apr 2017, 7:30 PM
DeleteThisAccount
+ 4
use python
13th Apr 2017, 11:40 AM
Supersebi3
Supersebi3 - avatar
+ 4
it's a way to manage memory, you specify sizes,to allocate just what you need and also this is why we have dynamic allocation !
17th Apr 2017, 2:53 AM
Ayoub Lebhal
Ayoub Lebhal - avatar
+ 3
Lua, Java Script, and Swift don't need to be defined whether it's an integer or a string or a char, etc. there might be some more languages as well
20th Apr 2017, 2:22 AM
Xander A.
Xander A. - avatar
+ 3
When we compile a code, the compiler will allocate memory to variables and implement built in functions on them. The problem here is that information exists in different types like integer data, string. data, decimal number data and different set of methods exist for each type. Specifying the data type of a variable makes it easy for the compiler to ensure that only valid operations and methods are applied on the corresponding data. or to understand it more practically, it allows to spot onvalid data operation errors in compile time instead of crashing the entire program in runtime. example: string data has different set of methods like concatenate, substr etc. Note: float data type has an entirely different hardware implementation inside alu.
24th Apr 2017, 3:45 PM
Krishna Sai Vootla
Krishna Sai Vootla - avatar
+ 2
In C++ variables are declared and can also be assigned at the same time that is how Syntex is built and also we assigned so that any time we want to change the memory space we just change the variable
11th Apr 2017, 1:54 PM
calvin baiden
calvin baiden - avatar
+ 2
Consider a scenario, you are creating variable "a=20" without defining its type to "int". Another person reusing your code may reinitialize "a="hello"". then still his program compiles and get executed without error and showing/storing wrong information. Happy Coding...😊
13th Apr 2017, 6:06 PM
Sandip Bhambre (Sandy)
Sandip Bhambre (Sandy) - avatar
+ 1
Because based on the type only the memory will be allocated to the variable
13th Apr 2017, 4:43 AM
PADDY
PADDY - avatar
0
in c++ that is...python works without specifying
12th Apr 2017, 1:18 PM
Frankline Maina
Frankline Maina - avatar
0
The data type of a variable declares the type of data that a variable can store and how much memory is required to store this data. The data type also defines the format in which a data of particular type should be stored. Source : http://www.techcrashcourse.com/2015/05/c-programming-language-data-types.html
15th Apr 2017, 7:12 AM
Arun Kumar
Arun Kumar - avatar
0
ntoh..ambo pun hok barghu lg belaja.Dok tahu sangak..
24th Apr 2017, 4:23 PM
Xie
0
you need to specify type of variable so that the compiler can reserve a certain amount of memory large enough to store the value of the declared type without loss of value. Compilers needs to do that because sizeof integral datatype depends on the underlying OS. As a result the sizeof char may not be same across all target OS. This has to do with a fact that in the initial days of computing memory was an expensive resource, so why use 4 bytes for something that can be safely represented in 1 byte. Due to OS limitations the object model(internal representation of data when program is active in memory) of various programming languages have evolved in different ways. Knowing the size of data type beforehand allows compilers, linkers and loaders of various languages to make various types of optimizations like packing data, aligining data on byte, word or dword boundaries, resulting in efficient and faster data access. languages like java or c# which compiles to virtual machine specification ensures size of datatype is same across all platforms have made life easier for programmers but the dirty work is still done by virtual machines and runtimes environment for the target systems (OS).
11th Jun 2017, 1:19 PM
NeutronStar
NeutronStar - avatar