Thread: dynamic-variable-sized array

  1. #31
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Not to mention of course that going over the edge of the allocation is by no means guaranteed to fail in any particular manner - it is simply undefined behaviour.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  2. #32
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    > We hear crash. Perhaps page fault, at best. Not SIGSEGV. Linux talk.
    It's programmer talk, you should know what a signal is. And you should know what a segfault is.

    As far as it goes, if it's in MSDN I'd expect a Windows programmer to acknowledge it . But MSDN is huge, and too I do see your point... so whatever

  3. #33
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,401
    Quote Originally Posted by matsp
    But you do not know under which circumstances the compiler/OS will use these variables. E.g. errno may expand to a macro that uses "_error[current_thread()]" - now try compiling that with a local int called _error...
    I suspect that we could classify that as a compiler bug rather than a user mistake. I would rather say that it is better to avoid such names entirely to avoid actually using a reserved name when a mistake places it in a context where it is reserved.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #34
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,653
    Quote Originally Posted by zacs7 View Post
    > We hear crash. Perhaps page fault, at best. Not SIGSEGV. Linux talk.
    It's programmer talk, you should know what a signal is. And you should know what a segfault is.

    As far as it goes, if it's in MSDN I'd expect a Windows programmer to acknowledge it . But MSDN is huge, and too I do see your point... so whatever
    I would say page fault.
    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.

  5. #35
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Elysia View Post
    I would say page fault.
    However, it may not be that - it may actually be a "segment fault" (general protection error) if the system is running in x86 protected mode using segments.

    If we are not to assume that it is running any particular OS, then don't use terms that are specific to how the OS happens to implement its memory protection.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #36
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,653
    Yes, page fault or seg fault or something. I am not 100% on what those terms mean because I have never needed them.
    I am not a Linux dev.
    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.

  7. #37
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by laserlight View Post
    I suspect that we could classify that as a compiler bug rather than a user mistake. I would rather say that it is better to avoid such names entirely to avoid actually using a reserved name when a mistake places it in a context where it is reserved.
    Really? How would you make the internal implementation of the errno expansion work, then? Most C libraries provide errno as a implementation specific macro that takes into account the current thread so that errno's are thread-safe. Unless we are using C++ where we could use namespaces, the only thing that would make errno expand in a way that is unlikely to collide with user-defined names is to use a name that is reserved for the compiler/library implementation - it may not be the names I've suggested, but it should be a name that is specific to the C-runtime.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #38
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,401
    Quote Originally Posted by matsp
    Not to mention of course that going over the edge of the allocation is by no means guaranteed to fail in any particular manner - it is simply undefined behaviour.
    Speaking of that, I was trying to make sense of the syntax, and apparently int[nnode+1] is a temporary variable length array. This would mean that sizeof(int[nnode+1]) == sizeof(int)*(nnode+1), if I understand variable length arrays correctly. Consequently, the whole point about segfault/page fault would be irrelevant.

    Quote Originally Posted by matsp
    How would you make the internal implementation of the errno expansion work, then?
    Use __errno instead of _errno since __errno is reserved for any use.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #39
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by laserlight View Post
    Use __errno instead of _errno since __errno is reserved for any use.
    Ok, yes. But isn't _xxx also reserved?

    Either way, I think it's not a bad idea to avoid using any prefix based on underscore, just to avoid any possibility of getting into trouble, don't you agree?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #40
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,401
    Quote Originally Posted by matsp
    Ok, yes. But isn't _xxx also reserved?
    In file scope only, so I would think that using it with a macro intended for use in any scope would be a bug.

    Quote Originally Posted by matsp
    Either way, I think it's not a bad idea to avoid using any prefix based on underscore, just to avoid any possibility of getting into trouble, don't you agree?
    Of course, assuming that you are not implementing the implementation.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #41
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by laserlight View Post
    Speaking of that, I was trying to make sense of the syntax, and apparently int[nnode+1] is a temporary variable length array. This would mean that sizeof(int[nnode+1]) == sizeof(int)*(nnode+1), if I understand variable length arrays correctly. Consequently, the whole point about segfault/page fault would be irrelevant.
    It very well might be that sizeof(int[nnode+1]) == sizeof(int)*(nnode+1) but the use of a reserved keyword might confuse the heck out of people like me for instance and if they are equivalent then page fault or sigsegv wouldn't come into play.

  12. #42
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by Elysia View Post
    But in Windows, you would never hear the term sigsev or whatever. You would hear page fault or crash.
    Windows != Linux. People must learn this.
    As it always has been. As it always will be. As it always must be.
    You're right Elysia "Linux is Linux and Windows is Windows and never the twain shall meet"
    Last edited by itCbitC; 01-05-2009 at 09:58 AM.

  13. #43
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,401
    Quote Originally Posted by itCbitC
    It very well might be that sizeof(int[nnode+1]) == sizeof(int)*(nnode+1) but the use of a reserved keyword might confuse the heck out of people like me for instance
    What do you mean by "use of a reserved keyword"? The use of sizeof is correct in this case.

    Quote Originally Posted by itCbitC
    You're right Elysia "Linux is Linux and Windows is Windows and never the twain shall meet"
    Code:
      L
      i
    Windows
      u
      x
    Last edited by laserlight; 01-05-2009 at 09:44 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #44
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by laserlight View Post
    What do you mean by "use of a reserved keyword"? The use of sizeof is correct in this case.
    I mean int

  15. #45
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by laserlight View Post
    Code:
      L
      i
    Windows
      u
      x
    At the very least orthogonality

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamic Array
    By firetheGlazer in forum C Programming
    Replies: 4
    Last Post: 07-11-2008, 11:57 AM
  2. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  3. Replies: 4
    Last Post: 11-02-2006, 11:41 AM
  4. Variable Allocation in a simple operating system
    By awkeller in forum C Programming
    Replies: 1
    Last Post: 12-08-2001, 02:26 PM
  5. making an array name a variable
    By Zaarin in forum C++ Programming
    Replies: 5
    Last Post: 09-02-2001, 06:17 AM