小编het*_*fan的帖子

简单分配中的分段错误(核心转储)

我正在使用c并在freeBSD系统上编写此代码.

///// defines /////

#define CPUCORES                2
#define THREADAMOUNT            CPUCORES - 1
#define nullptr                 NULL

///// typedefs /////

typedef enum BOOL_e
{
    e_FALSE = 0,
    e_TRUE = 1
}BOOL_t;

typedef struct NewClient_s
{
int sdNewClient;
struct sockaddr sdinClientIp;
socklen_t sdlenIPsize;
}NewClient_t;

typedef struct ClientThreadArg_s
{
    int iInternID;
    NewClient_t sIncommingClient;
    BOOL_t bHasKillSig;
    BOOL_t bIsShutdown;
}ClientThreadArg_t;

//------------------------------------ imagin this is the main

    ClientThreadArg_t *spListOfArguments;
    size_t sizeIndexI;
    spListOfArguments = (ClientThreadArg_t *) malloc (THREADAMOUNT * sizeof     (ClientThreadArg_t));   



for (sizeIndexI = 0; sizeIndexI < THREADAMOUNT; …
Run Code Online (Sandbox Code Playgroud)

c malloc freebsd typedef

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

在多个函数中使用时,va_list的意外行为

在我的项目中,我创建的C类层次结构与ansi C中的Axel-Tobias Schreiner面向对象编程类似,但不一样,请参阅https://www.cs.rit.edu/~ats/books/ooc. pdf.例如.

我正在初始化我的对象与Axel有点不同.当我在多个初始化函数之间传递va_list对象时,我遇到了麻烦.假设我有一个对象2,它来自对象.然后在初始化两个对象时,我需要首先初始化一个部分,然后初始化两个部分.所以我在调用与初始化参数的公共初始化函数1个初始化函数一个的一部分2个对象和论据,只有初始化2个扩展部分一个对象.

我正在创建的库非常大,但我从中提取了一个展示相同问题的迷你项目:

的CMakeLists.txt:

cmake_minimum_required(VERSION 3.5)

project (var_args
        VERSION     "0.0"
        LANGUAGES   C CXX
        )

set(HEADERS "init.h")
set(SOURCES init.c program.c)

add_executable(program ${SOURCES} ${HEADERS})

if (NOT MSVC)
    target_compile_options(program PRIVATE -W -Wall -Wextra -pedantic)
else()
    target_compile_options(program PRIVATE /W4)
endif()
Run Code Online (Sandbox Code Playgroud)

init.h里:

typedef struct _one {
    int a;
    const char* msg;
} one_t;

/* this struct "derives" from struct _one */
typedef …
Run Code Online (Sandbox Code Playgroud)

c c++

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

除和乘std :: chrono :: durations

我正在使用std :: chrono :: duration计算时间随时间的一些变化,以便获得派生和梯形积分的近似值。

我想在一段时间内执行一些常见的操作,但不幸的是未能这样做,可能是因为我对计时的理解不正确。

using std::chrono::milliseconds;
using namespace std;

milliseconds eightms = milliseconds(8);
milliseconds fourms = milliseconds(4);
milliseconds twoms = milliseconds(eightms / fourms); //<-- why do I need this cast?
milliseconds twoms = eightms / fourms;

cout << "twoms = " << twoms.count() << " ms" << endl;
Run Code Online (Sandbox Code Playgroud)

预期的输出将是

twoms = 2毫秒

如果我不使用上面的转换,则会为我得到一个隐秘的编译器错误,当我再次将除法结果转换为毫秒时,它将按预期工作。

dur_div_mult.cpp: In function ‘int main()’:
dur_div_mult.cpp:13:11: error: no match for ‘operator=’ (operand types are ‘std::chrono::milliseconds {aka std::chrono::duration<long int, std::ratio<1l, 1000l> >}’ and ‘std::__success_type<long int>::type {aka …
Run Code Online (Sandbox Code Playgroud)

c++ c++11 c++-chrono

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

当函数未被前向声明时,GCC在编译阶段不会抛出错误

好,

我的目录中有3个文件.

main.c中

#include <stdio.h>
int main(int a, int b, int c)
{
    aprint();
    bprint();
}
Run Code Online (Sandbox Code Playgroud)

AC

#include <stdio.h>

void aprint()
{
    printf("hey This is a.c");
}
Run Code Online (Sandbox Code Playgroud)

公元前

#include <stdio.h>
void bprint()
{
   printf("This is b.c");
}
Run Code Online (Sandbox Code Playgroud)

我还没有创建任何头文件.我刚刚编译使用"gcc main.c ac bc"我没有得到任何错误.我想知道发生了什么?gcc是否只是假设链接阶段的一切都没问题,为什么gcc在编译期间没有抛出错误?

c unix gcc

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

验证C++的乘法值?

我必须做一个测试,其中一个数字必须是500 的倍数,

      do{
            cout << "Input Price[price>0|price multiple of 500]: ";
            cin >> price;
            cin.sync(); cin.clear();
        } while (price<1 || price>5000);
Run Code Online (Sandbox Code Playgroud)

代码仍然不完整,我只需要添加以下验证.我该怎么办?

还有什么是这种验证的合适术语?我很难确定标题.

c++ validation

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

混淆了范围和堆内存生命周期

好吧所以我试图掌握堆的概念及其范围

#include <iostream>
int main(){

    {
        int* x = new int(10);
    }

    std::cout<<*x;//ERROR Use of undeclared identifier 'x'
}
Run Code Online (Sandbox Code Playgroud)

但是因为我将它分配给堆,为什么它不再存在于块的范围之后呢?

c++

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

标签 统计

c++ ×4

c ×3

c++-chrono ×1

c++11 ×1

freebsd ×1

gcc ×1

malloc ×1

typedef ×1

unix ×1

validation ×1