Skip to main content
gamedev.net
🔒 Locked

simple question

Started by Stash1071 10 replies 350+ views
Original Post
Stash1071
Stash1071
Im taking a programming class where I had to implement my own version of C''s strcpy. I did it like this char *cpy(char *a, char *b) { while(*b != ''\0'') { *a = *b; a++; b++; } *a = ''\0''; return a; } but I did try it like this and couldnt figure out why this caused a fatal error at the end of execution char *cpy(char *a, char *b) { while(*b != ''\0'') { *a = *b; a++; b++; } while(*a != ''\0'') { *a = ''\0''; a++; } return a; } It doesnt really matter now that I got it to work, but Im just curious as to why the latter would cause an error. Anyone have any ideas? =) Thanks for your time, John
PouyaCat
PouyaCat
Hmmmz...

My first impression: you are checking an uninitialized entry. There is nothing in the memoryspace of *a, thus, how you can compare it to ''\0'' then?
Mezz
Mezz
You make the assumption that the end of an array terminates with a ''\0'' character. This is rarely (and certainly not defined to be) the case. It is only the immediate end of a string present in an array that terminates with a ''\0''.

The problem with that assumption is that you will likely overrun past the end of the array, and probably get an access violation.

-Mezz
daerid
daerid
he said he''s doing a re-implementation of strcpy, which then it would be safe to assume that the string terms with \0
daerid@gmail.com
a38750
a38750
in your second function, in the second while loop, a is initially pointing to the first character after the copied string, right? And you were right the first time, all you need to do is set *a to null. I assume in your second example that you are trying to neatly set the remaining space to nulls. This isn't going to work though because the rest of the buffer is just junk, and probably won't contain a null (there isn't automatically one at the end of a buffer). So what's happening is your loop is just writing nulls indefinitly (and usually beond the buffer, hence the fault).

PS: shouldn't you be returning a pointer to the start of string a, not the end as you are at the moment?

Edit: hmm... I probably should have checked to see if anyone had replied already before I posted

[edited by - frankd on August 4, 2002 7:12:43 PM]
Dobbs
Dobbs
Both versions have several errors and will likely crash/cause errors.

First of all, in both versions you''re modifying the pointers that are passed to the function. a and b should still point to the first character of their respective strings when the function is finished, think about what your code is actually doing with them.

Your second implementation is crashing because you''re incrementing a after writing the terminating \0.
a38750
a38750
quote:
Original post by Dobbs
...
First of all, in both versions you''re modifying the pointers that are passed to the function. a and b should still point to the first character of their respective strings when the function is finished, think about what your code is actually doing with them.

Changing the position of the pointers a and b doesn''t affect anything outside the function. The function has a copy of the addresses passed to it which are stored in the local variables a and b.


Xanth
Xanth
You're missing a semi-colon ; after your while loop's ending curly brace }

change your code to the following and is should work.

char *cpy(char *a, char *b) {
while(*b != '\0') {
*a = *b;
a++;
b++;
}; // <<< Added ;

*a = '\0';
return a;
}


"I thought Genius lived in bottles..." - Patrick Star


[edited by - Xanth on August 4, 2002 8:31:55 PM]
"I thought Genius lived in bottles..." - Patrick Star
c_wraith
c_wraith
Come on... This is C. The code should be as evil as possible. =)


  
char *cpy(char *a, char *b)
{
char *result = a;
while (*a++ = *b++);
return result;
}


This version does return a pointer to the beginning of the string copied into, which is what it should do, I believe.
Xanth
Xanth
Or...


  
void cpy(char* destination, char* source) {
while((*(destination++) = *(source++)) != '\0');
}


=D Doesn't return anything, but I don't see much use in returning anything anyway. ???

[edited by - Xanth on August 4, 2002 12:28:01 AM]
"I thought Genius lived in bottles..." - Patrick Star
Mezz
Mezz
daerid: Safe to assume the string he is copying FROM is terminated with a ''\0''. Not the array he is copying TO (which is why he is probably overrunning the end).

Take for instance, the case where the character array ''b'' contains nothing in it''s string, i.e. it was initialised perhaps like so:

char my_array[6] = "";

Then the first loop in the second function would never execute. So what are you comparing the array ''a'' to now? Well, nothing really because you have no idea what it contains. The only way you can neatly assign ''\0'' to the rest of the array (as somebody else noticed, this is probably your intention) is to know the size of the array to begin with, because there is nothing to say where the end of an array is.

-Mezz

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.