调用realloc函数后除了第一个其他全部变为野指针

to__be 2017-10-16 09:23:35
#include <iostream>
#define QUEUE_INIT_SIZE 4
#define QUEUE_ADD 10
typedef struct {
int *rear;
int *front;
int queuesize;
}SqQueue;
void InitQueue(SqQueue &s) {
s.front = (int *)malloc(sizeof(int));
if (!s.rear) exit(0);
s.rear = s.front;
s.queuesize = QUEUE_INIT_SIZE;
}
void EnQueue(SqQueue &s, int e) {
if (s.rear - s.front >= s.queuesize)
{
int *ptr = (int *)realloc(s.front, (s.queuesize + QUEUE_ADD) * (sizeof(int)));
if (!ptr) exit(0);
s.front = ptr;
/*printf("%d\n", *(s.front ));
printf("%d\n", *(s.front+1));
printf("%d\n", *(s.front + 1+1));*/
s.rear = s.front + s.queuesize;
s.queuesize += QUEUE_ADD;
printf("申请空间成功\n");
}
*s.rear = e;
s.rear++;
printf("元素%d已入队列\n", e);
}
void DeQueue(SqQueue &s) {
if (s.front == s.rear)
{
printf("队列为空\n");
exit(0);
}
int e = *(s.front);
s.front++;
printf("元素%d已出队列\n", e);
}
int main() {
SqQueue s;
InitQueue(s);
EnQueue(s, 1);
EnQueue(s, 2);
EnQueue(s, 3);
EnQueue(s, 4);
EnQueue(s, 5);
DeQueue(s);
DeQueue(s);
DeQueue(s);
return 0;
}



在使用realloc之后除了第一个s.front,其他的都变成了野指针
求解,谢谢。
...全文
189 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
to__be 2017-10-31
  • 打赏
  • 举报
回复
谢谢大家..问题基本解决
自信男孩 2017-10-17
  • 打赏
  • 举报
回复
realloc后修改s.rear的位置,反而会导致再次入队时缓存越界
自信男孩 2017-10-17
  • 打赏
  • 举报
回复
#include <iostream>
#include <cstdio>
#include <cstdlib>

#define QUEUE_INIT_SIZE 4
#define QUEUE_ADD 10

typedef struct {
    int *rear;
    int *front;
    int queuesize;
}SqQueue;

void InitQueue(SqQueue &s)
{
    s.front = (int *)malloc(sizeof(int));
    if (!s.rear)
        exit(0);
    s.rear = s.front;
    s.queuesize = QUEUE_INIT_SIZE;
}
void EnQueue(SqQueue &s, int e)
{
    if (s.rear - s.front >= s.queuesize) {
        int *ptr = (int *)realloc(s.front, (s.queuesize + QUEUE_ADD) * (sizeof(int)));
        if (!ptr)
            exit(0);
        s.front = ptr;
#if 0
        printf("%d\n", *(s.front ));
          printf("%d\n", *(s.front+1));
          printf("%d\n", *(s.front + 1+1));
#endif
        //s.rear = s.front + s.queuesize;    /*不需要修改s.rear的位置*/
        s.queuesize += QUEUE_ADD;
        printf("申请空间成功\n");
    }
    *s.rear = e;
    s.rear++;
    printf("元素%d已入队列\n", e);
}
void DeQueue(SqQueue &s) {
    if (s.front == s.rear)
    {
        printf("队列为空\n");
        exit(0);
    }
    int e = *(s.front);
    s.front++;
    printf("元素%d已出队列\n", e);
}
int main() {
    SqQueue s;
    InitQueue(s);
    EnQueue(s, 1);
    EnQueue(s, 2);
    EnQueue(s, 3);
    EnQueue(s, 4);
    EnQueue(s, 5);
    DeQueue(s);
    DeQueue(s);
    DeQueue(s);
    return 0;
}
当队列满时,不需要修改s.rear的位置,因为s.rear的位置只会在入队的时候修改; realloc后不会出现野指针的问题。
赵4老师 2017-10-17
  • 打赏
  • 举报
