IQOD #7

Posts 1–11 of 11 · Page 1 of 1
IQOD #7
These are all really starting to look a like to me, except for a few which reference some obscure C functions noone even knows about anymore lol (va_start anyone? or setlongjmp?)

Anyway I'm thinking after these we'll move into some 'larger' questions and possibly open ended (e.g. "write some code without use of the compiler/ide etc")

Consider the following program snippet (so don't worry about missing includes etc) on a 16-bit platform:
Code:
void f(char **p)
{
     char *t;
     t = (p+= sizeof(int))[-1];
     printf("%s",t);
}

void main()
{
    char * argv[] = { "ab", "cd", "ef", "gh", "ij", "kl" };
    f(argv);
}
What is the output?
What would the output be on a 32-bit platform?
Quote Originally Posted by B1ackAnge1 View Post
These are all really starting to look a like to me, except for a few which reference some obscure C functions noone even knows about anymore lol (va_start anyone? or setlongjmp?)

Anyway I'm thinking after these we'll move into some 'larger' questions and possibly open ended (e.g. "write some code without use of the compiler/ide etc")

Consider the following program snippet (so don't worry about missing includes etc) on a 16-bit platform:
Code:
void f(char **p)
{
     char *t;
     t = (p+= sizeof(int))[-1];
     printf("%s",t);
}

void main()
{
    char * argv[] = { "ab", "cd", "ef", "gh", "ij", "kl" };
    f(argv);
}
What is the output?
What would the output be on a 32-bit platform?
va_start: yes
setlongjump: no :P

I must say that this one is pretty hard.
a wild guess:
16 bit: cd
16/sizeof(int)=4
argv[4-1] = c but since its pairs of 2 its cd

32 bit: gh
32/sizeof(int) = 8
argv[8-1] = g but since its pairs of 2 its gh

either that or an int takes up only 2 bytes on 16 bit systems which would mean that following my above logic they would both result in gh


va_start
Code:
void __cdecl add_log(const char * fmt, ...)
{
	va_list va_alist;
	char logbuf[256];
	FILE * fp;
	struct tm * current_tm;
	time_t current_time;

	time (&current_time);
	current_tm = localtime (&current_time);

	sprintf (logbuf, "[%02d:%02d:%02d] ", current_tm->tm_hour, current_tm->tm_min, current_tm->tm_sec);

	va_start (va_alist, fmt);
	_vsnprintf (logbuf+strlen(logbuf), sizeof(logbuf) - strlen(logbuf), fmt, va_alist);
	va_end (va_alist);

	if ( (fp = fopen ( "C:\\debuglog.txt" , "a")) != NULL )
	{
		fprintf ( fp, "%s\n", logbuf );
		fclose (fp);
	}
}
I think I feel Too intimidated to try this one >_<
Quote Originally Posted by B1ackAnge1 View Post
t = (p+= sizeof(int))[-1];
I've actually never seen a line like this before in my life. I have no idea what it does besides that it assigns something to t.
Code:
t = (p+= sizeof(int))[-1];
Ok let's break it down:

t=         //Assign Something to 't', but what? the following:
    (p+=                          //Add Something to Pointer P
            sizeof(int)           // Something is 'SizeOf(int)
    ) 
    [-1];                         //Then access index [-1] of that modified Pointer
                                   
So t = value at index -1 of (pointer p + size of int)
Quote Originally Posted by B1ackAnge1 View Post
Code:
t = (p+= sizeof(int))[-1];
Ok let's break it down:

t=         //Assign Something to 't', but what? the following:
    (p+=                          //Add Something to Pointer P
            sizeof(int)           // Something is 'SizeOf(int)
    ) 
    [-1];                         //Then access index [-1] of that modified Pointer
                                   
So t = value at index -1 of (pointer p + size of int)
Oh wow. I guess it makes sense now. one more question. is p a pointer to a pointer?
yup since argv is a char* then to get a pointer to argv you get char**
Ok. thanks BA. Now let me see if I can figure this thing out
On a 16 bit platform an Int = 2 bytes , while on a 32 bit system an Int = 4 bytes
so have an offset of either 2, or 4 to move the Pointer 'p' by.
a 'plain' int usually is the standard of the platform.. 16 bit, 32 bit etc. i.e. a 'long' on 16 bit was a 32 bit int, while a long on a 32 bit platform is 64 bit

We start off by pointing at "ab", so move it twice and you end up pointing at "ef". Then you need index -1, which brins you back to "cd".

On 32 bit you'd move it 4 total: ->Ab ; ->cd (+1) ; -> ef (+2) ; ->gh(+3) ; ->ij(+4). So we end up at "ij". Then again index -1 which is basically just taking one step back so on 32 bit you'd end up with "gh"
lol, my answers were correct, but for the wrong reason XD oh well i learned from it ^^
The thing some people stumble with is that while sizeof() returns how many bytes something is, it is just a number that initself has no 'size' linked to it.
They then think "oh I need to move ahead 2 bytes, or 4 bytes", but that's not the case since it all depends on the size of what the pointer is pointing to.

for instance: If you were to look at one of our previous IQOD's we incremented a pointer +1 and it jumped ahead 10 bytes (since it was a pointer to an char[10] array)
Posts 1–11 of 11 · Page 1 of 1
This thread is closed for replies.

Similar Threads

Tags for this Thread

Talk with us