小编ste*_*ave的帖子

在使用malloc分配新内存后,是否必须调用memset

#include "stdlib.h"
#include "stdio.h"
#include "string.h"
int main(int argc, char* argv[])
{
    int *test = malloc(15 * sizeof(int));
    for(int i = 0;i < 15 ;i  ++ )
        printf("test is %i\n",test[i]);

    memset(test,0,sizeof(int) * 15);

    for(int i = 0 ; i < 15; i ++ )
        printf("test after memset is %i\n",test[i]);

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

我得到的输出非常奇怪:

    test is 1142126264
    test is 32526
    ...
    test is 1701409394
    test is 1869348978
    test is 1694498930
    test after memset is 0
    test after memset is 0
    test …
Run Code Online (Sandbox Code Playgroud)

c malloc memset

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

ENUM类型和c和c ++中的用法

如果我这样写:

typedef enum {
  foo_1,
  foo_2
}foo ;
Run Code Online (Sandbox Code Playgroud)

我发现我可以使用

int footype = foo::foo_1 在c ++中

我可以直接使用

int footype = foo_1 在c中,

那么是否有一种方法可以编写在c ++和c中都能运行的相同代码?代码在一个只有一个结构的头文件中.

c c++ enums

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

如何使用可以在c中读取的python编写二进制结构?

说这样的结构:

typedef struct testVertex_s {
       char *vert_name; //for test only...
       float x;
       float y;
       float z;
}testvertex_t;
Run Code Online (Sandbox Code Playgroud)

如何使用python将其写入二进制文件?我想用c中的fread读它;

c python

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

在char指针中搜索'\n'使用c

我试图循环一个char*str使用它来找出多少行:

char *str = "test1\ntest2\ntest3";

int lines = 0;

for(int i = 0 ; i < ?? ; i ++ )
{
    if(str[i] == '\n') {
        lines++;
    }
}
Run Code Online (Sandbox Code Playgroud)

我不知道该放什么?,问题是:

1.我的意思是我需要使用strlen(str)+ 1吗?

2.当str为"test1 \ntest2 \ntest3 \n"时,代码是否仍然计算正确的行?

我顺便使用gcc,谢谢

c gcc

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

是否可以使用G_TRIANGLE_FAN或GL_TRIANGLE_STRIP绘制两个单独的四边形?

可能吗?如果我有2个四边形的8个顶点和单独的位置(基于四边形),是否可以通过一次调用glDrawElement来绘制这个四边形中的两个?

c++ opengl

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

标签 统计

c ×4

c++ ×2

enums ×1

gcc ×1

malloc ×1

memset ×1

opengl ×1

python ×1