C++ Tutorial – 27 – Protecting Arrays with const

[ad_1]
Facebook –
GitHub –
Google+ –
LinkedIn –
reddit –
Support –
thenewboston –
Twitter –


Posted

in

by

Tags:

Comments

35 responses to “C++ Tutorial – 27 – Protecting Arrays with const”

  1. alfons bara Avatar

    Was not populate an int func, shouldent that func to return an int?

  2. Ернар Ерболов Avatar

    Thanks for the video. I have a qestion. why you didn't write an operator return in populate function?

  3. Cody Kiser Avatar

    Im assuming it has something to do with him using DevC++. That ide/compiler is buggy and old

  4. Spartasaurus Rex Avatar

    i wrote return temp and it worked then

  5. FF Fanatic Avatar

    That variable will be optimized out by anything that calls itself an optimizer. What should be done is to have a constant to represent the size and to use the constant in place of the magic number. The compiler will most likely replace each use with the value, but it's much easier to change later on.

  6. FF Fanatic Avatar

    The compiler doesn't have to do anything when you don't return a value from a function other than main that has a return value. It's simply undefined behaviour. That said, most compilers will at least warn you. According to the standard, reaching the end of the function without returning anything is equivalent to return;, which definitely does not have any well-defined behaviour when there is a return type.

  7. 100GPing100 Avatar

    You don't need to return the size of the array when it is a constant (in this case 5), therefore the programmer already knows the size of the array. Because of this the return type could be void without a problem, you would just not do:
    int size = populate(bucky,5);
    You would do:
    int size = 5;
    populate(bucky,5);

    He didn't have to create and use the variable size because the size is a constant set by him. The use of the variable size is just wasting memory.

  8. 100GPing100 Avatar

    Their problem is that usually when you declare a function with a return type you have to return something or the compiler gives you an error.
    No errors are given with some compilers where the default value of the type is returned (which is what happens in this case).

  9. 100GPing100 Avatar

    I know how it works, I'm asking why he used memory on a temporary variable when he could've read from the input stream straight into the array.

  10. Charice Super Avatar

    cin >> bucky[i]

    we know i runs in a loop, remember i++.
    say i goes up to 5

    cin >> bucky[i]

    is like

    cin >> bucky[0]
    cin >> bucky[1]
    cin >> bucky[2]
    cin >> bucky[3]
    cin >> bucky[4]

    I'm showing this without the loop.

  11. Charice Super Avatar

    something is wrong with your loop

  12. katulobotomia Avatar

    Fix populate function to void, no need to create "int size…" and just populate and spit after "double items[5];"

  13. shiju thomas Avatar

    hey big fan, dont get the point of this video if ur not changing the values in the array in the function spit, since ur trying to show us that the array values dont change when it returns back after calling spit.

  14. aldemos Avatar

    the compiler doesnt print out a error message because:
    bucky = notch, but for c++.. :D:D

  15. 100GPing100 Avatar

    cin >> bucky[i]; ?

  16. TheBeresford7 Avatar

    yeh ever since tutorial 20 ive exactly copied everything uve done and it always gives me errors

  17. Ted Avatar

    pretty much the same to previous one ,,,SO talk the main point more, which is difference between the two

  18. Main Avatar

    in 6:45 he says that the size is the return of populate

  19. Harsh Gupta Avatar

    int populate must return a value but there's no error comes out in your Dev C++ compiler.

  20. Adam Avatar

    @AutomatonGames A function is void if it is not returning something, example: an integer; int sum ;
    A function that starts with an (int or other ) data type is expecting the function to return a value of that data type. int functions return int values. Hope this helps. Respectfully!

  21. Alex o Avatar

    @aXque No need for size.

  22. Alex o Avatar

    Whats the point of the size? populate() does not return anything. And you dont do anything with size

  23. double0seven Avatar

    @RaamAnkka this is why you should use Netbeans (C++ IDE), because it will tell you right away "this variable is not used!"

  24. RaamAnkka Avatar

    @Mahiz96 So there is no use for it really?

  25. RaamAnkka Avatar

    @Mahiz96 Yes it is, in the main function you have int size

  26. RaamAnkka Avatar

    what is int size for?

  27. KLD Avatar

    f*ck this ,
    I did it more than 4 times , it didn't work

  28. Steinar Avatar

    I wrote "cout << size;" to test, and got "5" printed out.. can anyone explain to me why?

  29. Jozef Podlecki Avatar

    @xanhort return bucky

  30. Jozef Podlecki Avatar

    @2ndMilitary okay
    return 2ndMilitary * 0;

  31. Prettyboredrightnow Avatar

    When I ran it it showed me all the addresses of the items after what you had on your screen. Not sure what I did wrong there….

  32. enesog Avatar

    size = populate(items, 5);

    I know you can do it but at this moment it does not make sense.

  33. ModernTechLife Avatar

    Oh I fixed it. I kept populate as int and just gave it a return value like this.
    "return i;"

Leave a Reply

Your email address will not be published. Required fields are marked *