回复
realloc Reallocate memory blocks. void *realloc( void *memblock, size_t size ); Routine Required Header Compatibility realloc <stdlib.h> and <malloc.h> ANSI, Win 95, Win NT For additional compatibility information, see Compatibility in the Introduction. Libraries LIBC.LIB Single thread static library, retail version LIBCMT.LIB Multithread static library, retail version MSVCRT.LIB Import library for MSVCRT.DLL, retail version Return Value realloc returns a void pointer to the reallocated (and possibly moved) memory block. The return value is NULL if the size is zero and the buffer argument is not NULL, or if there is not enough available memory to expand the block to the given size. In the first case, the original block is freed. In the second, the original block is unchanged. The return value points to a storage space that is guaranteed to be suitably aligned for storage of any type of object. To get a pointer to a type other than void, use a type cast on the return value. Parameters memblock Pointer to previously allocated memory block size New size in bytes Remarks The realloc function changes the size of an allocated memory block. The memblock argument points to the beginning of the memory block. If memblock is NULL, realloc behaves the same way as malloc and allocates a new block of size bytes. If memblock is not NULL, it should be a pointer returned by a previous call to calloc, malloc, or realloc. The size argument gives the new size of the block, in bytes. The contents of the block are unchanged up to the shorter of the new and old sizes, although the new block can be in a different location. Because the new block can be in a new memory location, the pointer returned by realloc is not guaranteed to be the pointer passed through the memblock argument. realloc calls malloc in order to use the C++ _set_new_mode function to set the new handler mode. The new handler mode indicates whether, on failure, malloc is to call the new handler routine as set by _set_new_handler. By default, malloc does not call the new handler routine on failure to allocate memory. You can override this default behavior so that, when realloc fails to allocate memory, malloc calls the new handler routine in the same way that the new operator does when it fails for the same reason. To override the default, call _set_new_mode(1) early in your program, or link with NEWMODE.OBJ. When the application is linked with a debug version of the C run-time libraries, realloc resolves to _realloc_dbg. For more information about how the heap is managed during the debugging process, see Using C Run-Time Library Debugging Support. Example /* REALLOC.C: This program allocates a block of memory for * buffer and then uses _msize to display the size of that * block. Next, it uses realloc to expand the amount of * memory used by buffer and then calls _msize again to * display the new amount of memory allocated to buffer. */ #include <stdio.h> #include <malloc.h> #include <stdlib.h> void main( void ) { long *buffer; size_t size; if( (buffer = (long *)malloc( 1000 * sizeof( long ) )) == NULL ) exit( 1 ); size = _msize( buffer ); printf( "Size of block after malloc of 1000 longs: %u\n", size ); /* Reallocate and show new size: */ if( (buffer = realloc( buffer, size + (1000 * sizeof( long )) )) == NULL ) exit( 1 ); size = _msize( buffer ); printf( "Size of block after realloc of 1000 more longs: %u\n", size ); free( buffer ); exit( 0 ); } Output Size of block after malloc of 1000 longs: 4000 Size of block after realloc of 1000 more longs: 8000 Memory Allocation Routines See Also calloc, free, malloc
paschen 2017-10-17
  • 打赏
  • 举报
回复
如果当前内存不够分配,realloc会重新寻找其他位置内存,并将数据移动过去,导致原来的一些指针失效
真相重于对错 2017-10-17
  • 打赏
  • 举报
