자료구조 동적할당 소멸 및 동적/일반으로했슬때의 차이를 모르겠어요 .ㅠ
글쓴이: rnjsqhs1628 / 작성시간: 목, 2015/11/12 - 9:00오후
#ifndef __P__
#define __P__
typedef struct
{
int x;
int y;
}P;
void SetP(P* p,int a,int b);
void ShowP(P* p);
int PComp(P* p1,P* p2);
#endif#include <stdio.h>
#include "P.h"
void SetP(P* p,int a,int b)
{
p->x=a;
p->y=b;
}
void ShowP(P* p)
{
printf("(%d%,%d)\n", p->x,p->y);
}
int PComp(P* p1,P* p2)
{
if(p1->x==p2->x && p1->y==p2->y)
return 0;
else if(p1->x==p2->x)
return 1;
else if(p1->y==p2->y)
return 2;
else
return -1;
}#ifndef __List__
#define __List__
#include "P.h"
#define true 1
#define false 0
#define Len 100
typedef P* Ldate;
typedef struct
{
Ldate d[Len];
int count;
int current;
}List;
void ListInit(List* l);
void LInSert(List* l,Ldate d);
int LCount(List* l);
int LFirst(List* l,Ldate* d);
int LNext(List* l,Ldate* d);
Ldate LRemove(List* l);
#endif#include <stdio.h>
#include "리스트.h"
void ListInit(List* l)
{
l->count=0;
l->current=-1;
}
void LInSert(List* l,Ldate d)
{
if(l->count>=Len)
{
puts("저장 공간 초과");
return;
}
l->d[(l->count)++]=d;
}
int LCount(List* l)
{
return l->count;
}
int LFirst(List* l,Ldate* d)
{
if(l->count==0)
return true;
l->current=0;
*d=l->d[l->current];
return true;
}
int LNext(List* l,Ldate* d)
{
if(l->current>=l->count-1)
return false;
(l->current)++;
*d=l->d[l->current];
return true;
}
Ldate LRemove(List* l)
{
int i;
int c=l->count;
int cu=l->current;
Ldate d=l->d[cu];
for(i=cu;i<c-1;i++)
l->d[i]=l->d[i+1];
l->count--;
(l->current)--;
return d;
}#include <stdio.h>
#include <stdlib.h>
#include "리스트.h"
int main()
{
List list;
int i;
P *p;
P p2; <-- 1번-- 이부분을 P* p2=(P*)malloc(sizeof(P)) 이렇게바꾸고 SetP() 이것두 다하고 PCom(p,p2)
ListInit(&list);//초기화 로바꾸고 해봤는데 이렇게 할경우는 아예 변화가없네요 이렇게 해두 되지않나요 .ㅜ?
p2.x=2;
p2.y=0;
for(i=0;i<5;i++)
{
p=(P*)malloc(sizeof(P));
SetP(p,i+1,i+2);
LInSert(&list,p); //추가
}
printf("데이터 수:%d\n\n", LCount(&list)); //데이터 수 측정
if(LFirst(&list,&p)) //저장된 데이터 출력
{
ShowP(p);
while(LNext(&list,&p))
ShowP(p);
}
printf("\n\n");
if(LFirst(&list,&p)) //저장된 데이터 삭제
{
if(PComp(p,&p2)==1)
{
p=LRemove(&list);
free(p);
}
while(LNext(&list,&p))
{
if(PComp(p,&p2)==1)
{
printf("3213213dwdwq\n");
p=LRemove(&list);
free(p);
}
}
}
printf("\n\n");
printf("데이터 수:%d\n\n", LCount(&list)); //데이터 수 측정
if(LFirst(&list,&p)) //저장된 데이터 출력
{
ShowP(p);
while(LNext(&list,&p))
ShowP(p);
}
printf("\n\n");
return 0;
}긴글 읽어주셔서 감사합니다 일딴 제가 너무 궁금한부분이 2가지가있는데 .
1가지가 main.c에 1번 이라구 써져있는곳 이고 또한가지는 p가 총 4번 동적할당을 했는데 소멸하는건 LRemove 에
나오는 주소의반환값 만입니다 . 그럼 나머지 동적할당한 것들은 어디서 없어지나요 ..??
긴글읽어주셔서 감사합니다 이 2가지가 좀 궁금합니다 .!!
긴글 읽어주셔서 감사합니다 일딴 제가 너무 궁금한부분이 2가지가있는데 .
1가지가 main.c에 1번 이라구 써져있는곳 이고 또한가지는 p가 총 4번 동적할당을 했는데 소멸하는건 LRemove 에
나오는 주소의반환값 만입니다 . 그럼 나머지 동적할당한 것들은 어디서 없어지나요 ..??
긴글읽어주셔서 감사합니다 이 2가지가 좀 궁금합니다 .!!
Forums:


