unions and structs

This is a discussion on unions and structs within the C Programming forums, part of the General Programming Boards category; static functions are only visible in the file they are declared/defined in. The two morecore declaration and definitions are not ...

  1. #16
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    static functions are only visible in the file they are declared/defined in. The two morecore declaration and definitions are not the same function.

  2. #17
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Well, I'm assuming he's not compiling his .h file separately (although perhaps I shouldn't). I had tried it (as mentioned above) with the prototype in a #included header, and that works just fine (as it should).

  3. #18
    Ron
    Ron is offline
    Registered User
    Join Date
    May 2006
    Posts
    151
    Yes I am running the static function in the same file.
    And , am I not right in this. As I have included the . h in the beginnning of my program; wont the compiler run the .h file before running the program file.
    Hence I have not compiled my .h file seperately. Even if I did, it should not hurt. As the compiler will anyway do the same.
    Last edited by Ron; 07-22-2008 at 11:28 PM.

  4. #19
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Does your prototype
    Code:
    static Header *morecore(unsigned long nunits);
    occur before the definition of Header? (I can't see that resulting in your error, but still . . . .)

    Could you post your complete code, .c and .h files?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #20
    Ron
    Ron is offline
    Registered User
    Join Date
    May 2006
    Posts
    151
    Header file:

    Code:
    #ifndef MYMALLOC_H
    #define MYMALLOC_H
    
    
    void *mymalloc(unsigned  long nbytes);
    static Header *morecore(unsigned long nunits);
    void myfree(void *p);
    
    #endif
    C File:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <assert.h>
    #include "mymalloc.h"
    
    
    #define MEM_SIZE 1024 * 1024
    #define NALLOC 1024
    
    typedef long Align; /* for alignment to long boundary */
    union header{ /* block header */
    	struct{
    		unsigned long size; /* size of this block */
    		int prev_check;
    		union header *ptr; /* next block if on free list */
    
    	} s;
    	Align x; /* force alignment of blocks */
    };
    
    typedef union header Header;
    
    
    static Header base; /* empty list to get started */
    
    
    static Header *freeptr = NULL; /* start of free list */
    
    /* malloc: general-purpose storage allocator */
    void *mymalloc(unsigned long  nbytes)
    {
    	Header *p, *prevptr;
    	unsigned long  nunits;
    	nunits = (nbytes+sizeof(Header)-1)/sizeof(Header) + 1;
    
    	if ((prevptr = freeptr) == NULL)
    	{ /* no free list yet */
    		base.s.ptr = (Header *)calloc(MEM_SIZE,sizeof(Header));
    		base.s.size = 0;
    		/* allocase a memory from OS and then manage it */
    		base.s.ptr->s.size = MEM_SIZE;
    		base.s.ptr->s.ptr = &base;
    		freeptr = prevptr = &base;
    	}
    
    	for (p = prevptr->s.ptr; ; prevptr = p, p = p->s.ptr)
    	{
    		if (p->s.size >= nunits)
    		{ /* big enough */
    			if (p->s.size == nunits) /* exactly */
    				prevptr->s.ptr = p->s.ptr;
    			else
    			{ /* allocate tail end */
    				p->s.size -= nunits;
    				p += p->s.size;
    				p->s.size = nunits;
    			}
    			freeptr = prevptr;
    			base.s.prev_check =0;
    			return (void *)(p+1);
    		}
    		if (p == freeptr) /* wrapped around free list */
    		if ((p = morecore(nunits)) == NULL)
    				return NULL; /* none left */
    	}
    }
    
    static Header *morecore(unsigned long nunits)
    {
    	char *cp, *sbrk(int);
    	Header *up;
    
    	if (nunits < NALLOC)
    		nunits = NALLOC;
    
    	cp = sbrk(nunits * sizeof(Header));
    
    	if (cp == (char *) -1)
    		return NULL;
    
    	up = (Header *) cp;
    	up->s.size = nunits;
    
    	free ((void *)(up+1));
    	return freeptr;
    }
    
    /* free: put block ap in free list */
    void myfree(void *ap)
    {
    	Header *bp, *p;
    	bp = (Header *)ap - 1; /* point to block header */
    	printf("bp->s.size &#37;d\n",bp->s.size);
    	for (p = freeptr; !(bp > p && bp < p->s.ptr); p = p->s.ptr)
    		if (p >= p->s.ptr && (bp > p || bp < p->s.ptr))
    		{
    			printf("in the corner\n");
    			break; /* freed block at start or end of arena */
    		}
    
        bp->s.prev_check =1;
        if (bp + bp->s.size == p->s.ptr)
    	{
           /* join to upper nbr */
    		bp->s.size += p->s.ptr->s.size;
    		bp->s.ptr = p->s.ptr->s.ptr;
    	}
    	else
    		bp->s.ptr = p->s.ptr;
    	if (p + p->s.size == bp)
    	{ /* join to lower nbr */
    		p->s.size += bp->s.size;
    		p->s.ptr = bp->s.ptr;
    	}
    	 else
    		p->s.ptr = bp;
    	freeptr = p;
    }

  6. #21
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Yes, you see, my guess was right. You can't use Header in the header file because it hasn't been defined yet.

    Move this
    Code:
    typedef long Align; /* for alignment to long boundary */
    union header{ /* block header */
    	struct{
    		unsigned long size; /* size of this block */
    		int prev_check;
    		union header *ptr; /* next block if on free list */
    
    	} s;
    	Align x; /* force alignment of blocks */
    };
    
    typedef union header Header;
    into the header file, probably before your function prototypes.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #22
    Ron
    Ron is offline
    Registered User
    Join Date
    May 2006
    Posts
    151
    Ok I wasnt aware of that. That has fixed the problem.
    Now something I cant figure out.
    Code:
    Undefined                       first referenced
     symbol                             in file
    main                                /usr/lib/crt1.o
    ld: fatal: Symbol referencing errors. No output written to a.out
    collect2: ld returned 1 exit status
    But how am I getting undefined symbol main when the main function is not in this file?

  8. #23
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    It looks like you invoked the linker and tried to create an executable.

    When you're dealing with multiple source files, compile them with -c, and then link the object files together together without -c.
    Code:
    $ gcc -c source1.c
    $ gcc -c source2.c
    $ gcc source1.o source2.o -o executable
    $ ./executable
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  9. #24
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You can't get an executable out of a file that doesn't have a main. If this is just supposed to be a library, do "compile only" or something similar -- or add a "real" program to your project so that it will have something to do.

  10. #25
    Ron
    Ron is offline
    Registered User
    Join Date
    May 2006
    Posts
    151
    Thats a great sigh of relief. I didnt know about the -c functionality. I thought there was something wrong with my program.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unions & Structs... Puzzled.
    By Sparrowhawk in forum C Programming
    Replies: 10
    Last Post: 12-14-2008, 04:45 PM
  2. classes, structs and unions
    By Luciferek in forum C++ Programming
    Replies: 24
    Last Post: 08-09-2008, 11:26 AM
  3. Possible to create methods for data in structs?
    By eccles in forum C Programming
    Replies: 19
    Last Post: 12-15-2004, 09:46 AM
  4. why unions and structs couldnt be initialized ?
    By black in forum C++ Programming
    Replies: 5
    Last Post: 11-11-2002, 05:05 AM
  5. Structs and Unions
    By Encrypted in forum C Programming
    Replies: 4
    Last Post: 11-05-2002, 03:53 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21