Strings

Go To Last Post
10 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello Freaks, I'm comparing two strings with the function, "strcmp". See below for the code example, however it never returns a match, even when I load the strings for testing. Any advise is much appreciated. Also note, char * program is sent from the called has,

tcbptr = get_tid("task2");

And that "pidCB->prg_id" points to another string.

void * get_tid(char * program)
	{
	struct rcb extern *SchedularCB_ptr;
	struct pid *pidCB;
	
	pidCB = SchedularCB_ptr->start;
	for(int8 i=0; SchedularCB_ptr->nr_pid != i ;i++)
		{
		if(strcmp(program, pidCB->prg_id) == 0)
			return(pidCB->tcb_ptr);
		else
			pidCB = pidCB->nx;
		}
	return(NULL);
	}
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

How certain are you that pidCB->prg_id really is pointing at some chars. If you have a UART/printf() facility for debugging this I'd start with:

printf("pgm=, prg_od=\n", program, pidCB->prg_id)

Otherwise, if you have JTAG then dereference the two pointers and see what they are really pointing at.

By the way, as you are walking a linked list I'd be at least NULL checking if not bounds checking the pointers each time you move onto ->nx, how certain are you that ->nx wasn't NULL or invalid? On an AVR you can get away with reading/writing NULL pointers (you actually probably end up corrupting R0 as it is found as "RAM" address 0x0000) but on memory protected architectures you'll start throwing exceptions if you read/write through NULL pointers.

Cliff

PS Because of the forum bug '/' represents a percent symbol

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

good ........

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello Cliff, yes pid is pointing to a string, char *. The code for this is as follows.

void prog_id_enroll(char const *program, struct tcb *tcb_ptr)
	{
	struct rcb extern *SchedularCB_ptr;
	struct pid *prg_ptr;

	char *prg_name;
	
	prg_ptr  = malloc(sizeof(struct pid));
	prg_name = malloc(strlen(program) + 1);
	strcpy(prg_name, program);
	prg_ptr->prg_id = prg_name;

Also, I checked the the memory locations pointed to by the char * ptr's, all seems okay. I'm away for a cupa tea and to think about it. And no I do not need to check for a null, It's a double link list and will always point to a control block else or the nr_pid will cause the function to exit without a match and return a NULL.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

okay, when I load the values into the function it still fails. For example, when I set the string comparison to the following I still do not gewt a match.

void * get_tid(char * program)
	{
		if(strcmp(program, "test1") == 0)
			return(pidCB->tcb_ptr);

And I'm calling the function as follows.

tcbptr = get_tid("task1");
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Well, and did you expect a match between "test1" and "task1"?

JW

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Jan, in Ireland there are many wonders.

They have very good tea. They even drink more tea than the English.

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

They even drink more tea than the English.

Unless you marry one of them (don't ask me how I know ;-))

(but in an Irish run household it's technically impossible to start doing anything without first having tea - you'd have thought I married Mrs. Doyle from Father Ted! "Ah go on, you will, you will, you will....")

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Listen, that was a simple mistake, however it still does not make a match...

I think I'll have another cup of tea, can't find any drinkers the night. Buckfast rules! ;)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

got it working. And quess what, I'm not telling how. LMAO