小编mac*_*e_1的帖子

为什么realloc在这个while循环中不起作用?

我很想知道为什么realloc()在我的循环中不起作用.我做了一个grep函数,我在一个大文本文件上测试,突然程序崩溃告诉我"堆的腐败"所以我决定分解它并尝试它规模较小,但问题仍然存在.可以解释一下有什么问题吗?

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

void grep(const char *Pattern,FILE *file);

int main(void)
{
    FILE *file;
    if(fopen_s(&file,"file.txt","r"))
        return 1;
    grep("word",file);
    fclose(file);
    return 0;
}

void grep(const char *Pattern,FILE *file)
{
    size_t size = 5*sizeof(char);
    char *_Buf = (char*)malloc(size);
    int n = 0, c;
    while(c=getc(file))
    {
        _Buf[n++] = c;
        if(c == '\n' || c == EOF)
        {
            _Buf[n] = '\0';
            if(strstr(_Buf,Pattern))
                printf("%s",_Buf);
            if(c == EOF)
                break;
            n = 0;
        }
        if(n == size)
        { …
Run Code Online (Sandbox Code Playgroud)

c pointers loops realloc dynamic-memory-allocation

2
推荐指数
1
解决办法
762
查看次数

如何使用静态内联成员指针和静态成员函数正确的实例?

我有一个静态回调成员函数,它通过静态内联指针调用非静态窗口过程成员函数。问题是,它最终调用了最后一个实例。这是一个演示问题核心的例子:

#include <iostream>

struct A {
    static inline A* pThis;
    int i;     

    A(int i) : i(i) {
        pThis = this;
    }

    static void foo() {
        std::cout << pThis->i << std::endl;
    }
};

int main() {
    A a1(1); 
    A a2(2); 

    a1.foo(); // prints 2 instead of 1
    a2.foo(); // prints 2 as expected

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

请注意,我无法更改回调函数的签名,因此建议让 foo 接受参数是徒劳的。我该如何解决这个问题?

c++ class callback static-members c++20

2
推荐指数
1
解决办法
109
查看次数

为什么负数大于正数?

我使我的冒泡排序程序通用.我继续测试它,它运行良好,直到我在阵列中放置一个负数,我很惊讶它被推到最后,使它比正数更大.

显然memcmp是原因,那么为什么memcmp()负数大于正数呢?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void bubblesortA(void *_Buf, size_t bufSize, size_t bytes);

int main(void)
{
    size_t n, bufsize = 5;
    int buf[5] = { 5, 1, 2, -1, 10 };
    bubblesortA(buf, 5, sizeof(int));

    for (n = 0; n < bufsize; n++)
    {
        printf("%d ", buf[n]);
    }
    putchar('\n');

    char str[] = "bzqacd";
    size_t len = strlen(str);
    bubblesortA(str, len, sizeof(char));

    for (n = 0; n < len; n++)
    {
        printf("%c ", str[n]);
    }
    putchar('\n');
    return …
Run Code Online (Sandbox Code Playgroud)

c sorting memcmp

1
推荐指数
2
解决办法
1116
查看次数

为二维整数数组分配内存,但它应该是连续的

我想将内存分配给类型的二维数组,int但内存应该是连续的.

只需拨打一个电话就可以释放它free( ptr ).我不需要为每个内存块免费调用.

c arrays malloc pointers multidimensional-array

1
推荐指数
1
解决办法
75
查看次数

用C语言格式化浮点值

我需要打印.50.5使用printfC语言的语句.可以使用以下代码轻松完成.

printf("%03f", myfloatvalue);
Run Code Online (Sandbox Code Playgroud)

但情况myfloatvalue是动态的.所以,我不知道它是否是一个3字符数,也可能是.5.56.567.在上述情况下,我需要打印的0.5,0.560.567.

c printf output-formatting

1
推荐指数
1
解决办法
375
查看次数

数组被"清零"的困惑

#include<stdio.h>
#include<stdlib.h>

static char mem[4];

int main()
{
    int* A = (int*)(mem);

    mem[0] = 0;
    mem[1] = 1;
    mem[2] = 2;
    mem[3] = 3;

    int i;
    A[0] = 5;

    for (i = 0; i<4; i++)
    {
        printf("%d\t", mem[i]);
    }

    printf("\n");
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我的问题是,为什么打印5 0 0 0而不是5 1 2 3?为什么阵列"消失了?"

c arrays

1
推荐指数
1
解决办法
108
查看次数

我的链表代码有什么错误?

这是我用ANSI C编写的代码.我经常遇到运行时错误:Segmentation Fault(SIGSEGV).请帮帮我.我是数据结构和C的新手.我无法检测到问题.

#include<stdio.h>
#include<stdlib.h>

typedef struct node
{
    int data;
    struct node *nxt;
}node;

node * create(int n);
void display(node *head);

int main()
{
    int n = 0;
    node *head = NULL;
    printf("Enter the number of nodes\n");
    scanf("%d", &n);
    head = create(n);
    display(head);
    return 0;
}

node * create(int n)
{
    int i;
    node *head = NULL;
    node *temp = NULL;
    node *p = NULL;
    for (i = 0; i < n; i++)
    {
        temp = (node *)malloc(sizeof(node));
        printf("\nEnter …
Run Code Online (Sandbox Code Playgroud)

c linked-list data-structures

1
推荐指数
1
解决办法
88
查看次数

任务是计算在圆圈中移动的两个对象之间的直线距离

在此输入图像描述

我们在距离为r1的直线上有两个点对象B和C,在点A处有r2个单位.在时间t = 0秒时,对象开始在圆形路径中移动,A在中心,角速度为v1和每秒v2度.

给定输入v1,v2,r1和r2,计算N秒后B和C之间的距离.我已经做到了,但它给出了错误的答案,任何人都可以建议我一个更好的解决方案.

#include"stdio.h"
#include"math.h"
int main()
{
    float v1,v2,r1,r2,t;
    scanf("%f%f%f%f%f", &v1, &r1, &v2, &r2, &t);

    int diff = v1 > v2 ? (v1 - v2) : (v2 - v1);
    int total_diff = diff * t;

    if(total_diff % 360 == 0)
        printf("%.2f", r2 - r1);
    else if(total_diff % 180 == 0)
        printf("%.2f", r1 + r2);
    else if(total_diff % 90 == 0)
        printf("%.2f", sqrt(pow(r2, 2) - pow(r1, 2)));
    else
        printf("%.2f", sqrt(pow(total_diff, 2) - pow(r2-r1, 2)));
}
Run Code Online (Sandbox Code Playgroud)

我接受了其他部分:https://math.stackexchange.com/questions/1727504/calculate-distance-between-two-points-on-concentric-circles

c

1
推荐指数
1
解决办法
449
查看次数

这是一个找到数字阶乘的程序.为什么我得到0作为答案?我应该使用哪种数据类型?

我对C比较陌生.我做了这个程序来找到任何数字的阶乘.在执行程序时,当我提供33作为输入时 - 我得到2147483648作为答案.如果我提供34,我得到0作为答案.

回答我的问题 - 为什么我得到0作为答案?我使用的数据类型的范围是0-4294967295.我得到0因为这超出了unsigned int的范围吗?如果我想获得一个大数字作为输出,我应该使用哪种数据类型?

使用的编译器 - GCC 8.2.1

这是代码 -

#include<stdio.h>
#include<stdlib.h>
int fact(unsigned int n)
{
        int result;
        if(n==0 || n==1)
                result=1;
        else
                result=n*fact(n-1);
        return result;
}
int main()
{
        unsigned int n,ans;
        printf("Enter n:");
        scanf("%u",&n);
        ans=fact(n);
        printf("Factorial of %u:%u",n,ans);
}
Run Code Online (Sandbox Code Playgroud)

c gcc

1
推荐指数
1
解决办法
100
查看次数

在这种情况下首选递归吗?

假设我们有一个字符串,我们想以相反的顺序打印它。在这种情况下,递归似乎是更快的选择,因为字符串被“遍历”一次,而通常的循环方法会“遍历”两次。

对于这类问题,有什么理由不喜欢递归?在大输入的情况下,递归是否会造成某种不利?

考虑以下代码:

#include <stdio.h>
#include <string.h>

void printReverse_1(const char *str)
{
    if (!*str)
        return;

    printReverse_1(str + 1);
    putchar(*str);
}

void printReverse_2(const char *str)
{
    const char *tmp = str + strlen(str) - 1;

    while (tmp > str)
    {
        putchar(*tmp--);
    }
    putchar(*tmp);
}

int main(void)
{
    printReverse_1("abc");
    putchar('\n');

    printReverse_2("abc");
    putchar('\n');

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c recursion performance

1
推荐指数
1
解决办法
126
查看次数