回复
引用 5 楼 to__be 的回复:
[quote=引用 1 楼 zhao4zhong1 的回复:] realloc Reallocate memory blocks. void *realloc( void *memblock, size_t size ); Routine Required Header Compatibility realloc <stdlib.h> and <malloc.h> ANSI, Win 95, Win NT For additional compatibility information, see Compatibility in the Introduction. Libraries LIBC.LIB Single thread static library, retail version LIBCMT.LIB Multithread static library, retail version MSVCRT.LIB Import library for MSVCRT.DLL, retail version Return Value realloc returns a void pointer to the reallocated (and possibly moved) memory block. The return value is NULL if the size is zero and the buffer argument is not NULL, or if there is not enough available memory to expand the block to the given size. In the first case, the original block is freed. In the second, the original block is unchanged. The return value points to a storage space that is guaranteed to be suitably aligned for storage of any type of object. To get a pointer to a type other than void, use a type cast on the return value. Parameters memblock Pointer to previously allocated memory block size New size in bytes Remarks The realloc function changes the size of an allocated memory block. The memblock argument points to the beginning of the memory block. If memblock is NULL, realloc behaves the same way as malloc and allocates a new block of size bytes. If memblock is not NULL, it should be a pointer returned by a previous call to calloc, malloc, or realloc. The size argument gives the new size of the block, in bytes. The contents of the block are unchanged up to the shorter of the new and old sizes, although the new block can be in a different location. Because the new block can be in a new memory location, the pointer returned by realloc is not guaranteed to be the pointer passed through the memblock argument. realloc calls malloc in order to use the C++ _set_new_mode function to set the new handler mode. The new handler mode indicates whether, on failure, malloc is to call the new handler routine as set by _set_new_handler. By default, malloc does not call the new handler routine on failure to allocate memory. You can override this default behavior so that, when realloc fails to allocate memory, malloc calls the new handler routine in the same way that the new operator does when it fails for the same reason. To override the default, call _set_new_mode(1) early in your program, or link with NEWMODE.OBJ. When the application is linked with a debug version of the C run-time libraries, realloc resolves to _realloc_dbg. For more information about how the heap is managed during the debugging process, see Using C Run-Time Library Debugging Support. Example /* REALLOC.C: This program allocates a block of memory for * buffer and then uses _msize to display the size of that * block. Next, it uses realloc to expand the amount of * memory used by buffer and then calls _msize again to * display the new amount of memory allocated to buffer. */ #include <stdio.h> #include <malloc.h> #include <stdlib.h> void main( void ) { long *buffer; size_t size; if( (buffer = (long *)malloc( 1000 * sizeof( long ) )) == NULL ) exit( 1 ); size = _msize( buffer ); printf( "Size of block after malloc of 1000 longs: %u\n", size ); /* Reallocate and show new size: */ if( (buffer = realloc( buffer, size + (1000 * sizeof( long )) )) == NULL ) exit( 1 ); size = _msize( buffer ); printf( "Size of block after realloc of 1000 more longs: %u\n", size ); free( buffer ); exit( 0 ); } Output Size of block after malloc of 1000 longs: 4000 Size of block after realloc of 1000 more longs: 8000 Memory Allocation Routines See Also calloc, free, malloc
感谢您的回答,但是我看realloc的说明上会引起数据丢失的只有新申请的空间比原来小的情况,不太明白为什么我的代码还是会造成数据丢失。[/quote] 你的init函数 里 s.front = (int *)malloc(sizeof(int));只分配了1个int长度,但是后面你却对4个int长度的内存有操作,你破坏了堆结构,再realloc,则出错 建议init里面malloc(sizeof(int)*4);
赵4老师 2017-10-17
  • 打赏
  • 举报
回复
#include <iostream>
#include <cstdio>
#include <cstdlib>

#define QUEUE_INIT_SIZE 4
#define QUEUE_ADD 10

typedef struct {
    int *rear;
    int *front;
    int *orig;
    int queuesize;
}SqQueue;

void InitQueue(SqQueue &s)
{
    s.orig = (int *)malloc(QUEUE_INIT_SIZE * sizeof(int));
    if (!s.orig)
        exit(0);
    s.rear = s.front = s.orig;
    s.queuesize = QUEUE_INIT_SIZE;
}
void EnQueue(SqQueue &s, int e)
{
    if (s.rear - s.orig >= s.queuesize) {
        int *ptr = (int *)realloc(s.orig, (s.queuesize + QUEUE_ADD) * (sizeof(int)));
        if (!ptr)
            exit(0);
        s.orig = ptr;
        s.queuesize += QUEUE_ADD;
        printf("申请空间成功\n");
    }
    *s.rear = e;
    s.rear++;
    printf("元素%d已入队列\n", e);
}
void DeQueue(SqQueue &s) {
    if (s.front == s.rear)
    {
        printf("队列为空\n");
        return;
    }
    int e = *(s.front);
    s.front++;
    printf("元素%d已出队列\n", e);
}
int main() {
    SqQueue s;
    InitQueue(s);
    EnQueue(s, 1);
    EnQueue(s, 2);
    EnQueue(s, 3);
    EnQueue(s, 4);
    EnQueue(s, 5);
    DeQueue(s);
    DeQueue(s);
    DeQueue(s);
    free(s.orig);
    return 0;
}
//元素1已入队列
//元素2已入队列
//元素3已入队列
//元素4已入队列
//申请空间成功
//元素5已入队列
//元素1已出队列
//元素2已出队列
//元素3已出队列
//
自信男孩 2017-10-17
  • 打赏
  • 举报
回复
引用 4 楼 to__be 的回复:
[quote=引用 2 楼 cfjtaishan 的回复:]
#include <iostream>
#include <cstdio>
#include <cstdlib>

