Let's discuss computer programming

I'm learning C at the moment, and it really messes my brain up.

It'd be probably wonderful for first language, and learned at slow pace, but I have Java burden, and the aim is to learn it in six weeks (first week basics, second week pointers, third week input/output...)

Here's one of the many things I don't understand, from Kerninghan & Ritchie's book:


Ok, I understand perfectly well what this is doing, but suppose that s is shorter than t. Doesn't this write to memory locations that aren't allowed? Once we pass s's '\0' that is.

Here's an illustration,

SSUUUTTT

There's two bytes for s, four for t and in between three for U. If we copy t to s, doesn't this write over first Us?

/* Copies string t to s */
void strcopy(char *s, char* t)
{
while ((*s++ = *t++) != '\0')
;
}

Of course c using pointers allows you to overwrite memory outside the bounds of the array, which is why great care needs to be taken, this means you have to do the bounds checking yourself

if t pointed to an area in memory that immediately preceeded where s pointed to in memory and there was no NUL \o then it is concevable that this would keep going in your while loop until some fatal memory exception.
 
So it's programmers business to take care that he doesn't copy to a shorter string?

I managed to do AES encryption during the course, although, it was broken up to several sub-exercises. Anyhow, it was more than pleasing to finally get something else than nonsense from the decrypt-function. :D

The memory still leaks like a sieve though.
 
If you programme in C (or Assembler) it is your business, in C# you will have less to worry about.

In c you evem have to be careful of this sort of code

char MyString[10];

for (i = 0; i < 11; i++)
{
Mystring = '0';
}

This will write to memory beyond the end of the string, also dont forget that strings should be null terminated.

So if you had a string for the word DOG you will need 4 bytes of data for it in C.

Some compilers are better than others though for warnings and errors.
 
So it's programmers business to take care that he doesn't copy to a shorter string?

Yeah. C is extremely permissive, in good or bad. Like a chainsaw versus a hacksaw, the former is more effective but will hurt more if it goes in your leg. In fact many previously standard library routines are considered dangerous because of easily enabling accidental buffer overflow attacks, due to leaving security up to the developer. I think gcc will issue warnings for many of them nowadays, too.

EDIT: Consider the following code. I've used this approach myself a couple of times.

Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
     
    int main()
    {
            char* eight = malloc(sizeof(char)*8);
            char* four = eight + 4*sizeof(char);
            if(eight == NULL)
                    exit(1);
     
            strcpy(eight, "1234567");
            puts(four);
     
            exit(0);     
    }

Code:
vassal ~ $ ./a.out
567
vassal ~ $

EDIT2: This is an excellent video series for anyone wishing to understand better what's going on with the memory.


Link to video.
 
Top Bottom