Skip to content
Snippets Groups Projects
Commit a3c2ccde authored by KimWooJeong's avatar KimWooJeong
Browse files

assignment output is wrong....aaa

parent 81530590
No related branches found
No related tags found
No related merge requests found
File added
#include "alloc.h"
#include <stdio.h>
#include <sys/types.h>
void* start = 0;
void* end = 0;
extern int fit_type = 1;
void* m_malloc(size_t size)
{
meta* block;
if(start == 0)
{
start = sbrk(0);
end = start;
}
if(end == start)
{
block = -1;
}
meta* nextblk = end;
end += size + sizeof(meta);
if(sbrk(size+sizeof(meta)) == -1)
{
return 0;
}
nextblk->free=0;
nextblk->next=0;
nextblk->prev = block;
nextblk->size = size;
block = nextblk;
printf("%d %d ",nextblk->free,nextblk->size);
return block->data;
}
void m_free(void *ptr)
{
ptr = ptr-sizeof(meta);
if(!ptr)
{
printf("NULL\n");
return;
}
}
void* m_realloc(void* ptr,size_t size){
//GG
}
\ No newline at end of file
#ifndef _ALLOC_H_
#define _ALLOC_H_
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdbool.h>
#include <stdint.h>
typedef struct meta_struct {
uint32_t size;
uint32_t free;
struct meta_struct* next;
struct meta_struct* prev;
char data[1];
} meta;
#endif
void* m_malloc(size_t size);
void m_free(void* ptr);
void* m_realloc(void* ptr,size_t size);
3 F
s Think like a man of action and act like man of thought.
s Courage is very important. Like a muscle, it is strengthened by use.
s Life is the art of drawing sufficient conclusions from insufficient premises.
\ No newline at end of file
main 0 → 100755
File added
#include "alloc.h"
int main()
#include <string.h>
#include <stdio.h>
extern int fit_type;
int main(int argc,char* argv[])
{
char buf[1024];
char* tok;
int line_num; // 1,2,3,4,5
int fit_type=0;
FILE* fp = fopen(argv[1],"r");
fgets(buf,sizeof(buf),fp);
tok = strtok(buf, " ");
line_num = atoi(tok);
if(line_num==0)
{
printf("line number is zero\n");
return 0;
}
tok = strtok(NULL, " ");
if(tok[0]=='F')
fit_type = 1;
else if(tok[0]=='B')
fit_type = 2;
else if(tok[0]=='W')
fit_type = 3;
else
{
printf("fit type is not correct\n");
return 0;
}
char** lines = (char**)malloc(line_num * sizeof(char*));
for(int i=0;i<line_num;i++)
{
memset(buf,0,sizeof(buf));
fgets(buf,sizeof(buf),fp);
buf[strlen(buf)-1]=0;
if(buf[0]=='s')
{
lines[i] = (char*)m_malloc(sizeof(char)*strlen(buf+2)+1);
strcpy(lines[i], buf+2);
printf("%s\n", lines[i]);
continue;
}
}
//for(int i=0; i<line_num;i++)
//{
// printf("%s\n", lines[i]);
//}
return 0;
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment