Welcome, Guest. Please login or register.

Author Topic: Beginners 'C' programing group  (Read 13076 times)

Description:

0 Members and 1 Guest are viewing this topic.

Offline Karlos

  • Sockologist
  • Global Moderator
  • Hero Member
  • *****
  • Join Date: Nov 2002
  • Posts: 16879
  • Country: gb
  • Thanked: 5 times
    • Show all replies
Re: Beginners 'C' programing group
« Reply #14 from previous page: February 13, 2004, 12:35:12 PM »
@Cymric

I tend to agree. C is the starting point for someone new.

You need to know C syntax, functions and structures before you have the knowledge base to truly take in what a class is. That's before you get into stuff like overloading, generic programming etc.

One of the things which Bjarne Stroutsrup himself notes in his C++ Language book is that "a good C program is also a good C++ program."
int p; // A
 

Offline Karlos

  • Sockologist
  • Global Moderator
  • Hero Member
  • *****
  • Join Date: Nov 2002
  • Posts: 16879
  • Country: gb
  • Thanked: 5 times
    • Show all replies
Re: Beginners 'C' programing group
« Reply #15 on: February 13, 2004, 12:47:36 PM »
@mikeymike

You'd be surprised for a "high level" language how low level C really is. Its much lower level than BASIC type languages and as a compiled language it is faster mostly due to the fact that it is quite close to the machine level already.

The beauty of C is that it gives you high level abstractions for things which are a pain (and non portable) to do in asm, without really being much higher.
int p; // A
 

Offline Karlos

  • Sockologist
  • Global Moderator
  • Hero Member
  • *****
  • Join Date: Nov 2002
  • Posts: 16879
  • Country: gb
  • Thanked: 5 times
    • Show all replies
Re: Beginners 'C' programing group
« Reply #16 on: February 13, 2004, 02:27:50 PM »
@mikeymike

/me needs stronger specs :-)
int p; // A
 

Offline Karlos

  • Sockologist
  • Global Moderator
  • Hero Member
  • *****
  • Join Date: Nov 2002
  • Posts: 16879
  • Country: gb
  • Thanked: 5 times
    • Show all replies
Re: Beginners 'C' programing group
« Reply #17 on: February 14, 2004, 03:17:42 PM »
@Cymric

Fluffy isn't wrong, he is saying when you understand pointers properly, you know *why* the code he presented is so screwed up.

-edit-
It appears the 'bad' code is to make a function that will create a new unique copy of the source string.
-/edit-

For those who can't see it

First of all, he creates a single character 'result', gets a pointer (the address of the bit of memory where 'result' is) to it by using '&result'.

Next, he attempts to copy a string of characters to that piece of memory using a standard C function 'strcpy()' for copying strings. Strings in C are basically arrays of characters (that end in a zero byte). When you copy them, you copy the entire array from one place to another.

The result is that the memory beyond the space occupied by 'result' is overwritten. Variables like the one he declares here are created on the programs' stack, where all the important temporary information at any given instant lives. The effects of overwriting this memory are hence generally catastrophic to say the least...

The second error is that he returns the address of 'result' from the function. The problem here is that the actual character 'result', created on the stack, only exists during the lifetime of this function. Once you leave the function (by returning from it), that character effectively ceases to exist and its memory space is used by something else.
By returning its address, we can then later go back and use this variable which as we have just seen, no longer really exists. Any attempts to use the variable involve, once again, illegally messing up the stack resulting in another bomb out.

-edit-

Anyway, to fix this bad code, we need to create a genuine, seperate copy of the string and return its address. This means we need to dynamically allocate (that is, create an area in the computers 'free' memory) sufficient space to store a copy of the string, copy the characters accross and then return the address of this new bit of memory we have floating around.

char* clone(char* string)
{
  char* result;
  result = (char*)malloc(strlen(string)+1); /* Don't be a muppet like me and forget the +1 :-) */
  strcpy(result, string);
  return result;
}

This can be improved upon by checking that the allocation using malloc worked (check the value of 'result' to make sure it is not NULL) before we use it, but I ommited this check for the sake of clarity.

ie instert after the line with malloc() and before strcpy() the following check

if (result == NULL)
  return NULL;
int p; // A
 

Offline Karlos

  • Sockologist
  • Global Moderator
  • Hero Member
  • *****
  • Join Date: Nov 2002
  • Posts: 16879
  • Country: gb
  • Thanked: 5 times
    • Show all replies
