[Sussex] C programming help

Steve Dobson steve at dobson.org
Tue Apr 26 07:17:41 UTC 2005


Hi Cap'in

On Sun, Apr 24, 2005 at 01:10:21PM +0100, Captain Redbeard wrote:
> I've just spent the last 48 hours (I think I remember getting some sleep 
> somewhere) trying to get a C program that I'm coding to work.  I'm not a 
> trained programmer, just amateur stuff, library books, things like that 
> but the program uses some fairly advanced concepts like function 
> pointers, arrays of structures, etc.  The program (a miniature ecosystem 
> simulator) has pretty much all of the functionality coded into it that I 
> want however I keep running into run-time crashes like "segmentation 
> faults" and more bafflingly "*** glibc detected *** double free or 
> corrupted (!prev):" and variations on this.  I suspect that the problem 
> comes from mishandling pointers and malloc()/free() statements, 
> traceable probably to a deeper misunderstanding of how memory allocation 
> works in C.  Does anybody know of any good forums where learner/newbie 
> programmers can get help with this sort of thing?  None of the 
> tutorials/books I found delve deeply enough into the kind of concepts 
> mentioned enough to be of use and Googling for the error messages only 
> gives errors in specific applications without explaining exactly what 
> "*** glibc detected ***" actually means.  On the other hand if there are 
> any C programmers on this mailing list who would be willing to help me 
> out then let me know and I can post some short example programs here.  
> I've been working on the program every evening for about two weeks now 
> and have a LOT of backlogged questions.
> 
> Here is an example of what I am running into.  As far as I know the 
> following program should be syntactically correct.  It compiles OK but 
> when I run it I get a "Segmentation fault" error.  Why?

The numbers of ways one can screw up the use of dynamic memory use are
legion.  I don't know of any books, I learnt the hard way, just as you
are now doing.  I know, that doesn't help much does it.
 

Note: My comments below assume that this is using a 32-bit system
> ========================================
> #include <stdio.h>
> #include <stdlib.h>
> 
> // Set up a coordinate structure
> struct Coords
> {
>  int X;
>  int Y;
> };
> 
> // Set up an array of structures (???)
> struct Coords **CoordArray;

This declares a pointer to an array of "Coords".  The array
to hold the "Coords" has not been created yet.  All you have
is a four bytes that point to that array.  From your comment
I think you think that you have the array as well.  THIS IS
NOT THE CASE.  The system doesn't know how many elements 
the array needs to hold.  Compare this with a static array
delcaration where the number of elements *must* be defined:

    struct Coords CoordArray[100];

It is a very good idea (practice) to all ways initialise any
variables.  Also this is not the array but a pointer to it
so it would be better if you declared it thus:

  struct Coords **CoordArrayPtr = NULL;

 
> int main()
> {
>  // Initialises the first element of the array
>  CoordArray[0] = malloc (sizeof (struct Coords));

This allocates the first element to the array.  But as I hope
you now see you haven't allocated the array yet.  You need
to insert before this line:

   CoordArray = (Coords *) calloc(MAX_COORDS, sizeof(struct Coords));
 
>  return 0;
> }
> ========================================
> 
> Note: if it makes any difference, I'm using GCC version 3.3.4 under 
> Slackware 10.2 using the command "gcc -Wall -o malloctest malloc.c".
> 
> Any ideas anyone?

Yes, see above.

Steve

-- 
If God is One, what is bad?
		-- Charles Manson
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
Url : http://mailman.lug.org.uk/pipermail/sussex/attachments/20050426/6136b630/attachment.pgp 


More information about the Sussex mailing list