#define QUEUE_INIT_SIZE 4
#define QUEUE_ADD 10

typedef struct {
    int *rear;
    int *front;
    int queuesize;
}SqQueue;

void InitQueue(SqQueue &s)
{
    s.front = (int *)malloc(sizeof(int));
    if (!s.rear)
        exit(0);
    s.rear = s.front;
    s.queuesize = QUEUE_INIT_SIZE;
}
void EnQueue(SqQueue &s, int e)
{
    if (s.rear - s.front >= s.queuesize) {
        int *ptr = (int *)realloc(s.front, (s.queuesize + QUEUE_ADD) * (sizeof(int)));
        if (!ptr)
            exit(0);
        s.front = ptr;
#if 0
        printf("%d\n", *(s.front ));
          printf("%d\n", *(s.front+1));
          printf("%d\n", *(s.front + 1+1));
#endif
        //s.rear = s.front + s.queuesize;    /*不需要修改s.rear的位置*/
        s.queuesize += QUEUE_ADD;
        printf("申请空间成功\n");
    }
    *s.rear = e;
    s.rear++;
    printf("元素%d已入队列\n", e);
}
void DeQueue(SqQueue &s) {
    if (s.front == s.rear)
    {
        printf("队列为空\n");
        exit(0);
    }
    int e = *(s.front);
    s.front++;
    printf("元素%d已出队列\n", e);
}
int main() {
    SqQueue s;
    InitQueue(s);
    EnQueue(s, 1);
    EnQueue(s, 2);
    EnQueue(s, 3);
    EnQueue(s, 4);
    EnQueue(s, 5);
    DeQueue(s);
    DeQueue(s);
    DeQueue(s);
    return 0;
}
当队列满时,不需要修改s.rear的位置,因为s.rear的位置只会在入队的时候修改; realloc后不会出现野指针的问题。
这是您修改后代码的运行截图,还是会出现野指针问题,还有就是我觉得应该需要修改s.rear的地址,因为s.front已经指向了新地址,如果s.rear不修改的话,新入队的元素就无法通过s.front++来出队。 如有不正确的地方,请您指正。[/quote] 没看出有啥问题,在我的PC上测试都是正常的。
to__be 2017-10-17
  • 打赏
  • 举报
回复
引用 1 楼 zhao4zhong1 的回复:
realloc Reallocate memory blocks. void *realloc( void *memblock, size_t size ); Routine Required Header Compatibility realloc <stdlib.h> and <malloc.h> ANSI, Win 95, Win NT For additional compatibility information, see Compatibility in the Introduction. Libraries LIBC.LIB Single thread static library, retail version LIBCMT.LIB Multithread static library, retail version MSVCRT.LIB Import library for MSVCRT.DLL, retail version Return Value realloc returns a void pointer to the reallocated (and possibly moved) memory block. The return value is NULL if the size is zero and the buffer argument is not NULL, or if there is not enough available memory to expand the block to the given size. In the first case, the original block is freed. In the second, the original block is unchanged. The return value points to a storage space that is guaranteed to be suitably aligned for storage of any type of object. To get a pointer to a type other than void, use a type cast on the return value. Parameters memblock Pointer to previously allocated memory block size New size in bytes Remarks The realloc function changes the size of an allocated memory block. The memblock argument points to the beginning of the memory block. If memblock is NULL, realloc behaves the same way as malloc and allocates a new block of size bytes. If memblock is not NULL, it should be a pointer returned by a previous call to calloc, malloc, or realloc. The size argument gives the new size of the block, in bytes. The contents of the block are unchanged up to the shorter of the new and old sizes, although the new block can be in a different location. Because the new block can be in a new memory location, the pointer returned by realloc is not guaranteed to be the pointer passed through the memblock argument. realloc calls malloc in order to use the C++ _set_new_mode function to set the new handler mode. The new handler mode indicates whether, on failure, malloc is to call the new handler routine as set by _set_new_handler. By default, malloc does not call the new handler routine on failure to allocate memory. You can override this default behavior so that, when realloc fails to allocate memory, malloc calls the new handler routine in the same way that the new operator does when it fails for the same reason. To override the default, call _set_new_mode(1) early in your program, or link with NEWMODE.OBJ. When the application is linked with a debug version of the C run-time libraries, realloc resolves to _realloc_dbg. For more information about how the heap is managed during the debugging process, see Using C Run-Time Library Debugging Support. Example /* REALLOC.C: This program allocates a block of memory for * buffer and then uses _msize to display the size of that * block. Next, it uses realloc to expand the amount of * memory used by buffer and then calls _msize again to * display the new amount of memory allocated to buffer. */ #include <stdio.h> #include <malloc.h> #include <stdlib.h> void main( void ) { long *buffer; size_t size; if( (buffer = (long *)malloc( 1000 * sizeof( long ) )) == NULL ) exit( 1 ); size = _msize( buffer ); printf( "Size of block after malloc of 1000 longs: %u\n", size ); /* Reallocate and show new size: */ if( (buffer = realloc( buffer, size + (1000 * sizeof( long )) )) == NULL ) exit( 1 ); size = _msize( buffer ); printf( "Size of block after realloc of 1000 more longs: %u\n", size ); free( buffer ); exit( 0 ); } Output Size of block after malloc of 1000 longs: 4000 Size of block after realloc of 1000 more longs: 8000 Memory Allocation Routines See Also calloc, free, malloc
感谢您的回答,但是我看realloc的说明上会引起数据丢失的只有新申请的空间比原来小的情况,不太明白为什么我的代码还是会造成数据丢失。
to__be 2017-10-17
  • 打赏
  • 举报
