sbrk 때문에 소스코드 실행이 안되요 ㅠ.ㅠ

nety2k의 이미지

방금 the c programming language 라는 책에서
malloc() 이거 함수 구현하는것을 열심히 타이핑 했습니다. 그런데 sbrk()이함수는 UNIX 함수 호출이라고 나오네요.. windowsxp에서 sbrk()이런 역할을 하는 함수를 가르쳐 주세요..

#include<stdio.h>
#include<string.h>

#define NALLOC 1024

typedef long Align;

union header
{
struct {
union header *ptr;
unsigned size;
}s;

Align x;
};

typedef union header Header;

static Header base;
static Header *freep = NULL;

void free(void *ap)
{
Header *bp, *p;
bp = (Header*)ap -1;

for ( p = freep ; !(bp>p &&bp < p->s.ptr); p = p->s.ptr)
if ( p >= p->s.ptr && (bp >p ||bp <p->s.ptr))
break;

if ( bp + bp->s.size == p->s.ptr)
{
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)
{
p->s.size +=bp->s.size;
p ->s.ptr = bp ->s.ptr;
}
else
p->s.ptr = bp;
freep = p;
}

static Header *morecore( unsigned nu)
{
char *cp, *sbrk(int );
Header *up;

if ( nu <NALLOC)
nu = NALLOC;
cp = sbrk(nu * sizeof(Header));
if ( cp == (char *) -1)
return NULL;
up = (Header *) cp;
up ->s.size = nu;
free ((void*)(up+1));
return freep;

}

void *mymalloc (unsigned nbytes)
{
Header *p, *prevp;
Header *morecore(unsigned);
unsigned nunits;

nunits = (nbytes +sizeof(Header)-1)/sizeof(Header)+1;
if ((prevp = freep)==NULL)
{
base.s.ptr = freep = prevp = &
base.s.size = 0;

}

for ( p = prevp ->s.ptr; ; prevp = 0, p = p->s.ptr)
{
if ( p ->s.size >= nunits)
{
if ( p->s.size == nunits)
prevp ->s.ptr= p->s.ptr;

else{
p->s.size -= nunits;
p += p->s.size;
p->s.size = nunits;
}

freep = prevp;
return (void*)(p+1);
}

if ( p == freep)
if ((p = morecore(nunits)) ==NULL)
return NULL;
}

}

void main()
{
char * p ;

p = (char *)mymalloc (30);
strcpy(p, "hello world");
printf("%s", p);
}

ageldama의 이미지

기본적으로는 없을겁니다.

UWIN(맞나염?)이나 cygwin등과 같은 posix호환 레이어를 제공하는 환경에서라면 있을지도 모르지만...

sbrk을 LocalAlloc등의 Win32 API로 작성해주어야 할겁니다.

그리고...

#include <stdio.h> 
#include <string.h> 

#define NALLOC 1024 

typedef long Align; 

union header 
{ 
	struct { 
		union header *ptr; 
		unsigned size; 
	}s;
	Align x; 
}; 

typedef union header Header; 

static Header base; 
static Header *freep = NULL; 


void myfree(void *ap) 
{ 
	Header *bp, *p; 
	bp = (Header*)ap -1; 
	
	for ( p = freep ; !(bp>p &&bp < p->s.ptr); p = p->s.ptr) 
		if ( p >= p->s.ptr && (bp >p ||bp <p->s.ptr)) 
			break; 
	
	if ( bp + bp->s.size == p->s.ptr) 
	{ 
		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) 
	{ 
		p->s.size +=bp->s.size; 
		p ->s.ptr = bp ->s.ptr; 
	} 
	else 
		p->s.ptr = bp; 
	freep = p; 
} 

static Header *morecore( unsigned nu) 
{ 
	char *cp, *sbrk(int ); 
	Header *up; 
	
	if ( nu <NALLOC) 
		nu = NALLOC; 
	cp = sbrk(nu * sizeof(Header)); 
	if ( cp == (char *) -1) 
		return NULL; 
	up = (Header *) cp; 
	up ->s.size = nu; 
	myfree ((void*)(up+1)); 
	return freep; 
} 



void *mymalloc (unsigned nbytes) 
{ 
	Header *p, *prevp; 
	Header *morecore(unsigned); 
	unsigned nunits; 
	
	nunits = (nbytes +sizeof(Header)-1)/sizeof(Header)+1; 
	if ((prevp = freep)==NULL) 
	{ 
		base.s.ptr = freep = prevp = & 
		base.s.size = 0; 
	} 
	
	for ( p = prevp ->s.ptr; ; prevp = 0, p = p->s.ptr) 
	{ 
		if ( p ->s.size >= nunits) 
		{ 
			if ( p->s.size == nunits) 
				prevp ->s.ptr= p->s.ptr; 			
			else
			{ 
				p->s.size -= nunits; 
				p += p->s.size; 
				p->s.size = nunits; 
			} 
		
			freep = prevp; 
			return (void*)(p+1); 
		} 
	
		if ( p == freep) 
			if ((p = morecore(nunits)) ==NULL) 
				return NULL; 
	} 
	
} 


void main() 
{ 
	char * p ; 
	
	p = (char *)mymalloc (30); 
	strcpy(p, "hello world"); 
	printf("%s", p); 
	myfree((void*)p);
}
...가 맞나요? BBCode을 안쓰셔서;;; 깨져서요;;;
코드가 맞는지 확인해주세요. 저도 한번 해보게요.

그럼~

----
The future is here. It's just not widely distributed yet.
- William Gibson

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.