我在C中有一个LDA代码,我正在尝试调试,而且我已经敲了很长时间了.
lda_model *model = NULL;
model = malloc(sizeof(lda_model));
model = quiet_new_lda_model(corpus->num_terms, NTOPICS);
printf ("%f\n", model->alpha); // Segfaults here
Run Code Online (Sandbox Code Playgroud)
如果我们看一下模型创建函数
lda_model* quiet_new_lda_model(int num_terms, int num_topics) {
int i;
lda_model* model;
model = malloc(sizeof(lda_model));
model->num_topics = num_topics;
model->num_terms = num_terms;
model->alpha = 1.0;
printf ("%f\n", model->alpha); // Prints 1.0
model->log_prob_w = malloc(sizeof(double*)*num_topics);
for (i = 0; i < num_topics; i++)
{
model->log_prob_w[i] = malloc(sizeof(double)*num_terms);
memset(model->log_prob_w[i],0,sizeof(double)*num_terms);
}
printf ("%f\n", model->alpha); // Prints 1.0
return(model);
}
Run Code Online (Sandbox Code Playgroud)
quiet_new_lda_model函数和原始调用者之间的事务中可能存在什么问题?
谢谢!
我有这段代码可以将素数打印到屏幕上.
例如,printPrimes(500000)将填满屏幕,所有素数最多为500000(即7368787).
问题是,传递600000或1000000这样的较大数字会破坏程序.
有任何想法吗?提前致谢.
typedef enum {false, true} bool;
bool isPrime(long number, long primes[], long n) {
int divisor, index;
for (index = 0; index < n; ++index) {
divisor = primes[index];
if (divisor * divisor > number) {
return true;
} else if (number % divisor == 0) {
return false;
}
}
return 0;
}
void printPrimes(long n) {
long primes[n];
long odd, index;
primes[0] = 2;
odd = 1;
index = 1;
while (index < n) { …Run Code Online (Sandbox Code Playgroud) 我一直在使用一个程序,使用GCC 4.4.1(ISO C99)编译并且今天注意到了这个怪癖.虽然它在目标上运行没有任何问题,但编译器也不会使用-Wall发出任何警告.
void mutex_init(mutex_t *mutex)
{
if(unlikely(mutex->magic == MUTX_MAGIC_CHAR))
return;
mutex->owner = NULL;
mutex->prior = NULL;
mutex->magic = MUTX_MAGIC_CHAR;
thread_queue_init(&mutex->queue);
}
Run Code Online (Sandbox Code Playgroud)
不应该是这样的
*mutex->owner = NULL;
Run Code Online (Sandbox Code Playgroud) struct s1 { int a; int b; };
struct s2 { int a; int b; };
struct s2 test(void) {
struct s1 s = { 1, 2 };
return s; // incompatible types
}
Run Code Online (Sandbox Code Playgroud)
在上面的代码中,我可以返回s而不创建一个新struct s2变量并用它s的值填充它吗?保证struct s1始终与之相同struct s2.
因此printf()是一个函数,如果发生错误,它返回成功或负值时写入的字符数,看这个例子,预期的输出为零.
#include <stdio.h>
int main(void)
{
printf("%d");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
现在当我添加更多这些%d: http ://ideone.com/brw5vG输出更改为: 0 134513819 -1216430092 134513808
我无法弄清楚随机垃圾值是什么?输出中也有负值,负值可以证明错误是正确的,所以任何人都可以确切地指出这里的错误是什么吗?请简明扼要,具体.谢谢.
我正在通过88 C程序工作,在将近二十五年之后重新学习C(由于自1990年以来至少对该语言本身进行了两次主要版本修订这一事实很复杂,我确信Turbo-C我当时用的并不完全兼容C89).我不记得我所用的类对二维数组做了什么后果,并将它们传递给函数,期望我需要在不知道编译时的维数的情况下处理数据,这很好.我的头爆炸了.
我正在使用gcc(目前在Ubuntu 14.04的标准存储库中找到的版本),我收集它应该支持C99或C2011标准下的可变长度数组声明,但是我试图用来制作函数的声明识别数组而不必知道编译时的大小是否有关于"冲突类型"的错误.我正在编写警告设置为max,使用一个小的Python程序来节省每次我需要编译时输入一个长命令行(结果命令是gcc -std=c99 -pedantic -Wall -Wextra -o target target.c -lm).以下是原型,来电线和功能定义(代码中充满了警告,导致不良做法,我从书中复制了它;我现在不关心那些,我对学习感兴趣这样做的理智方式,所以我不必使用那些丑陋,令人困惑的方法).
#include <stdio.h>
#include <stdlib.h>
/* ANSI prototypes */
void s2 (int, int, int*);
int main (void);
int main (void)
{
int one[5]={0,1,2,3,4};
int two[3][4] =
{
{0,1,2,3},
{10,11,12,13},
{100,101,102,103}
};
/* try to call s2, it won't work */
printf("\ncalling s2 \n");
s2(1, 5, one);
s2(3, 4, two); /* will be compiler warning */
}
void s2(int rows, int cols, int *x[rows][cols]) …Run Code Online (Sandbox Code Playgroud) 请考虑以下代码:
const char* text = "hi";
printf("%s\n",text);
printf("%p\n", &text);
printf("%p\n", text);
Run Code Online (Sandbox Code Playgroud)
每个人从哪里printf获取它打印的价值?
有什么区别?
我写了一个简单的C程序来测试_Generic关键字的可用性.
int main() {
int _Generic;
}
Run Code Online (Sandbox Code Playgroud)
我在Ubuntu上用gcc-5.3.1和clang-3.8.0编译器运行程序.
显然,当使用最新的c11标准编译时,该程序会产生错误.
但是,当使用-std = c90和-std = c99标志进行编译时,它也会生成错误.然而,_Generic关键字仅在c11标准中引入.
这是-std = flags的行为不同吗?有没有办法测试纯c90和c99标准?
编辑:
我确实使用其他标识符运行相同的程序,这些标识符不是c11标准的关键字.喜欢:
int _Hello;
int _Gener;
Run Code Online (Sandbox Code Playgroud)
他们成功编译没有任何错误或警告.这可能是因为7.1.3,它说
如果程序在保留它的上下文中声明或定义标识符(除了7.1.4允许的标识符),或者将保留标识符定义为宏名称,则行为未定义
正如@Art和@Lundin所说.
根据C99,嵌套函数调用是否有限制?
例:
result = fn1( fn2( fn3( ... fnN(parN1, parN2) ... ), par2), par1);
Run Code Online (Sandbox Code Playgroud)
注意:这段代码绝对不是一个好的做法,因为很难管理; 但是,此代码是从模型自动生成的,因此可管理性问题不适用.
我想使用该<complex.h>库来加速某些计算,其中可能包括多达20个复杂元素和运算(+-* /)。但是,我的同事声明了一个结构:
struct complex {
double a;
double b;
};
Run Code Online (Sandbox Code Playgroud)
因此,complex由于与上述结构冲突,无法使用来自库的宏。
事实是,由于对当前项目的巨大影响,我无法更改结构的名称。我尝试使用_Complex宏来代替,并取出#include <complex.h>头,它的工作,但_Imaginary_I,_Complex_I还是I不起作用。
#include <stdio.h> /* Standard Library of Input and Output */
// #include <complex.h> /* I want to remove this */
int main() {
struct complex
{
double a;
double b;
};
double _Complex z1 = 1.0;// + 3.0 * _Complex_I;
double _Complex z2 = 1.0;// - 4.0 * _Complex_I;
// none of these …Run Code Online (Sandbox Code Playgroud)