2014-11-25 2 views
-3

동적 메모리 할당을 사용하여이 간단한 버블 정렬 프로그램을 작성했습니다. 저는 VC++ 컴파일러를 사용하고 있습니다.이 프로그램에서 잘못된 점은 무엇입니까?

// bubble_sort.cpp : Defines the entry point for the console application. 
// 

#include "stdafx.h" 
#include <stdio.h> 
#include <stdlib.h> 
void bubble_sort(int a[],int n); 
int main() 
{ 
    int *p,i; 
    int n; 
    printf("Enter number of array elements\n"); 
    scanf("%d",&n); 
    p=(int*)malloc(sizeof(int)*n); 
    for(i=0;i<n;i++) 
     scanf("%d",(p+i)); 
    bubble_sort(p,5); 
    printf("Sorted elements\n"); 
    for(i=0;i<n;i++) 
     printf("%d ",p[i]); 
    free(p); 
    system("pause"); 
    return 0; 
} 
void bubble_sort(int a[],int n) 
{ 
    int i,j,temp; 
    for(i=0;i<n;i++) 
    { 
     for(j=0;j<n-1-i;j++) 
     { 
      if(a[j]>a[j+1]) 
      { 
       temp=a[j]; 
       a[j]=a[j+1]; 
       a[j+1]=temp; 
      } 
     } 
    } 
} 

위의 프로그램에서 잘못된 점은 무엇입니까? 컴파일러는 다음 경고를 표시합니다. 무슨 뜻이에요?

Warning 1 warning C4996: 'scanf': This function or variable may be unsafe. Consider using scanf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details. 

Warning 2 warning C4996: 'scanf': This function or variable may be unsafe. Consider using scanf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details. 

도와주세요.

+1

경고는 당신이 거의 알아야 할 모든 것을 말해줍니다. 어느 부분이 불분명합니까? – Biffen

+2

그런 질문은 아마도 http://codereview.stackexchange.com/에 더 적합 할 것입니다. 그러나 프로그램이 예상 한대로 작동한다면 그것은 올바른 의미입니다. – Biffen

+1

scanf에는 많은 문제가 있으므로 피해야합니다. - http://c-faq.com/stdio/scanfprobs.html – user93353

답변

3

이것은 프로그램에서 문제가되지 않습니다. microsoft deprecated scanf 함수 대신 scanf_s 함수가 도입되어 보안 기능을 도입했습니다. 코드를 컴파일하려면 두 가지 옵션이 있습니다.

  1. 는. (http://msdn.microsoft.com/en-us/library/w40768et.aspx)
  2. 을 대신 검사의 scanf_s 기능을 사용하거나 컴파일러 설정에서 매크로 "_CRT_SECURE_NO_WARNINGS"를 넣어.
관련 문제