小编Bil*_*nch的帖子

const char的多重定义*

我得到了全局的上述消息链接器错误

const char* HOST_NAME = "127.0.0.1";
Run Code Online (Sandbox Code Playgroud)

我不认为我已经编译了两次文件,但这里是我对文件的定义.

main.cpp中

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include "connection.hpp"
Run Code Online (Sandbox Code Playgroud)

connection.cpp

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <sys/socket.h>
#include <sys/types.h>
#include <netdb.h>
#include <arpa/inet.h>
#include "connection.hpp"
Run Code Online (Sandbox Code Playgroud)

connection.hpp

#ifndef __connection__
#define __connection__
#include <unistd.h>
#include <netinet/in.h>

const int BUFFSIZE = sysconf(_SC_PAGESIZE);             //Define page size
const char* HOST_NAME = "127.0.0.1";                    //Local host
//Definition of a class
#endif
Run Code Online (Sandbox Code Playgroud)

有帮助吗?

c++ linker-errors multiple-definition-error

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

在linux内核中使用workQueue传递/共享数据

请任何人都可以帮助我理解“将数据(共享或私有)”传递给 workQueue 吗?

1:声明一个回调/工作处理程序

static void sample_work_fn(struct work_struct *Wq)
{
...........
...........
}
Run Code Online (Sandbox Code Playgroud)

2:填写一个work_struct结构(静态)

static DECLARE_WORK(sample_work, sample_work_fn);
Run Code Online (Sandbox Code Playgroud)

3:安排工作队列

static void samp_sysrq(int arg)
{
        schedule_work(sample_work);
}
Run Code Online (Sandbox Code Playgroud)

在这里,如果我需要使用我的工作队列传递/共享数据,这怎么可能?

linux-device-driver linux-kernel embedded-linux

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

用C语言递归声明函数

从adream307得到一个问题,我不知道,你呢?

我想声明一个这样的函数:(我们将这种类型的函数命名为F)

  1. F的返回类型是"无效"
  2. F的参数是一个函数指针,该指针指向一个类型与F相同的函数

我可以声明这样的函数吗?

c recursion compilation function declare

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

为什么这项任务会破坏我的计划?

我正在学习C并且我不确定如何表达这一点,但为什么在以下代码中取消注释第11行会破坏这个程序?

#include <stdio.h>

