冒泡排序中的分段错误

试图编写一个冒泡排序算法,对任何数据类型进行排序,其工作方式类似于C中stdlib中的qsort。 这是我编写的代码,编译它给我一个“分段错误”错误尝试使用-g编译gdb调试,这让错误更多 as: In function `testcmp’: (.text+0x21a): multiple definition of `testcmp’ /tmp/cc9ULHuO.o:new.c:(.text+0x12d): first defined here as: In function `_fini’: (.fini+0x0): multiple definition of `_fini’ /usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crti.o(.debug_info): relocation 0 has invalid symbol index 7 /usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crti.o(.debug_info): relocation 1 has invalid symbol index 8 /usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crti.o(.debug_info): relocation 2 has invalid symbol index 9 /usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crti.o(.debug_ranges): relocation 0 […]

有人在C中有一个条件语句的例子吗?

像这样的东西,我想看到完整的语法。 伪代码: var = user_input if var > 5: output = ‘var > 5’ else: output = ‘var < 5'

无法编译读取智能卡的ac应用程序

我正在尝试编译一个示例c应用程序,该应用程序正在使用pkcs#11查找令牌上的所有私钥,并打印其标签和ID,但会出现以下错误 /tmp/ccAqQ7UI.o: In function initialize’: pkcs11_example1.c:(.text+0x8e5): undefined reference to C_Initialize’ /tmp/ccAqQ7UI.o: In function `get_slot’: 这个例子来自这里 使用以下命令进行编译; `gcc pkcs11_example1.c -o slots -L /usr/lib/opensc-pkcs11.so` 我不确定在-L之后我应该链接哪个库。 任何人都可以指导如何编译它,是否有一些库需要链接。

计算c中的数字因子时ac程序的时间限制

我正在解决关于阶乘计算的问题,挑战如下! You are asked to calculate factorials of some small positive integers. Input An integer t, 1<=t<=100, denoting the number of testcases, followed by t lines, each containing a single integer n, 1<=n<=100. Output For each integer n given at input, display a line with the value of n! 我的代码给了我正确的解决方案,但超过了时间限制,即2秒: 代码如下: #include #include #include void […]

在C中获取两个数组的差异的有效方法是什么?

样本输入 Array_1 [] = { 10, 20, 30, 40, 50 }; Array_2 [] = { 30, 40, 50, 60, 70 }; 样本输出 Array_1_Extras [] = { 10, 20 }; Array_2_Extras [] = { 60, 70 }; 描述 输入数组未排序。 输入数组长度 – 10K。 输入数组值范围为0 – 4095。 不应修改输入数组顺序和值。 输出数组可以按任何顺序排列。 输出数组中不需要重复项。 我需要一些时间有效的逻辑来实现这个程序。 提前致谢。

将字符串数组传递给C中的函数

我刚刚学习C,有人可以解释为什么下面的代码在打印数组的第一个元素后会产生分段错误吗? 工作代码会是什么样的? #include #include #include #define ELEMENTS 8 void make(char ***array) { *array = malloc(ELEMENTS * sizeof(char *)); (*array)[0] = “test0”; (*array)[1] = “test1”; (*array)[2] = “test2”; (*array)[3] = “test3”; (*array)[4] = “test4”; (*array)[5] = “test5”; (*array)[6] = “test6”; (*array)[7] = “test7”; (*array)[8] = “test8”; } int main(int argc, char **argv) { char **array; make(&array); […]

将图像放置在固定布局GTK2中

我有一个试图执行以下操作的应用程序: 创建一个GTK2顶级主窗口 在主窗口中添加固定框架,以便对窗口小部件进行绝对定位 创建一个GtkImages矩阵,用于显示动画GIFS和静态JPEGS 在启动时,从列表中随机选取的静态JPEGS将填充矩阵 当事件发生时,矩阵将填充动画GIFS 当动画结束时,可能会在矩阵中再次显示不同的JPEGS 仅当两个或多个随机选择的JPEGS放置在矩阵的一行中时,才会发生运行时错误。 以下是此类运行时错误的示例: (wrong:3909): Gtk-WARNING **: Can’t set a parent on widget which has a parent 如果该行的每个图像都是唯一的,则不会发生运行时错误。 代码片段和运行时输出如下: /* * Compile me with: * gcc -Wall -o wrong wrong.c $(pkg-config –cflags –libs gtk+-2.0 gmodule-2.0) */ /* header includes */ /**** prototypes ****/ /********************/ typedef struct { unsigned int pixel_width, […]

strdup()导致内存泄漏?

我已经实现了一个返回字符串的函数。 它将整数作为参数( age ),并返回格式化的字符串。 一切都运作良好,除了我有一些疯狂的内存泄漏。 我知道strdup()是导致这种情况的原因,但我试图研究一些修复无济于事。 我的代码是: const char * returnName(int age) { char string[30]; sprintf( string, “You are %d years old”, age); return strdup(string); } Valgrind的输出是: ==15414== LEAK SUMMARY: ==15414== definitely lost: 6,192 bytes in 516 blocks ==15414== indirectly lost: 0 bytes in 0 blocks ==15414== possibly lost: 0 bytes in 0 blocks ==15414== […]

将字节数组转换为整数而不循环的方法?

(我已经看到了这些问题( 在C 中将字节 转换为Int / uint并将字节数组转换为Int24 )但它们看起来比我想做的更复杂,所以我想我会问。) 我在Arduino / Wiring中做了一些LED矩阵编程。 由于这里可能不相关的原因,我有一个表示LED行的“位状态”的字节数组,我用它来缓冲其他操作的结果。 要实际设置LED(我使用的是Maxim 7219芯片),我需要从字节数组中导出一个整数。 使用Arduino / Wiring bitWrite方法,我的下面的一个简单的例子程序工作,但我想知道是否有一个比循环更快的C方法。 byte dog[8] = {0,0,1,1,1,1,1,0}; byte cat; for (int i = 0; i < 8; i++){ bitWrite(cat, i, dog[7-i]); }

从OpenMP到MPI

我只是想知道如何将以下openMP程序转换为MPI程序 #include #define CHUNKSIZE 100 #define N 1000 int main (int argc, char *argv[]) { int i, chunk; float a[N], b[N], c[N]; /* Some initializations */ for (i=0; i < N; i++) a[i] = b[i] = i * 1.0; chunk = CHUNKSIZE; #pragma omp parallel shared(a,b,c,chunk) private(i) { #pragma omp for schedule(dynamic,chunk) nowait for (i=0; […]