Thread: I'm out of ideas...

  1. #16
    null pointer Structure's Avatar
    Join Date
    May 2019
    Posts
    338
    so then what of this little nugget...
    Code:
    while ( (k = txt = strstr( txt, "\n" )) ) { }
    looks like hell.

    and this...
    Code:
    if ( (ret = change_kvpair( kvpair, full->given, full->given, full->given, 1 )) != EXIT_SUCCESS ){ };
    this works ?

    I don't understand why you are defining values inside a condition like that.
    Last edited by Structure; 01-28-2020 at 04:24 PM.
    "without goto we would be wtf'd"

  2. #17
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    Quote Originally Posted by Structure View Post
    Some things you guys do i can understand. Others i can't and most likely never will.
    What i can tell you is it obviously doesn't work the way it's being done.
    @Structure, think of it in this way... ANY comparison will result in 0 or 1... And, in C, 0 means "false", everything else, "true". So:
    Code:
    if ( ! ( size = file_get_size( &ret, path ) ) ) ...
    In this case, 'size = file_get_size( ... )' is an assignment. If size is 0, !0 will be "true", otherwise "false", like:
    Code:
    size = file_get_size( &ret, path );
    if ( ! size ) ...
    Ot, showing another way:
    Code:
    size = file_get_size ( ... );
    if ( size == 0 ) { ... }
    So, you can assign and "compare" at the same time.

  3. #18
    null pointer Structure's Avatar
    Join Date
    May 2019
    Posts
    338
    you can assign and "compare" at the same time.
    I'm assuming this scopes the variable to the block?
    "without goto we would be wtf'd"

  4. #19
    null pointer Structure's Avatar
    Join Date
    May 2019
    Posts
    338
    Prepare to be shocked
    Code:
    #include <stdio.h>
    int main() {
        int a = 100, *b;
        if ( !(a = *b) ) printf("wtf?");
    };
    I'm out of ideas...-woah-jpg

    lol
    Last edited by Structure; 01-28-2020 at 04:43 PM.
    "without goto we would be wtf'd"

  5. #20
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Structure
    I'm assuming this scopes the variable to the block?
    It would... if you're declaring the variable in the condition. awsdert declared the variables before that point, so no, it doesn't.
    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

  6. #21
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,734
    Found it! Or rather both of the reasons why I was getting no results, the 1st one was at the bottom of proc_notice_info(), I had switched to using additional variables to identify key & value pairs more easily but I forgot to change the checked variable from txt to v in the if statement that followed the loop, down here:
    Code:
    if ( !txt ) {
            ret = ENOENT;
            if ( err ) *err = ret;
            ERRMSG( ret, "Couldn't locate ownerId" );
            return NULL;
        }
        /* Just in case we decide to take more parameters */
        *v = '\t';
        sscanf( txt, "%d",  &(notice->ownerId) );
        if ( err ) *err = EXIT_SUCCESS;
        return notice;
    The 2nd one was in the top code where I forgot to fill entryId with the id of the process being examined so that it could be used at the moment the process is determined to be possibly wanted, the place I should've filled it is here:
    Code:
    notice->ownerId = *(
                ((int*)(glance->idNodes.space.block)) + glance->process);
    Just had to tack in "entryId =" in front that and I started getting results, just takes a while to display my output because I hadn't yet removed the printf's that were there to check how far the code got executed

  7. #22
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,734
    Quote Originally Posted by Structure View Post
    so then what of this little nugget...
    Code:
    while ( (k = txt = strstr( txt, "\n" )) ) { }
    looks like hell.

    and this...
    Code:
    if ( (ret = change_kvpair( kvpair, full->given, full->given, full->given, 1 )) != EXIT_SUCCESS ){ };
    this works ?

    I don't understand why you are defining values inside a condition like that.
    I used it that way because it provides the strongest indication to the compiler that that particular assignment and comparison can be optimized so that size need only be stored to, the comparison can be done on the same register that the result was filled into before being stored to size, doing it the other way means under some situations the compiler is less likely to optimize that pair of actions.

    In other words I'm telling the compiler speed is wanted most there and I've no need to check the variable beforehand during debugging.

  8. #23
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,734
    Got spotty issues with read/write of memory but I'll post about that 2mw if need be, right now I just wanna pose a question related to proc_notice_info(), I can't see how to stop this from happening:
    Code:
    3608 'gasp.elf'
    3609 'private_gasp.el'
    Which should be this:
    Code:
    3608 'gasp.elf'
    3609 'private_gasp.elf'
    I'm currently thinking to just stick an if statement in there with a pointer increment but strstr normally gives me a pointer to the character/string so it shouldn't be needed, am I doing something wrong there?

  9. #24
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by awsdert
    Code:
    notice->ownerId = *(
                ((int*)(glance->idNodes.space.block)) + glance->process);
    Have you considered the syntactic sugar of array notation:
    Code:
    notice->ownerId = ((int*)(glance->idNodes.space.block))[glance->process];
    Quote Originally Posted by awsdert
    I used it that way because it provides the strongest indication to the compiler that that particular assignment and comparison can be optimized so that size need only be stored to, the comparison can be done on the same register that the result was filled into before being stored to size, doing it the other way means under some situations the compiler is less likely to optimize that pair of actions.

    In other words I'm telling the compiler speed is wanted most there and I've no need to check the variable beforehand during debugging.
    I think you'll find that any optimising compiler will be able to optimise to the same result either way. The way I see it, the usual reasons why people assign to variables within conditions is not to hint to the compiler for optimisation, but because they want to update a variable within a loop condition (e.g., your use of readdir), or they want to assign a variable and immediately check that it has a valid value (e.g., your use of opendir) while also prophylactically avoiding the case that a bug might introduced under maintenance such that the newly assigned variable is used before being checked.

    Quote Originally Posted by awsdert
    The 2nd one was in the top code where I forgot to fill entryId with the id of the process being examined so that it could be used at the moment the process is determined to be possibly wanted
    Traditionally, variables are declared at the start of functions in C even if they are used much later because declarations could not be mixed with "code" in the same scope, but if you're compiling with respect to C99 or later, you could consider a different style where variables are declared near first use. It would also be helpful to break down larger functions into smaller helper functions, e.g., your proc_notice_info is well over 110 lines: some of the sections that it consists of (those parts that you marked with comments, which is great) could have been moved into helper functions.
    Last edited by laserlight; 01-28-2020 at 06:02 PM.
    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

  10. #25
    null pointer Structure's Avatar
    Join Date
    May 2019
    Posts
    338
    if size is 0, !0 is "true"
    That makes sense.

  11. #26
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,734
    Quote Originally Posted by laserlight View Post
    Have you considered the syntactic sugar of array notation:
    Code:
    notice->ownerId = ((int*)(glance->idNodes.space.block))[glance->process];
    Lol, I already changed to that in code cause the plus didn't sit well with me, I just didn't bother posting it as it wasn't relevant to the
    now fixed problem, so in answer, yes I have considered it

    Quote Originally Posted by laserlight View Post
    I think you'll find that any optimising compiler will be able to optimise to the same result either way. The way I see it, the usual reasons why people assign to variables within conditions is not to hint to the compiler for optimisation, but because they want to update a variable within a loop condition (e.g., your use of readdir), or they want to assign a variable and immediately check that it has a valid value (e.g., your use of opendir) while also prophylactically avoiding the case that a bug might introduced under maintenance such that the newly assigned variable is used before being checked.
    Like I said before this is just for those rare situations it doesn't, I normally compile without any optimization switches so I can experience the worst run it can give so I can find where code needs to be optimized

    Quote Originally Posted by laserlight View Post
    Traditionally, variables are declared at the start of functions in C even if they are used much later because declarations could not be mixed with "code" in the same scope, but if you're compiling with respect to C99 or later, you could consider a different style where variables are declared near first use. It would also be helpful to break down larger functions into smaller helper functions, e.g., your proc_notice_info is well over 110 lines: some of the sections that it consists of (those parts that you marked with comments, which is great) could have been moved into helper functions.
    Helper functions I only make when more than one function does the same action, this makes it easier for the compiler to optimize under ANSI C without extensions since there is no option of inline functions there, sure if the compiler is smart enough it can do it anyway but this is for compilers that aren't smart enough, for example my mitsy compiler before it reaches stable stage won't be smart enough, up until beta I'll be working on features that don't involve optimization (such as actually compiling C XD), beta is where I'll focus on optimizing everything and implementing those features in the process while also dealing with any bugs peops raise (I'll make a thread when I have marked it beta to invite peops to start raising any issues they find)

  12. #27
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by awsdert
    I normally compile without any optimization switches so I can experience the worst run it can give so I can find where code needs to be optimized
    Where optimisation is concerned, that doesn't make sense: you should compile at the highest optimisation level that you're comfortable with so you can find where code needs to be optimised. If you instead compile without optimisations enabled when optimising, you'll increase your chances of finding code that you think needs to be optimised, but doesn't, hence wasting your time and effort.

    Quote Originally Posted by awsdert
    this makes it easier for the compiler to optimize under ANSI C without extensions since there is no option of inline functions there
    Inline functions were introduced to standard C over two decades ago.
    Last edited by laserlight; 01-29-2020 at 02:12 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

  13. #28
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,734
    Quote Originally Posted by laserlight View Post
    Where optimisation is concerned, that doesn't make sense: you should compile at the highest optimisation level that you're comfortable with so you can find where code needs to be optimised. If you instead compile without optimisations enabled when optimising, you'll increase your chances of finding code that you think needs to be optimised, but doesn't, hence wasting your time and effort.
    It's never fruitless, and I'm not talking small functions the compiler can easily track everything and optimize, I'm talking largish or larger functions like proc_notice_info(), anyways it's how I like to code, doing it this way helps me develop and maintian habits around optimizing my code which results in most of my code being optimized naturally, granted not all of my code is optimized but better to have the habit than to not have it
    Quote Originally Posted by laserlight View Post
    Inline functions were introduced to standard C over two decades ago.
    If I remember rightly it only become an official standard in c99 or higher or c++, c89 did not have that so I write as much code as possible in c89 style to avoid the most issues, things like stdint.h may have been introduced in c99 or something else but it doesn't stop them from being used in c89 (visual c is the best example because for a long time it supported some c99 headers in c89)

  14. #29
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by awsdert
    It's never fruitless, and I'm not talking small functions the compiler can easily track everything and optimize, I'm talking largish or larger functions onilike proc_notice_info()
    The reasoning still applies: if you don't enable optimisations when optimising, whatever measurements you do to determine bottlenecks etc will be inaccurate after you enable optimisations for production. Sure, optimising under such circumstances wouldn't always be fruitless, but it has a higher chance of wasting effort.

    Quote Originally Posted by awsdert
    If I remember rightly it only become an official standard in c99 or higher
    Yes, and that was 21 years ago, though I grant that it is true that support remains mediocre for that one major compiler vendor, but then their focus is on C++, not C.

    Software engineering wisdom is that a function should be written to do one thing and do it well; this means breaking up large functions into smaller ones, and as you noted where function definitions are available, compilers can even inline without use of inline functions: in fact, one argument in recent years is that inline should be seen mainly as a way to satisfy the one definition rule when it would otherwise be broken, rather than a tool for optimisation. Refusing to do such sensible decomposition of functions in the name of optimisation is premature optimisation.
    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

  15. #30
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Quote Originally Posted by awsdert View Post
    Lol, I already changed to that in code cause the plus didn't sit well with me, I just didn't bother posting it as it wasn't relevant to the
    now fixed problem, so in answer, yes I have considered it


    Like I said before this is just for those rare situations it doesn't, I normally compile without any optimization switches so I can experience the worst run it can give so I can find where code needs to be optimized


    Helper functions I only make when more than one function does the same action, this makes it easier for the compiler to optimize under ANSI C without extensions since there is no option of inline functions there, sure if the compiler is smart enough it can do it anyway but this is for compilers that aren't smart enough, for example my mitsy compiler before it reaches stable stage won't be smart enough, up until beta I'll be working on features that don't involve optimization (such as actually compiling C XD), beta is where I'll focus on optimizing everything and implementing those features in the process while also dealing with any bugs peops raise (I'll make a thread when I have marked it beta to invite peops to start raising any issues they find)
    It almost seems as if you think that the compiler optimises at the C level. It's quite reasonable to assume that optimising compilers do a better job when you do break up large functions (using "helper functions") because optimisation doesn't generally happen at the C level but on an intermediate (abstracted) representation of whatever you write (see Intermediate representation - Wikipedia). Besides that, large functions are difficult to maintain and hard to understand for humans. Most optimisations that have any relevance depend on choosing the correct algorithm in the first place (and in some cases using keywords like restrict so that the compiler can optimise more aggressively, for example. restrict - Wikipedia Don't bother with register in general... the compiler will use a register if it makes sense anyhow). Doing stuff like you're suggesting is not just premature optimisation, it might be doing the exact opposite of what you're trying to achieve.

    I guess an example (maybe not a good one) similar to what you think you're doing is a project I was working on last year. A co-worker submitted a cunningly constructed micro-optimised function with a scary comment saying I should not rearrange things because it was carefully optimised to minimise x86 cache misses and that it'd fit in the pipeline or some mumbo jumbo. The problem is that we were prototyping on x86 and the actual target was ARM. So, of course the carefully constructed function was BS, the ARM code generated was not optimal and the compiler already did a better job. If it's not clear by now: I hate micro-optimisations or giving hints to the compiler (apart from well-defined hints like restrict and inline) because in 99.9% of cases I've come across the compiler does a better job. Write clear code, use the correct algorithm, break down large functions and in general the compiler will do a very good job.
    Last edited by Hodor; 01-29-2020 at 05:00 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. in need of some ideas and help....HELP!!!!
    By ccar033 in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 04:58 AM
  2. help with ideas
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 05-02-2002, 11:21 PM
  3. Any Ideas?
    By brad123 in forum C Programming
    Replies: 4
    Last Post: 04-28-2002, 09:00 AM
  4. Ideas
    By GodLike in forum Game Programming
    Replies: 5
    Last Post: 04-16-2002, 01:57 PM
  5. ideas?
    By pode in forum C++ Programming
    Replies: 3
    Last Post: 02-04-2002, 04:13 PM

Tags for this Thread