鱼C论坛

 找回密码
 立即注册
查看: 1516|回复: 3

[已解决]用realloc动态申请内存出现以下提示

[复制链接]
发表于 2017-7-16 10:01:30 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
也不是每次都出错看不出哪里有在申请的内存外存取数据啊
还有随机的出现输入数字被改成0是什么原因
具体代码如下:
#include <stdio.h>
#include <stdlib.h>
//输入若干数字,从小到大排序

void quick_sort(int array[], int left, int right); //从小到大排序

int main(void)
{
        int i = 0;
        int num = 0;
        int count = 0;
        int *ptr;
        printf("Please input numbers(double '-1' to end):\n");
        do
        {
                scanf("%d", &num);
                ptr = (int *)realloc(ptr, count * sizeof(int));
                if(ptr == NULL)
                {
                        perror("realloc");
                        exit(-1);
                }
                ptr[count] = num;
                count++;
        }while(ptr[count-2] != -1 || num != -1);
        quick_sort(ptr, 0, count - 3);
        for(i = 0; i < count - 2; i++)
        {
                printf("%d ", ptr[i]);
        }
        putchar('\n');
        free(ptr);
        return 0;
}

void quick_sort(int array[], int left, int right) //从小到大排序
{
        int temp, pivot;
        int i = left, j = right;
        pivot = array[(left + right) / 2];
        while(i <= j)
        {
                while(array[i] < pivot)
                {
                        i++;
                }
                while(array[j] > pivot)
                {
                        j--;
                }
                if(i <= j)
                {
                        temp = array[j];
                        array[j] = array[i];
                        array[i] = temp;
                        i++;
                        j--;
                }
        }
        if(left < j)
        {
                quick_sort(array, left, j);
        }
        if(i < right)
        {
                quick_sort(array, i, right);
        }
}
最佳答案
2017-7-16 12:56:37
2017-07-16_125506.png
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. //输入若干数字,从小到大排序

  4. void quick_sort(int array[], int left, int right); //从小到大排序

  5. int main(void)
  6. {
  7.         int i = 0;
  8.         int num = 0;
  9.         int count = 1;
  10.         int *ptr;

  11.                 ptr = (int *)malloc(sizeof(int));
  12.         printf("Please input numbers(double '-1' to end):\n");
  13.         do
  14.         {
  15.                 scanf("%d", &num);
  16.                 ptr = (int *)realloc(ptr, count * sizeof(int));
  17.                 if(ptr == NULL)
  18.                 {
  19.                         perror("realloc");
  20.                         exit(-1);
  21.                 }
  22.                 ptr[count-1] = num;
  23.                 count++;
  24.         }while(ptr[count-2] != -1 || num != -1);
  25.         quick_sort(ptr, 0, count - 3);
  26.         for(i = 0; i < count - 2; i++)
  27.         {
  28.                 printf("%d ", ptr[i]);
  29.         }
  30.         putchar('\n');
  31.         free(ptr);
  32.         return 0;
  33. }

  34. void quick_sort(int array[], int left, int right) //从小到大排序
  35. {
  36.         int temp, pivot;
  37.         int i = left, j = right;
  38.         pivot = array[(left + right) / 2];
  39.         while(i <= j)
  40.         {
  41.                 while(array[i] < pivot)
  42.                 {
  43.                         i++;
  44.                 }
  45.                 while(array[j] > pivot)
  46.                 {
  47.                         j--;
  48.                 }
  49.                 if(i <= j)
  50.                 {
  51.                         temp = array[j];
  52.                         array[j] = array[i];
  53.                         array[i] = temp;
  54.                         i++;
  55.                         j--;
  56.                 }
  57.         }
  58.         if(left < j)
  59.         {
  60.                 quick_sort(array, left, j);
  61.         }
  62.         if(i < right)
  63.         {
  64.                 quick_sort(array, i, right);
  65.         }
  66. }
复制代码
Screen Shot 2017-07-16 at 9.40.38 AM.jpg
Screen Shot 2017-07-16 at 9.58.00 AM.jpg
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2017-7-16 12:56:37 | 显示全部楼层    本楼为最佳答案   
2017-07-16_125506.png
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. //输入若干数字,从小到大排序

  4. void quick_sort(int array[], int left, int right); //从小到大排序

  5. int main(void)
  6. {
  7.         int i = 0;
  8.         int num = 0;
  9.         int count = 1;
  10.         int *ptr;

  11.                 ptr = (int *)malloc(sizeof(int));
  12.         printf("Please input numbers(double '-1' to end):\n");
  13.         do
  14.         {
  15.                 scanf("%d", &num);
  16.                 ptr = (int *)realloc(ptr, count * sizeof(int));
  17.                 if(ptr == NULL)
  18.                 {
  19.                         perror("realloc");
  20.                         exit(-1);
  21.                 }
  22.                 ptr[count-1] = num;
  23.                 count++;
  24.         }while(ptr[count-2] != -1 || num != -1);
  25.         quick_sort(ptr, 0, count - 3);
  26.         for(i = 0; i < count - 2; i++)
  27.         {
  28.                 printf("%d ", ptr[i]);
  29.         }
  30.         putchar('\n');
  31.         free(ptr);
  32.         return 0;
  33. }

  34. void quick_sort(int array[], int left, int right) //从小到大排序
  35. {
  36.         int temp, pivot;
  37.         int i = left, j = right;
  38.         pivot = array[(left + right) / 2];
  39.         while(i <= j)
  40.         {
  41.                 while(array[i] < pivot)
  42.                 {
  43.                         i++;
  44.                 }
  45.                 while(array[j] > pivot)
  46.                 {
  47.                         j--;
  48.                 }
  49.                 if(i <= j)
  50.                 {
  51.                         temp = array[j];
  52.                         array[j] = array[i];
  53.                         array[i] = temp;
  54.                         i++;
  55.                         j--;
  56.                 }
  57.         }
  58.         if(left < j)
  59.         {
  60.                 quick_sort(array, left, j);
  61.         }
  62.         if(i < right)
  63.         {
  64.                 quick_sort(array, i, right);
  65.         }
  66. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-7-16 16:10:26 | 显示全部楼层

&#128591;十分感谢,我已经懂了,realloc的参数除了NULL必须是由malloc、calloc或realloc函数返回的值才行。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-7-16 16:51:18 | 显示全部楼层
知表不言 发表于 2017-7-16 16:10
&#128591;十分感谢,我已经懂了,realloc的参数除了NULL必须是由malloc、calloc或realloc函数返回的值才 ...

给个最佳吧老板
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-24 03:08

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表