我是C的新手,试图找出C中的内存分配,我有点困惑
#include <stdio.h>
#include <stdlib.h>
typedef struct
{
int a;
} struct1_t;
int main()
{
funct1(); //init pointer
return 1;
}
int funct2(struct1_t *ptr2struct)
{
printf("print a is %d\n",ptr2struct->a);
//free(ptr2struct);
printf("value of ptr in funct2 is %p\n", ptr2struct);
return 1; //success
}
int funct1(){
struct1_t *ptr2struct = NULL;
ptr2struct = malloc(sizeof(*ptr2struct));
ptr2struct->a = 5;
printf("value of ptr before used is %p", ptr2struct);
if (funct2(ptr2struct) == 0) {
goto error;
}
free(ptr2struct);
printf("value of ptr in funct1 after freed is is …
Run Code Online (Sandbox Code Playgroud) 我在下面写了这个C代码,当我循环时,它返回一个随机数.如果执行myrand(),如何实现5个不同的随机值?
#include <stdio.h>
#include <stdlib.h>
int myrand() {
int ue_imsi;
int seed = time(NULL);
srand(seed);
ue_imsi = rand();
return ue_imsi;
}
int main()
{
int value = 0;
int i=0;
for (i=0; i< 5; i++)
{
value =myrand();
printf("value is %d\n", value);
}
}
Run Code Online (Sandbox Code Playgroud) 我是C++的新手,我无法解决下面的编译错误.
data_structure.h
#include <stdint.h>
#include <list>
namespace A {
class B {
public:
bool func_init(); // init
};
};
Run Code Online (Sandbox Code Playgroud)
data_structure.cpp
#include "data_structure.h"
using namespace A;
bool B::func_init(){
std::cout << "test init" << std::endl;
return true;
}
Run Code Online (Sandbox Code Playgroud)
main.cpp中
#include <iostream>
#include "data_structure.h"
using namespace A;
int main( int argc, char **argv ) {
A::B s;
s.func_init();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我有一个错误如下
未定义的引用`A :: B :: func_init()'
请告知为什么我不能获得func_init,尽管它被宣布为公开?我在那里也放了正确的命名空间.
对任何回复表示感谢.
请为我这个非常基本的问题鞭挞我,但我仍然无法理解为什么会这样.
我读到了为了在昏迷后打印几个值,我应该在C中使用%.f
所以我有这个问题,计数90/100.我希望打印0.9
#include <stdio.h>
#include <math.h>
int main()
{
double c=0;
c = 90/100;
printf("%.1f\n", c);
}
Run Code Online (Sandbox Code Playgroud)
它显示我0.0 ..(错误..).试图将其更改为(printf("%f \n",c))返回0.00000 ..(错误..)
谁能帮我这个?(对不起,编程真的很新......)
谢谢
我正在学习C++,并且我正在尝试创建一个函数,其返回类型为"my_message_t",它是在类定义中定义的.但是当我尝试编译它时,编译器告诉我错误:'my_message_t'没有命名类型
我有以下代码(protocols.h和protocols.cpp)
namespace Protocols {
class Messages {
public:
typedef struct _my_message_t {
//stuffs
} my_message_t;
typedef enum {
//stuffs
} my_message_e;
private:
my_message_t my_msg;
my_message_e msg_en;
public:
Messages();
~Messages();
my_message_t create_message(const my_message__e);
};
};
Run Code Online (Sandbox Code Playgroud)
类定义如下:
namespace Protocols {
Messages::Messages(){
//stuff
};
Messages::~Messages(){
//stuffs
}
my_message_t Messages::create_message(my_message_e type){
my_message_t msg;
//do stuffs
return msg;
}
}
Run Code Online (Sandbox Code Playgroud)
为什么我不能创建一个类型的函数my_message_t
?我如何修复上面的代码?
我有一个函数返回指向结构的指针,如下所示:
//header file
typedef struct {
unsigned char *buffer;
uint8_t len;
} T_ABC_PACKET
Run Code Online (Sandbox Code Playgroud)
在主文件中,我创建一个指向函数的指针并尝试打印它
T_ABC_PACKET *pct = NULL;
pct = function_that_return_the_packet;
printf("value of packet is %s \n", pct->buffer);
Run Code Online (Sandbox Code Playgroud)
结果始终与打印功能一致.我希望缓冲区有一个8字节,最后一个字节总是一个损坏的内存. 值是10000357` 2U
但如果我在函数内打印缓冲区:
T_ABC_PACKET* function_that_return_the_packet {
T_ABC_PACKET *pct = NULL;
char string_temp[80];
//some more initialization...
pct->buffer = (unsigned char *)string_temp;
pct->len = 5;
printf("value of packet is %s \n", pct->buffer);
return pct;
}
Run Code Online (Sandbox Code Playgroud)
函数中打印的值为10000357f.只有最后一个字符被破坏.这总是提供一致的值,没有多次我运行程序,只有最后一个字符在函数的调用者中被破坏.我理解一个可能的情况是内存泄漏,但我试着仔细检查,我找不到任何泄漏.如何让pct->缓冲区正确包含所有内容?