回复
引用 2 楼 cfjtaishan 的回复:
#include <iostream>
#include <cstdio>
#include <cstdlib>

#define QUEUE_INIT_SIZE 4
#define QUEUE_ADD 10

typedef struct {
    int *rear;
    int *front;
    int queuesize;
}SqQueue;

void InitQueue(SqQueue &s)
{
    s.front = (int *)malloc(sizeof(int));
    if (!s.rear)
        exit(0);
    s.rear = s.front;
    s.queuesize = QUEUE_INIT_SIZE;
}
void EnQueue(SqQueue &s, int e)
{
    if (s.rear - s.front >= s.queuesize) {
        int *ptr = (int *)realloc(s.front, (s.queuesize + QUEUE_ADD) * (sizeof(int)));
        if (!ptr)
            exit(0);
        s.front = ptr;
#if 0
        printf("%d\n", *(s.front ));
          printf("%d\n", *(s.front+1));
          printf("%d\n", *(s.front + 1+1));
#endif
        //s.rear = s.front + s.queuesize;    /*不需要修改s.rear的位置*/
        s.queuesize += QUEUE_ADD;
        printf("申请空间成功\n");
    }
    *s.rear = e;
    s.rear++;
    printf("元素%d已入队列\n", e);
}
void DeQueue(SqQueue &s) {
    if (s.front == s.rear)
    {
        printf("队列为空\n");
        exit(0);
    }
    int e = *(s.front);
    s.front++;
    printf("元素%d已出队列\n", e);
}
int main() {
    SqQueue s;
    InitQueue(s);
    EnQueue(s, 1);
    EnQueue(s, 2);
    EnQueue(s, 3);
    EnQueue(s, 4);
    EnQueue(s, 5);
    DeQueue(s);
    DeQueue(s);
    DeQueue(s);
    return 0;
}
当队列满时,不需要修改s.rear的位置,因为s.rear的位置只会在入队的时候修改; realloc后不会出现野指针的问题。
这是您修改后代码的运行截图,还是会出现野指针问题,还有就是我觉得应该需要修改s.rear的地址,因为s.front已经指向了新地址,如果s.rear不修改的话,新入队的元素就无法通过s.front++来出队。 如有不正确的地方,请您指正。
1 愉快的开始-HELLO WORLD 14 1.1 INCLUDE头文件包含 14 1.2 MAIN函数 14 1.3 注释 14 1.4 {}括号,程序题和代码块 14 1.5 声明 14 1.6 C语言自定义名字的要求 15 1.7 PRINTF函数 15 1.8 RETURN语句 15 1.9 SYSTEM系统调用 15 1.9.1 System返回值在windows和unix下的不同, 15 1.9.2 POSIX 15 1.10 C语言编译过程,GCC参数简介 16 1.10.1 C语言编译过程 16 1.10.2 -E预编译 16 1.10.3 -S汇编 16 1.10.4 -c编译 16 1.10.5 链接 16 1.11 操作系统结构 17 1.11.1 用户模式 17 1.11.2 内核模式 17 1.12 64位,32位系统区别 18 1.12.1 CPU内部结构与寄存器 18 1.12.2 RISC与CISC CPU构架 18 1.12.3 SPARC,x86与ARM 18 1.13 汇编语言 18 1.13.1 I386汇编简介 18 1.13.2 VS反汇编 19 1.14 IDE工具 19 1.14.1 QT常用快捷键 19 1.14.2 VS常用快捷键 19 1.14.3 VS断点,调试 19 2 C语言中的数据类型 19 2.1 常量 19 2.1.1 #define 19 2.1.2 const 19 2.2 字符串常量 20 2.3 二进制数、位、字节与字 20 2.4 八进制 20 2.5 十六进制 20 2.6 原码 21 2.7 反码 21 2.8 补码 21 2.9 SIZEOF关键字 22 2.10 INT类型 22 2.10.1 int常量,变量 22 2.10.2 printf输出int值 23 2.10.3 printf输出八进制和十六进制 23 2.10.4 short,long,long long,unsigned int 23 2.10.5 整数溢出 23 2.10.6 大端对齐与小端对齐 23 2.11 CHAR类型 24 2.11.1 char常量,变量 24 2.11.2 printf输出char 24 2.11.3 不可打印char转义符 24 2.11.4 char和unsigned char 25 2.12 浮点FLOAT,DOUBLE,LONG DOUBLE类型 25 2.12.1 浮点常量,变量 25 2.12.2 printf输出浮点数 25 2.13 类型限定 25 2.13.1 const 25 2.13.2 volatile 26 2.13.3 register 26 3 字符串格式化输出和输入 26 3.1 字符串在计算机内部的存储方式 26 3.2 PRINTF函数,PUTCHAR函数 27 3.3 SCANF函数与GETCHAR函数 28 4 运算符表达式和语句 29 4.1 基本运算符 29 4.1.1 = 29 4.1.2 + 29 4.1.3 – 29 4.1.4 * 29 4.1.5 / 29 4.1.6 % 29 4.1.7 += 29 4.1.8 -= 29 4.1.9 *= 29 4.1.10 /= 30 4.1.11 %= 30 4.1.12 ++ 30 4.1.13 -- 30 4.1.14 逗号运算符 30 4.1.15 运算符优先级 30 4.2 复合语句 31 4.3 空语句 31 4.4 类型转化 31 5 条件分支语句 31 5.1 关系运算符 31 5.1.1 < 31 5.1.2 <= 31 5.1.3 > 32 5.1.4 >= 32 5.1.5 == 32 5.1.6 != 32 5.2 关系运算符优先级 32 5.3 逻辑运算符 32 5.3.1 && 32 5.3.2 || 32 5.3.3 ! 33 5.4 IF 33 5.5 IF ELSE 34 5.6 IF ELSE IF 34 5.7 SWITCH与BREAK,DEFAULT 35 5.8 条件运算符? 36 5.9 GOTO语句与标号 36 6 循环语句 36 6.1 WHILE 36 6.2 CONTINUE 37 6.3 BREAK 37 6.4 DO WHILE 37 6.5 FOR 37 6.6 循环嵌套 37 7 数组 38 7.1 一维数组定义与使用 38 7.2 数组在内存的存储方式 38 7.3 一维数组初始化 38 7.4 二维数组定义与使用 39 7.5 二维数组初始化 39 8 字符串与字符数组 39 8.1 字符数组定义 39 8.2 字符数组初始化 39 8.3 字符数组使用 40 8.4 随机数产生函数RAND与SRAND 40 8.5 用SCANF输入字符串 40 8.6 字符串的结束标志 41 8.7 字符串处理函数 41 8.7.1 gets 41 8.7.2 fgets函数 41 8.7.3 puts函数 42 8.7.4 fputs函数 42 8.7.5 strlen,字符串长度 42 8.7.6 strcat,字符串追加 42 8.7.7 strncat,字符串有限追加 43 8.7.8 strcmp,字符串比较 43 8.7.9 strncmp,字符串有限比较 43 8.7.10 strcpy字符串拷贝 43 8.7.11 strncpy字符串有限拷贝 43 8.7.12 sprintf,格式化字符串 43 8.7.13 Sscanf函数 44 8.7.14 strchr查找字符 44 8.7.15 strstr查找子串 44 8.7.16 strtok分割字符串 44 8.7.17 atoi转化为int 45 8.7.18 atof转化为float 45 8.7.19 atol转化为long 45 9 函数 45 9.1 函数的原型和调用 45 9.2 函数的形参与实参 45 9.3 函数的返回类型与返回值 46 9.4 MAIN函数与EXIT函数函数的RETURN语句 46 9.5 多个源代码文件程序的编译 47 9.5.1 头文件的使用 47 9.5.2 #include与#define的意义 47 9.5.3 #ifndef与#endif 47 9.6 函数的递归 48 9.6.1 递归的过程分析 48 9.6.2 递归的优点 52 9.6.3 递归的缺点 52 1 指针 52 1.1 指针 52 1.1.1 指针的概念 52 1.1.2 指针变量的定义 52 1.1.3 &取地址运算符 52 1.1.4 无类型指针 52 1.1.5 NULL 53 1.1.6 空指针与野指针 53 1.1.7 指针的兼容性 53 1.1.8 指向常量的指针与指针常量 54 1.1.9 指针与数组的关系 54 1.1.10 指针运算 54 1.1.11 通过指针使用数组元素 55 1.1.12 指针数组 55 1.1.13 指向指针的指针(二级指针) 55 1.1.14 指向二维数组的指针 57 1.1.15 指针变量做为函数的参数 57 1.1.16 一维数组名作为函数参数 57 1.1.17 二维数组名作为函数参数 58 1.1.18 const关键字保护数组内容 58 1.1.19 指针做为函数的返回值 58 1.1.20 指向函数的指针 59 1.1.21 把指向函数的指针做为函数的参数 60 1.1.22 memset,memcpy,memmove函数 61 1.1.23 指针小结 63 2 字符指针与字符串 64 2.1 指针和字符串 64 2.2 通过指针访问字符串数组 64 2.3 函数的参数为CHAR * 64 2.4 指针数组做为MAIN函数的形参 65 3 内存管理 65 3.1 作用域 65 3.1.1 auto自动变量 65 3.1.2 register寄存器变量 65 3.1.3 代码块作用域的静态变量 66 3.1.4 代码块作用域外的静态变量 66 3.1.5 全局变量 66 3.1.6 外部变量与extern关键字 66 3.1.7 全局函数和静态函数 66 3.2 内存四区 66 3.2.1 代码区 67 3.2.2 静态区 67 3.2.3 栈区 67 3.2.4 栈溢出 68 3.2.5 堆区 68 3.3 堆的分配和释放 70 3.3.1 malloc 70 3.3.2 free 70 3.3.3 calloc: 70 3.3.4 realloc 71 4 结构体,联合体,枚举与TYPEDEF 71 4.1 结构体 71 4.1.1 定义结构体struct和初始化 71 4.1.2 访问结构体成员 71 4.1.3 结构体的内存对齐模式 72 4.1.4 指定结构体元素的位字段 72 4.1.5 结构数组 72 4.1.6 嵌套结构 73 4.1.7 结构体的赋值 73 4.1.8 指向结构体的指针 73 4.1.9 指向结构体数组的指针 73 4.1.10 结构中的数组成员和指针成员 73 4.1.11 在堆中创建的结构体 74 4.1.12 将结构作为函数参数 74 4.1.13 结构,还是指向结构的指针 74 4.2 联合体 75 4.3 枚举类型 75 4.3.1 枚举定义 75 4.3.2 默认值 76 4.4 TYPEDEF 76 4.5 通过TYPEDEF定义函数指针 76 5 文件操作 77 5.1 FOPEN 77 5.2 二进制和文本模式的区别 77 5.3 FCLOSE 78 5.4 GETC和PUTC函数 78 5.5 EOF与FEOF函数文件结尾 78 5.6 FPRINTF,FSCANF,FGETS,FPUTS函数 78 5.7 STAT函数 78 5.8 FREAD和FWRITE函数 79 5.9 FREAD与FEOF 79 5.10 通过FWRITE将结构保存到二进制文件中 79 5.11 FSEEK函数 80 5.12 FTELL函数 80 5.13 FFLUSH函数 80 5.14 REMOVE函数 81 5.15 RENAME函数 81 6 基础数据结构与算法 82 6.1 什么是数据结构 82 6.2 什么是算法 82 6.3 排序 83 6.3.1 冒泡排序 83 6.3.2 选择排序 83 6.4 查找 83 6.4.1 顺序查找 83 6.4.2 二分查找 83 6.5 链表 84 6.5.1 单向链表定义 84 6.5.2 单向链表数据结构定义 85 6.5.3 单向链表的实现 85

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