Re: Beginners 'C' programing group
« Reply #18 on: February 14, 2004, 11:50:09 PM »
Quote

FluffyMcDeath wrote:


What's really amazing is how often code like this WON'T bring the Amiga or many other systems down right away. If this code is executed at the end of a long sequence of calls, and it doesn't write past the end of the stack (or even if it does for that matter) and the stack never goes that deep again, there is no reason why the data can't live for ever and be manipulated at will. It's just memory.


Actually, it brings amigaos down with a thump almost immediately. Remember, the stack grows downwards on amiga/680x0 so as the copy writes to succesively higher addresses, one of the first things it will trash is the functions return address which was pushed by jsr onto the stack right after all of the arguments.

Hence it doesn't matter how deep into the code you are, once you overwrite your functions' return address you are shafted...
int p; // A
 

Offline Karlos

  • Sockologist
  • Global Moderator
  • Hero Member
  • *****
  • Join Date: Nov 2002
  • Posts: 16879
  • Country: gb
  • Thanked: 5 times
    • Show all replies
Re: Beginners 'C' programing group
« Reply #19 on: February 15, 2004, 12:59:27 AM »
This is beginning to sound like an 80's infomerical on the hazards of pointer abuse :lol:

Think before you dereference!
int p; // A
 

Offline Karlos

  • Sockologist
  • Global Moderator
  • Hero Member
  • *****
  • Join Date: Nov 2002
  • Posts: 16879
  • Country: gb
  • Thanked: 5 times
    • Show all replies
Re: Beginners 'C' programing group
« Reply #20 on: February 15, 2004, 03:04:54 AM »
Quote

FluffyMcDeath wrote:

BTW I just compiled this with a version of gcc at work that we use for our embedded systems. It didn't even warn!


Realistically I don't think it can warn against the first misuse (since it is technically not invalid in the language) unless the error checking does some kind of flow analysis to see that &result is a actually reference to a single character.

However, returning the address of an automatic variable should definately give a warning. Even StormC manages that...
int p; // A
 

Offline Karlos

  • Sockologist
  • Global Moderator
  • Hero Member
  • *****
  • Join Date: Nov 2002
  • Posts: 16879
  • Country: gb
  • Thanked: 5 times
    • Show all replies
Re: Beginners 'C' programing group
« Reply #21 on: February 15, 2004, 12:16:55 PM »
@Piru

Whoops, I knew I'd forgotten something :-D I thought about the +1 afterwards and thought "I don't remember - do I need it or not?"...then went to bed :-)

-edit-

It also suggests that this isn't even a beginner mistake ;-) Perhaps I should go join Matt and play with lego in the corner :lol:
int p; // A
 

Offline Karlos

  • Sockologist
  • Global Moderator
  • Hero Member
  • *****
  • Join Date: Nov 2002
  • Posts: 16879
  • Country: gb
  • Thanked: 5 times
    • Show all replies
Re: Beginners 'C' programing group
« Reply #22 on: February 15, 2004, 12:37:01 PM »
Quote

boing wrote:
  I don't think I'll ever like C.  It looks like something created for a parser that was written in one night instead intended as  a quality language.  One can have a great language without the visual ugliness of C. Or it's counterintuitive use of  grammar and math characters.  It's just the most goddamn impossible language tp read through that I've ever seen.


Written in one night? :lol: C has the just about cleanest syntax going. Java, javascript, C++, CF, etc. didn't copy it for fun, you know.

Still, it does look a bit odd at first, but with experience you will actually realise its stuff like BASIC that is unweildy.

Quote

As far as I'm concerned, either use Assembler, or use AMOS or Blitz.  Anything inbetween is kind of pointless.


1) Don't use assembler unless you either (a) don't care at all about portability, or (b) optimising some code for a particular system.

2) AMOS is one of the worst languages ever. Period.

Quote

Having said that, I guess I'd like to revisit C since so much $&^@ documentation assumes you're comfortable with it.  Bastards.  This is what happens when wannabe's outnumber true coders in colleges.


A very mature attitude! A bad craftsman always blames his tools. If you are a 'true coder' as you say and have the mentality for programming (that is analysing problems and devising solutions) you can adapt to just about any language.

Since I studied chemisrty, I learned 680x0/PPC asm, C, C++ and Java entirely in my own time. I also started with a knowledge of BASIC and can assure anybody that moving to C from such a background is far from impossible.

Quote
Still, why waste time with C when C++ exists?


If you can't take C syntax, you don't stand a chance of learning C++
int p; // A