- 论坛徽章:
- 0
|
本帖最后由 wangzhen11aaa 于 2011-11-03 16:37 编辑
我描述一下这个很奇怪的事情: 我在main.c中分配数组然后赋值。如果用make 命令: 会出现会多输入一个数字的问题,而且之后就会出现段错误?!如果我单独编译它,就没有错误,这是怎么回事?
我现在不知道是怎么回事。请教。
代码如下。-
- 9 /*这是我测试的一个程序*/
- 10 #include<stdio.h>
- 11 #include<stdlib.h>
- 12 int A[2];
- 13 int main()
- 14 {
- 15 int i;
- 16 for (i = 1; i < 2; i++)
- 17 scanf("%d", &A[i ])
- 18
- 19 printf("%p\n", A);
- 20 printf("%p\n", &A[1]);
- 21
- 22 for(i = 1; i < 2; i++)
- 23 printf("%d\n", A);
- }
复制代码 这里这个程序是测试用的。证明了数组是声明或者定义都会分配内存。
./testshuzu
1
0x80496d8
0x80496dc
1
这里成功了,但是我的另一个小程序却出现了.这是一个排序:我列出所有的文件,
- 1 /* This file is the main.c
- 2 */
- 3
- 4 #include "heap.h"
- 5 #include<stdio.h>
- 6 #include"heapsort.h"
- 7
- 8 int heapsize;
- 9
- 10 int main()
- 11 {
- 12
- 13 int B[11];
- 14 int n;
- 15 printf("The B's size %d", sizeof (B));
- 16
- 17 printf("Input the nums that you want to sort \n");
- 18 for(n = 1; n < 11; n++)
- 19 scanf("%d \n", &B[n]);
- 20 printf("end in scan");
- 21 heapsize = Length(B);
- 22 printf("The heapsize is %d", heapsize);
- 23 Heapsort(B);
- 24 printf("The sorted nums are : \n");
- 25 for (; n >=1; n--)
- 26 printf("%d\n", B[n]);
- 27
- 28
- 29 return 0;
- 30 }
复制代码 复制代码
/*This is the heapsort.h*/
3 #ifndef HEAPSORT_H
4 #define HEAPSORT_H
5 #endif
6
7 void Heapsort(int A[]);
8
9
复制代码
/*This is the heapify.h*/
4 #ifndef HEAPIFY_H
5 #define HEAPIFY_H
6 #endif
7
8 void Max_heapify(int A[], int i);
9
10
复制代码
/*This is the build_max_heap.h*/
3 #ifndef BUILD_MAX_HEAP_H
4 #define BUILD_MAX_HEAP_H
5 #endif
6 void Build_max_heap(int C[]);
7
复制代码
[/code]
1 /*This is the heapsort.c*/
2 #include"heapsort.h"
3 #include"heapify.h"
4 #include"heap.h"
5 extern int heapsize;
6 void Heapsort(int A[])
7 {
8 int i;
9 Build_max_heap(A);
10 for(i = heapsize ; i >= 2; i--)
11 {
12 Exchange(A[1] , A);
13 heapsize--;
14 Max_heapify(A, 1);
15 }
16 }
复制代码
/*This is the heapify file*/
1 #include"heap.h"
2 #include"heapify.h"
3 extern unsigned int heapsize;
4 void Max_heapify(int A[],int i)
5 {
6 int l, r;
7 l = Left(i);
8 r = Right(i);
9
10 int largest;
11 if(l <= heapsize && A[l] > A)
12 largest = l;
13 else
14 largest = i;
15 if ( r <= heapsize && A[r] > A[largest])
16 largest = r;
17 if (largest != i)
18 Exchange(A, A[largest]);
19 Max_heapify(A, largest);
20 // return 0;
21 }
复制代码
1 #include"heapify.h"
2 #include"build_max_heap.h"
3 #include"heap.h"
4 void Build_max_heap(int C[ ])
5 {
6 int i;
7 for(i = Length(C)>>1; i >= 1; i-- )
8 Max_heapify(C, i);
9 }
复制代码
[/code]
- 1
- 2 #ifndef HEAP_H
- 3 #define HEAP_H
- 4 #endif
- 5
- 6 #define Heap_size(B) (sizeof(B) /sizeof(B[1]) - 1)
- 7
- 8 #define Length(N) (sizeof(N) /sizeof(N[1]) - 1)
- 9
- 10 #define Left(i) i<<1
- 11 #define Right(i) ((i<<1)+1)
- 12
- 13 #define Exchange(x, y)do{\
- 14 typeof(int) m = x; \
- 15 x = y; \
- 16 y = m; \
- 17 }while(0)
复制代码 这是makefile文件
1
2 object= main.o heapsort.o heapify.o build_max_heap.o
3 edit : $(object)
4 cc -o edit $(object)
5 main.o : heapsort.h
6 # cc -g -Wall -c main.c
7 heapsort.o : build_max_heap.h
8 heapsort.o heapify.o build_max_heap.o : heap.h
9 # cc -g -c heapsort.c
10 #heapify.o: heap.h
11 # cc -g -c heapify.c
12 build_max_heap.o heapsort.o: heapify.h
13 # cc -g -c build_max_heap.c
14 .PHONY: clean
15 clean:
16 -rm edit $(object)
复制代码 |
|