포인터와 배열을 설명 드립니다.
자세한 내용은 테스트. 네이버 책. 도서관. 서점등에서 확인하실 수 있습니다.
#include <iostream> #include <stdio.h> #include <stdlib.h> #include <memory.h> int main(int argc, char** argv) { // char c = 'a'; printf("sizeof(char) %d\n", sizeof(char)); printf("sizeof(c) %d\n", sizeof(c) ); printf("sizeof(&c) %d\n", sizeof(&c) ); printf("\n"); printf(" (c) %d\n", (c) ); printf(" (&c) 0x%d\n", (&c) ); printf("\n"); // char * p = NULL; p = &c; printf("sizeof(p) %d\n", sizeof(p) ); printf("sizeof(*p) %d\n", sizeof(*p) ); printf("sizeof(&p) %d\n", sizeof(&p) ); printf("\n"); printf(" (p) 0x%d\n", (p) ); printf(" (&p) 0x%d\n", (&p) ); printf(" (*p) %d\n", (*p) ); printf("\n"); // p = (char*) malloc (10*sizeof(char)); //10개 * 데이터 1개의 크기 memset(p, '\0', 10*sizeof(char)); printf("sizeof(p) %d\n", sizeof(p) ); printf("sizeof(*p) %d\n", sizeof(*p) ); printf("sizeof(&p) %d\n", sizeof(&p) ); printf("\n"); printf(" (p) 0x%d\n", (p) ); printf(" (&p) 0x%d\n", (&p) ); printf(" (*p) %d\n", (*p) ); printf("\n"); free(p); // int * n = NULL; n = (int*) malloc (10*sizeof(int)); //10개 * 데이터 1개의 크기 memset(n, '\0', 10*sizeof(int)); printf("sizeof(n) %d\n", sizeof(n) ); printf("sizeof(*n) %d\n", sizeof(*n) ); printf("sizeof(&n) %d\n", sizeof(&n) ); printf("\n"); printf(" (n) 0x%d\n", (n) ); printf(" (&n) 0x%d\n", (&n) ); printf(" (*n) %d\n", (*n) ); printf("\n"); free(n); return 0; }----------------------------------------------------------------------------
젊음'은 모든것을 가능하게 만든다.
매일 1억명이 사용하는 프로그램을 함께 만들어보고 싶습니다.
정규 근로 시간을 지키는. 야근 없는 회사와 거래합니다.
각 분야별. 좋은 책'이나 사이트' 블로그' 링크 소개 받습니다. shintx@naver.com
답변해 주셔서 감사합니다.
그런데
P* p2=(P*)malloc(sizeof(P)) 이렇게바꾸고
SetP() 이것두 다하고 PCom(p,p2)
pCom(p* p1,p* p2) 그냥 해두 상관없지않나요 ..?
p1두 동적 할당 받아서 가능한데 왜 p2 는 동적 할당하면 안되는지가 너무 궁금합니다.
이렇게 하시면 됩니다.
참고해보세요.
웹에서 테스트해보실 수 있습니다.
http://codepad.org/
자세한 사항은 MSDN이나
구글에서 malloc() c 언어'라고 치시면 함수 설명이 나옵니다.
new와 delete [] 또는 delete 는 또 사용하는 방법이 다릅니다. C++ 책보시면서 확인하실 수 있습니다.
네이버에서 책 검색해보시면 편합니다.
오픈소스 무료 강의도 있습니다.
http://olc.kr/main/index.jsp
LONG 형은 windows.h 에서 사용합니다. ㅇ_ㅇ;;
//구조체 선언 typedef struct { LONG x; LONG y; }POINT; ------------------------------------------------- //구조체 변수생성 POINT pt; pt.x = 10; pt.y = 20; ------------------------------------------------- //구조체 포인터 생성 (4바이트) POINT* ppt_addr = NULL; //구조체의 주소 4바이트를 포인터에 입력 ppt_addr = &pt; ppt_addr->x = 10; ppt_addr->y = 20; ------------------------------------------------- POINT* ppt = NULL; int size = sizeof(POINT); printf("POINT 에 크기는 %d 입니다.\n", size); //메모리 크기 할당(생성) ppt = (POINT*) malloc ( size); if(ppt == NULL) { printf("malloc() 생성이 실패했습니다.\n"); return; } ppt->x = 10; ppt->y = 20; //메모리 해제 free(ppt); //NULL로 초기화 알아보기 쉽게... ppt = NULL;----------------------------------------------------------------------------
젊음'은 모든것을 가능하게 만든다.
매일 1억명이 사용하는 프로그램을 함께 만들어보고 싶습니다.
정규 근로 시간을 지키는. 야근 없는 회사와 거래합니다.
각 분야별. 좋은 책'이나 사이트' 블로그' 링크 소개 받습니다. shintx@naver.com
자문이라는단어가.....
자문은 윗사람이 아랫사람에게 물어볼때 자문이라는 표현을 쓰지 않나요..?
혹시나해서요
댓글 달기