Like Tree13Likes

passing array to function

This is a discussion on passing array to function within the C++ Programming forums, part of the General Programming Boards category; Originally Posted by phantomotap O_o Potentially true for every mistake; why are you treating points/iterators with such fear? Seriously, why ...

  1. #31
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,772
    Quote Originally Posted by phantomotap View Post
    O_o

    Potentially true for every mistake; why are you treating points/iterators with such fear?

    Seriously, why are you programming in C/C++? Every post of yours that proselytizes C++ references features largely available to other languages. (For example, Python has scoped destruction and generics through decorators/"monkey patching" sufficient for most of your work.) Every post of yours complaining about C++ is referencing problems largely solved in other languages. (You may look no further than C#, with which you'd be partly familiar, to find many do not exist.) Every time you insist a newbie write code to babysit clients you are telling them to do things other languages (Ada/Java/PHP/Python/Ruby) do automatically.

    I know I've asked before, but I strongly recommend you branch out from C/C++ as it really doesn't seem a good fit for you.

    Soma
    I treat it with fear because all it takes is an invalid pointer to crash a program and it's something that's pretty hard to recover from. It's horrible for writing stable programs.
    But as for why C++... because it's my favourite language, it's the language I'm most familiar with and it's the one I love most. Especially since I pretty much learned C++ before other languages, I'm biased and find that the way other languages work conflict with how I think and feel.

    Yeah, I know of C#. I've used it. I use for all my GUIs because all GUI frameworks I know of for C++ suck (require dedicated IDEs which suck or just completely fail to use modern C++ idioms; a lot of frameworks still rely on tons of pointers, for example; tons of frameworks which I can find no WYSIWYG editor for visual layout of forms, etc, etc, etc).

    I hate Java because it has so many restrictions (e.g., no multiple inheritance (and no, implementing interfaces do not count), no public/private/protected inheritance, no pointer to pointers, no operator overloading, etc, etc), everything uses camel case, uses tons of "type polling" and dynamic dispatch, etc, etc.

    Haven't programmed in Python and all GUIs I've seen in Python look friggin' awful, so that scares me.

    And I have yet to see a language that offers the performance or features of C++ while still remaining easy. Templates? Meta programming? Compile-time evaluation? Extremely generic functions/objects?

    You know, I'm not a professional programmer. I am a hobbyist programmer. My exposure to programming languages are those I've chosen to study [or have been required to due to university requirements] because I've needed them for something that I wanted to do. My drive to learn programming drove me from VB to C++.

    My interests also lie in hardware, so hardware-near languages make a lot of sense to me, too. Yet, C is TOO close, so C++ is kind of the middle ground, the do-everything language, kinda, so to speak.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  2. #32
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    1,192
    Eh, Python is okay, I guess. It's kind of like C++ in the sense that it'll just do whatever you want it to but with far more hand holding and waaaay less low level features. I don't even think Python has pointers, does it? Personally, I think it's awful but it's good for small and dirty scripts. It's also good for scientists who maybe want to learn how to dabble in programming.

    You're right though, Elysia, pointer arithmetic errors are hard to recover from if not downright impossible. I don't think you can try-catch your way out of a seg fault, right? But that comes with the territory though. One of the things I really enjoy about C is how little it holds your hand. The computer's response is, "Hey buddy, you told me to read at address 40123432368 so I did and if it doesn't work, that's your fault."

    But all this talk of arrays is silly. I do like the idea of passing beginning and end pointers, assuming contiguous blocks of memory because then you can just use curr < end and it'll work perfectly but std::array solves all these issues so simply I can't believe this topic is actually growing this large. It's a nice container that's full proof because it supports all the classic iterators you'd expect (.begin(), .end()) and it has .size() member function as well. std::array is awesome and if dynamic storage is required then there's vectors.

    This is C++ guys, what are we doing passing a pointer and the proposed size?

  3. #33
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,772
    Quote Originally Posted by MutantJohn View Post
    You're right though, Elysia, pointer arithmetic errors are hard to recover from if not downright impossible. I don't think you can try-catch your way out of a seg fault, right?
    It is possible, both via compiler extensions (catch(...) in Microsoft's compiler, for instance) and via platform-specific ways such as SEH. They aren't perfect, of course. A mistake will still cost you if you haven't fool-proofed your code.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #34
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,738
    But all this talk of arrays is silly.
    I would normally agree, but that is the topic, and readers of the thread know why the current discussion came about.

    This is C++ guys, what are we doing passing a pointer and the proposed size?
    The STL is not a reason to avoid using that interface. I would even argue that the STL uses it in some algorithms, such as generate_n or fill_n or anything_n. The types aren't as primitive as the C version but the interface is the same.

  5. #35
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,332
    I treat it with fear because all it takes is an invalid pointer to crash a program and it's something that's pretty hard to recover from.
    O_o

    The issue isn't exclusive to pointers; you are just fooling yourself.

    You pass the wrong value to a systems API: huge crash. (You can bring down entire systems.)
    You pass the wrong value to a GUI API: loss of data. (Your clients would rather see a recoverable crash.)
    You pass the wrong value to a user API: massive security breach. (Your clients will simply find other solutions.)

    C++ doesn't "babysit", and you want a "babysitter". In fact, you want a "babysitter" so much you attempt to force other coders to be a "babysitter" and code such mechanic into everything they write.

    This isn't some stupidity about being a "Hardcore C++ Programmer"; part of using a language like C/C++ is the development cost associated with such serious problems when the language will not even try to protect you; you should take a long look at your own history at this forum.

    How many times have you tried tried to cram `std::shared_ptr' down a newbies throat for the implementation of low level facilities? How many times have you continued this behavior after having it explained that `std::shared_ptr' actually complicates the implementation because the "values" held by the `std::shared_ptr' must be manipulated for every operation in any event. Take a look at the "linked list" thread where you started throwing `std::shared_ptr' at everything without even understanding the problem with circular mutually held `std::shared_ptr' objects. You are so afraid of fiddly issues revolving around pointers that you attempt to throw `std::shared_ptr' at them without even understanding the problems, and you've tried many times to force newbies into the same mistakes.

    I'm not trying to be mean; you are a great C++ programmer and very valuable contributor here when you aren't proselytizing. The fact remains, everything you say about C++ tells me you'd be much happier with other languages because your issues with pointers are the very tip of a colossal API iceberg. C++ doesn't protect you, and you desperately want to be protected. Why are you unwilling to see what other languages offer in that direction?

    And I have yet to see a language that offers the performance or features of C++ while still remaining easy. Templates? Meta programming? Compile-time evaluation? Extremely generic functions/objects?
    How could you have seen such languages when you don't look for them?

    As you should know, meta-programming and compile-time evaluation are unintended consequence of templates. Have you ever really looked at functional languages where the very idea of "function"/"object"/"programming" are "first-order"? No. You have not really looked at such languages.

    Look even only as far as JavaScript (Yes. I realize that JavaScript isn't compiled, but that isn't here the point. I know that you claim to have used JavaScript on some medium projects so I know you have at least some familiarity with JavaScript.). You can create a JavaScript function which creates an entirely new mechanic for "objects". You don't need "template aliases", "CRTP", or manually coded "meta-functions" to do the job as even the very idea of "object", how JavaScript expresses almost everything, is open to your manipulation by design.

    You want meta-programming? Imagine being able to tell the C++ mechanism of inheritance that "every object should have a virtual destructor" and give that method a default implementation you decide without decorating code or objects in any way. You can do exactly that with JavaScript/Python/Ruby, and I am sure you could do similar things with other languages. Yes, I realize this isn't "true meta-programming" because these things happen during execution, but now I have to reference LISP and the common object system which can be compiled.

    The experience with using these other languages for serious projects will only, at worse, make you a more diverse developer so you wouldn't be wasting your time even if you come back to C++ every time.

    Don't worry, I will not again speak on this topic.

    Soma
    “Often out of periods of losing come the greatest strivings toward a new winning streak.” -- Fred Rogers
    “Salem Was Wrong!” -- Pedant Necromancer

  6. #36
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,772
    Quote Originally Posted by phantomotap View Post
    O_o

    The issue isn't exclusive to pointers; you are just fooling yourself.

    You pass the wrong value to a systems API: huge crash. (You can bring down entire systems.)
    You pass the wrong value to a GUI API: loss of data. (Your clients would rather see a recoverable crash.)
    You pass the wrong value to a user API: massive security breach. (Your clients will simply find other solutions.)
    Eh, I did not say it's exclusive to pointers. I did say pointers were scary.

    C++ doesn't "babysit", and you want a "babysitter". In fact, you want a "babysitter" so much you attempt to force other coders to be a "babysitter" and code such mechanic into everything they write.

    This isn't some stupidity about being a "Hardcore C++ Programmer"; part of using a language like C/C++ is the development cost associated with such serious problems when the language will not even try to protect you; you should take a long look at your own history at this forum.
    Precisely because C++ do not babysit programmers, I feel it is important that people understand how to write robust and safe code, so as to make their programs less buggy and more safe from a security standpoint.
    I don't want babysitting. I don't a language to tell me what I can and can't do. I want a language that produces as few bugs as possible and allows me as much flexibility as possible. Contradiction? I know. It's either one or the other or somewhere in between, but that's what I want, not necessarily what I can get.
    Of course I would try force such ideals on other people. It's precisely what many C++ experts do. They try to teach masses modern C++. I am trying to do the same, even if our methods do not always agree.

    How many times have you tried tried to cram `std::shared_ptr' down a newbies throat for the implementation of low level facilities? How many times have you continued this behavior after having it explained that `std::shared_ptr' actually complicates the implementation because the "values" held by the `std::shared_ptr' must be manipulated for every operation in any event. Take a look at the "linked list" thread where you started throwing `std::shared_ptr' at everything without even understanding the problem with circular mutually held `std::shared_ptr' objects. You are so afraid of fiddly issues revolving around pointers that you attempt to throw `std::shared_ptr' at them without even understanding the problems, and you've tried many times to force newbies into the same mistakes.
    Slight point I want to point out here:
    I have implemented linked lists with smart pointers. I fully know what circular references are. I know what std::shared_ptr does. I know what std::weak_ptr does.
    And I still feel it is a good lesson in learning memory management, particularly because programmers must understand what circular references are and how to avoid them.
    Yes, it may complicate things, but people who come here usually come to help learning C++ and not help write production code. In production code, everything is so difficult, but when learning stuff, it's so simple. You can experiment to your heart's content.

    The fact remains, everything you say about C++ tells me you'd be much happier with other languages because your issues with pointers are the very tip of a colossal API iceberg. C++ doesn't protect you, and you desperately want to be protected. Why are you unwilling to see what other languages offer in that direction?

    How could you have seen such languages when you don't look for them?
    I am not exclusively using C++. I have looked at other languages. I have used other languages. But I am not an expert in any of those languages. I don't even know half of what Java/C#/etc can do, but there are things I do like, such as module systems, so no annoying declarations and includes, for example.
    I am not unwilling to look elsewhere. I do that a lot. I have used, and sometimes am using: C, C++, C#, Java, SQL, HTML, Javascript, ASP.NET (C#), VBA, Windows bat files, Erlang, VHDL, Verilog, SystemVerilog, ADA, Haskell and others I probably forgot to mention. But you know, I'm not doing this as a profession, so I look at other languages if I feel there is something I need to do research on. Can I do this easier some other way? If there is a language that can do that, then I take a look at it. But there is so much out there and learning a language isn't trivial, so you will forgive me if I don't rush out to find and learning everything out there.
    If someone knows some language that might be interesting, then of course I take a look. I might even give it a try and see if I like it. But also, like I said, I kind of grew up with C++, so that's my core mindset: I see everything from a C++ point of view. Any languages that offer less flexibility automatically gets a minus in that category (of course I don't judge the entire language by a single category or a single feature, though).

    Examples I like are the type-strictness in ADA, being able to reference individual bits without needing bit operations in VHDL. There are more, of course...

    As you should know, meta-programming and compile-time evaluation are unintended consequence of templates. Have you ever really looked at functional languages where the very idea of "function"/"object"/"programming" are "first-order"? No. You have not really looked at such languages.
    Even so, meta-programming is cool IMO.
    I have used functional languages, or at least I have used Erlang.

    Look even only as far as JavaScript (Yes. I realize that JavaScript isn't compiled, but that isn't here the point. I know that you claim to have used JavaScript on some medium projects so I know you have at least some familiarity with JavaScript.). You can create a JavaScript function which creates an entirely new mechanic for "objects". You don't need "template aliases", "CRTP", or manually coded "meta-functions" to do the job as even the very idea of "object", how JavaScript expresses almost everything, is open to your manipulation by design.
    Maybe I just don't know enough of the language, but I fail to see what it brings to the table which C++ cannot do in a similar fashion. Javascript is also one of my least favourite languages, in line with Java, because it's just so type unsafe and pretty much allows for everything. There isn't even a way to create a "type" which can be enforced at compile-time.

    You want meta-programming? Imagine being able to tell the C++ mechanism of inheritance that "every object should have a virtual destructor" and give that method a default implementation you decide without decorating code or objects in any way. You can do exactly that with JavaScript/Python/Ruby, and I am sure you could do similar things with other languages. Yes, I realize this isn't "true meta-programming" because these things happen during execution, but now I have to reference LISP and the common object system which can be compiled.
    That's pretty cool, I will admit. It sounds cool, but Javascript I really dislike (though maybe it would help to learn Typescript if it still supports such stuff), and Python and Ruby I really don't know much about. Having to learn one or two entirely new languages from scratch just to try that out is a little daunting.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Page 3 of 3 FirstFirst 123
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 09-25-2012, 01:31 AM
  2. Passing Array To Function & Display Array Contents
    By mcertini in forum C++ Programming
    Replies: 4
    Last Post: 12-10-2010, 12:32 PM
  3. Passing an array to a function
    By will of fortune in forum C Programming
    Replies: 6
    Last Post: 02-09-2010, 02:49 PM
  4. passing 2d array to function
    By Tupcia in forum C Programming
    Replies: 3
    Last Post: 04-13-2008, 11:33 AM
  5. passing 2D array to a function
    By ashesh in forum C Programming
    Replies: 4
    Last Post: 06-09-2003, 11:16 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21