+ 4

(C/C++) Is it a logical way to give the size of an array from the user?

9th Jan 2018, 7:24 AM
Ali Z. Emili
Ali Z. Emili - avatar
15 odpowiedzi
+ 2
C++ standard doesn't allow it. The size must be known at compile time. SoloLearn uses gnu compiler, which accepts it. Personally, I think it is good thing, but most discussions I've read on the subject disagree.
9th Jan 2018, 7:33 AM
John Wells
John Wells - avatar
+ 5
@mohammed I know how to do it I’m wondering if there’s no problem to do in this way cause i’ve read an article about it and it says it will capture more than that size from the memory and it will effect on the speed of run then. I don’t know if it’s true. That’s why I asked.
9th Jan 2018, 7:39 AM
Ali Z. Emili
Ali Z. Emili - avatar
+ 5
Thank you guys. 👍
9th Jan 2018, 7:47 AM
Ali Z. Emili
Ali Z. Emili - avatar
+ 4
@shobhit won’t it have bad effects on the speed of programs?
9th Jan 2018, 7:42 AM
Ali Z. Emili
Ali Z. Emili - avatar
+ 4
it will have but suppose you know that size of array < 1000001 then you can. Because you may to do it otherwise as well. Abstain from doing it if not required.
9th Jan 2018, 7:46 AM
shobhit
shobhit - avatar
+ 4
Answers given above look rather theoretical. I see the question from more practical position. There's no such practical task as just 'allocate RAM of this size'. Array is memory resource abstraction. In real life you MUST CONTROL RESOURCES you code uses so idea to allocate any resource without handling it's availability is WRONG. Code must survive most real conditions if It's not engeneered for dedicated hardware. User has no idea on memory available so it's coder's responsibility to handle resources. From this practical point of view it's not 'logical', isn't it?
9th Jan 2018, 11:24 AM
Andrey Smirnov
Andrey Smirnov - avatar
+ 3
9th Jan 2018, 7:31 AM
shobhit
shobhit - avatar
+ 3
@shobhit exactly
9th Jan 2018, 7:32 AM
Ali Z. Emili
Ali Z. Emili - avatar
+ 3
@alizafar as john mentioned sometimes when compiler(other than gnu) doesn't allow the size of array to defined at runtime, you can the declare the array with a number that is bigger than what you may encounter. Something like this arr[1000001];
9th Jan 2018, 7:40 AM
shobhit
shobhit - avatar
+ 3
yep! this was my first problem while learning arrays in c++
9th Jan 2018, 7:50 AM
Saurabh Tiwari
Saurabh Tiwari - avatar
0
The cost of gnu doing it for you is cheaper than using the Array template, which people defending the standard suggest using to achieve the result. The compiler allocates a pointer and all usages have the cost of deferencing that pointer. It location isn't something I've looked up as yet. I suspect it is on the heap and allocated via new as that would be the easiest for the compiler to generate code for.
9th Jan 2018, 7:49 AM
John Wells
John Wells - avatar
0
Which is obsolete as 2003, 2011, & 2017 standards replaced it.
9th Jan 2018, 7:56 AM
John Wells
John Wells - avatar
0
you can make the loop time like if user enter my array size should be 5 then you can follow this... for(i=0;i<n;i++) { cin<<a[i]; }
16th Jan 2018, 4:26 PM
Atulpg7
Atulpg7 - avatar