int main(int argc, char *argv[])
{
    printf("argc: %d\n", argc);

    char *states[] = {};
    int i = 0;
    while(i < argc) {
        printf("arg %d: %s\n", i, argv[i]);
        //states[i] = "test";  
        i++;
    }


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

当我取消注释这一行并运行程序时,我得到了这个:

greggery@Lubu:~/code$ ./myprog aaa bbb ccc
argc: 4
arg 0: ./lc
arg 1: aaa
Run Code Online (Sandbox Code Playgroud)

为什么states[i] = "test";打破while循环?当我发表评论时,我看到所有的论点都打印出来.

c

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

如何为架构x86_64错误修复g ++未定义符号?

首先,我已经在Stackoverflow上阅读了有关此问题的相关内容,但我仍然无法解决它.我尽可能简化了我的代码.

我只有一个带.h和.cpp文件的自定义类,但是在尝试从main.cpp创建此类的实例时出错.

main.cpp中

#include "Customer.h"
using namespace std;

int main() {
    Customer a("string");

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

Customer.h

using namespace std;

class Customer {
public:
    Customer(string input);
};
Run Code Online (Sandbox Code Playgroud)

为customer.cpp

#include "Customer.h"
using namespace std;

Customer::Customer(string input) {
}
Run Code Online (Sandbox Code Playgroud)

我得到的错误信息如下?

  gcc *.cpp -o k


  Undefined symbols for architecture x86_64:
    "std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >::__init(char const*, unsigned long)", referenced from:
        _main in main-40340f.o
    "std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >::basic_string(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)", referenced from:
        _main in main-40340f.o
    "std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >::~basic_string()", referenced from:
        _main in …
Run Code Online (Sandbox Code Playgroud)

c++ string macos gcc

3
推荐指数
2
解决办法
8091
查看次数

realloc:下一个大小和malloc无效:内存损坏(快)

我正在做一个K和RC编程书的乐趣练习.该程序用于从用户输入的一组行中查找最长行,然后打印它.

输入:

This is a test

This is another long test

this is another long testthis is another long test
Run Code Online (Sandbox Code Playgroud)

观察:

它对前两个输入运行正常,但对于较大的字符串(第三个输入)则失败

错误:

Error in `./longest': realloc(): invalid next size: 0x000000000246e010 ***
Error in `./longest': malloc(): memory corruption (fast): 0x000000000246e030 ***
Run Code Online (Sandbox Code Playgroud)

我的努力:

我一直试图调试这个2天(橡皮鸭调试),但逻辑似乎很好.GDB指向_getline函数中的realloc调用,并在顶部显示glibc.so内存分配调用的大量回溯.

这是我写的(部分,部分内容直接取自本书): -

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

int MAXLINE =  10;
int INCREMENT = 10;

char* line = NULL, *longest = NULL;

void _memcleanup(){
    free(line);
    free(longest);      
}

void copy(char longest[], char line[]){
    int i=0;
    char* temp = realloc(longest,(MAXLINE)*sizeof(char)); …
Run Code Online (Sandbox Code Playgroud)

c realloc

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

当我们使用新的struct初始化格式时,未列出的成员是否有未指定的值?

所以我需要准备一个大型结构:

struct Config {
    int a;
    int b;
    int c;
    struct { int x; int y; } d[40];
};
Run Code Online (Sandbox Code Playgroud)

我想这样填写:

Config config = {
    .a = 3;
    .b = 4;
    .d[0] = {10, 12};
    .d[1] = {14, 16};
};
Run Code Online (Sandbox Code Playgroud)

在此之后,确实的价值config.c,并config.d[2]有一个未确定的值?还是零?

或者,我是否需要这样做:

Config config;
memset(&config, 0, sizeof(config));
config.a = 3;
config.b = 4;
config.d[0].x = 10;
config.d[0].y = 12;
...
Run Code Online (Sandbox Code Playgroud)

c

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

在python中,为什么只有在调用另一个模块的方法时才会执行类体中的代码?

与Java不同,在python中将模块导入另一个模块并调用模块的方法时,为什么主体中的代码会被执行而不是只执行方法中的代码?

示例(取自另一个SO问题):

提交one.py

def func():
    print("func() in one.py")

print("top-level in one.py")

if __name__ == "__main__":
    print("one.py is being run directly")
else:
    print("one.py is being imported into another module")
Run Code Online (Sandbox Code Playgroud)

文件two.py

import one

print("top-level in two.py")
one.func()

if __name__ == "__main__":
    print("two.py is being run directly")
else:
    print("two.py is being imported into another module")
Run Code Online (Sandbox Code Playgroud)

当我运行这个:

python two.py
Run Code Online (Sandbox Code Playgroud)

我明白了:

top-level in one.py
one.py is being imported into another module
top-level in two.py
func() in one.py
two.py is being run directly
Run Code Online (Sandbox Code Playgroud)

如果我只想要执行的func()方法, …

python

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

在编写可加载内核模块时需要使用类似module_init和module_exit的MACROS

什么是使用宏象的需要module_init,并module_exit在写加载内核模块?另外,为什么我们使用像__init或等的MACRO __exit.即使我们可以在不使用它们的情况下完成工作.

  1. 没有MACROS

    /*
    Without using MACROS
    Author: Sricharan Chiruvolu
    Date: 14 Dec 2014
    */
    #include <linux/module.h>
    #include <linux/kernel.h>
    int init_module(void){
        printk(KERN_ALERT "This is our first program.");
        return 0;
    }
    void cleanup_module(void){
        printk(KERN_ALERT "End of our first program.");
    }
    
    Run Code Online (Sandbox Code Playgroud)
  2. 有了MACRO

    /* 
    Edited first.c; Added macros module_init and module_exit
    Author: Sricharan Chiruvolu
    Date: 14 Dec 2014
    */
    #include <linux/module.h>
    #include <linux/kernel.h>
    #include <linux/init.h>
    
    static int __init first_init(void)
    {
        printk(KERN_ALERT "This is our first program."); …
    Run Code Online (Sandbox Code Playgroud)

macros kernel kernel-module linux-kernel

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

n个皇后的快速启发式算法(n> 1000)

我写了两个程序:

  1. 在国际象棋棋盘上放置n个皇后,没有任何回溯算法的威胁.但这对于大n来说非常沉重.最后你可以为100个皇后运行.
  2. 在国际象棋棋盘上放置n个皇后,没有任何威胁的爬山算法.这个算法比过去的解决方案更好,但是300个皇后需要2分钟,而且这个时间呈指数增长!

但我没有任何想法快速做到这一点!我想要算法更快地做到这一点.

我希望更快的方式尽快解决1000个皇后的问题.

这是我的登山代码:

// N queen - Reset Repair Hill Climbing.cpp
// open-mind.ir

#include "stdafx.h"
#include <vector>
#include <iostream>
#include <fstream>
#include <time.h>
#include <iomanip>


using namespace std;

//print solution in console
void printBoardinTerminal(int *board, int len)
{
    for (int i = 0; i < len; i++)
    {
        for (int j = 0; j < len; j++)
        {
            if (j == board[i])
            {
                cout << 1 << " ";
            }
            else
            {
                cout << 0 << " …
Run Code Online (Sandbox Code Playgroud)

c++ algorithm chess heuristics n-queens

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