Thread: I'm out of ideas...

  1. #31
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,733
    Quote Originally Posted by Hodor View Post
    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.
    That co-worker sounds like he had the wrong idea, anyways the only in code optimizations I make are simple ones that can be understood at a glance by any coder with decent knowledge of C, would not go so far as to optimize to the specific CPU, merely enough optimization that the compiler is guaranteed to understand that no extra load operations are needed, I avoid doing a multi-level expression beyond that unless I'm trying to avoid one-time variables. Anyways enough of that topic for now, any ideas as to why this loop continues inifinitly?
    Code:
    for ( i = glance->process; i < glance->idNodes.count; ++i )
    	{
    		entryId = notice->ownerId =
    			((int*)(glance->idNodes.space.block))[i];
    		fputs( "Enter\n", stderr );
    		while ( proc_notice_info( err, notice->ownerId, notice ) )
    		{
    			if ( notice->ownerId == glance->underId ) {
    				glance->process = ++i;
    				return proc_notice_info( err, entryId, notice );
    			}
    			if ( notice->ownerId <= 0 )
    				break;
    		}
    		fputs( "Leave\n", stderr );
    	}
    I get a list of "Enter" outputs but never a "Leave" output, I've done a session check for calls to the function containing this and there are only 2, one for when a glance object is open and one for the proc_locate_name() function which calls it like this:
    Code:
    for ( notice = proc_glance_open( err, &glance, underId )
    		; notice; notice = proc_notice_next( err, &glance )
    	)
    proc_glance_open() is never called elsewhere at the moment and proc_locate_name() is only called once by main()
    I can't find any reason for the loop to restart itself without ever reaching the "Leave" statement

  2. #32
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Quote Originally Posted by awsdert View Post
    [...] any ideas as to why this loop continues inifinitly?
    Code:
    for ( i = glance->process; i < glance->idNodes.count; ++i )
        {
            entryId = notice->ownerId =
                ((int*)(glance->idNodes.space.block))[i];
            fputs( "Enter\n", stderr );
            while ( proc_notice_info( err, notice->ownerId, notice ) )
            {
                if ( notice->ownerId == glance->underId ) {
                    glance->process = ++i;
                    return proc_notice_info( err, entryId, notice );
                }
                if ( notice->ownerId <= 0 )
                    break;
            }
            fputs( "Leave\n", stderr );
        }
    I get a list of "Enter" outputs but never a "Leave" output, I've done a session check for calls to the function containing this and there are only 2, one for when a glance object is open and one for the proc_locate_name() function which calls it like this:
    Code:
    for ( notice = proc_glance_open( err, &glance, underId )
            ; notice; notice = proc_notice_next( err, &glance )
        )
    proc_glance_open() is never called elsewhere at the moment and proc_locate_name() is only called once by main()
    I can't find any reason for the loop to restart itself without ever reaching the "Leave" statement
    How would I know? There's no context, no comments, it's unstructured and I don't even know what the loop is supposed to do. Maybe split it off into a function

  3. #33
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,733
    Quote Originally Posted by Hodor View Post
    How would I know? There's no context, no comments, it's unstructured and I don't even know what the loop is supposed to do. Maybe split it off into a function
    Ah you responded before I had a chance to post that I realised its not going infinitely, was something else causing the process to hang, was investigating other possibilities I thought of but I will have to finish later after work, btw what do you mean by unstructured? looks pretty structured to me.

  4. #34
    null pointer Structure's Avatar
    Join Date
    May 2019
    Posts
    338
    Code:
    return proc_notice_info( err, entryId, notice );
    The return statement will exit the current function, not just the loop it is in.
    Code:
     proc_notice_info( err, notice->ownerId, notice )
    What is the return value of this function ?
    This would be the first thing i would check.
    Last edited by Structure; 01-29-2020 at 12:14 PM.
    "without goto we would be wtf'd"

  5. #35
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,733
    Quote Originally Posted by Structure View Post
    Code:
    return proc_notice_info( err, entryId, notice );


    Code:
     proc_notice_info( err, notice->ownerId, notice )
    What is the return value of this function ?
    This would be the first thing i would check.
    For that in all error scenarios it returns NULL, only when it did not experience an error does it return the pointer it was given, anyways I finally tracked down the source of the infinite loop after making liberal use of stderr, I made some modifications realising it could be attempting to read memory beyond the buffer but nope, it just refuses to die, this is the loop:
    Code:
    	n = k = full->block;
    	while ( size ) {
    		n = strchr( n, '\n' );
    		if ( n && *n == 0 ) n = NULL;
    		if ( n ) {
    			*n = 0;
    			size -= (intptr_t)(n - k);
    		}
    		else size = 0;
    		fprintf( stderr, "Size = %d Bytes\n", size );
    		v = strchr( k, '\t' );
    		*v = 0;
    		/* We only care about the name and ppid at the moment */
    		if ( strcmp( k, "Name:" ) == 0 ) {
    			strcpy( name->block, v );
    			got_name = 1;
    		}
    		else if ( strcmp( k, "PPid:" ) == 0 ) {
    			ERRMSG( EXIT_SUCCESS, "Point 15");
    			sscanf( v, "%d", &(notice->ownerId) );
    			if ( err ) *err = EXIT_SUCCESS;
    			got_ppid = 1;
    		}
    		*v = '\t';
    		if ( n ) *n = '\n';
    		k = n;
    		++k;
    	}
    Somehow size actually increases, anyone have any ideas on how I can correct that to decrease instead?

    Edit: Never mind I got it to die by changing the top to this:
    Code:
    while ( size > 0 ) {
    	n = strchr( n, '\n' );
    	if ( n && *n == 0 ) n = NULL;
    	if ( n ) *n = 0;
    	size -= strlen(k) + 1;
    Last edited by awsdert; 01-30-2020 at 05:30 AM.

  6. #36
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    -- deleted response because it no longer makes sense after the edit --

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