我想问你一个问题.我有以下代码:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#define XXX 1024*1024
int main()
{
int *p;
unsigned long x=0;
while (1)
{
//p = (int *) calloc (1,XXX);
p = (int *) malloc (XXX);
memset (p,0,XXX);
x++;
printf ("%lu MB allocated.\n",x);
sleep (1);
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
如果我运行此代码,一切都正常运行.每秒,在内存中分配一个新的MB.我遇到的问题是如果我取消注释calloc()行并注释malloc()和memset()行.据我所知,calloc()应该在分配的内存中将所有字节初始化为零; 与malloc()和memset()相同的事情.
当我使用calloc()(没有malloc()和memset())运行代码时,分配初始1 MB(正常),然后在几秒(~10)之后分配另一个MB.
为什么会这样?
提前致谢!
当分配到void-pointer 或从-pointer 分配时,不需要强制转换(C99§6.3.2.2sub 1/§6.5.16.1sub 1).将(例如int- )指针传递给期望void-pointer 的函数时,这也是正确的吗?
例如:
void foo(void * p){
// Do something
}
int main(){
int i = 12;
foo(&i); // int * to void *: no cast needed?
}
Run Code Online (Sandbox Code Playgroud)
当我使用GCC(4.8.1,MinGW-32)编译它时,我既没有错误也没有警告(使用-Wall&-pedantic).
与此答案相反,建议此调用需要强制转换(以消除-Wformat警告):
int main(){
int i = 12;
printf("%p\n", &i);
}
Run Code Online (Sandbox Code Playgroud)
但就我而言,海湾合作委员会并没有抱怨.
那么:将非void指针传递给期望void-pointer 的函数时需要进行强制转换吗?
对于两个(或更多)structS:Base和Sub具有共同第一(未命名)struct,是安全的转换/从投Base至Sub反之亦然?
struct Base{
struct{
int id;
// ...
};
char data[]; // necessary?
}
struct Sub{
struct{
int id;
// same '...'
};
// actual data
};
Run Code Online (Sandbox Code Playgroud)
这些功能是否保证安全且技术上正确?(另外:零长度char data[]成员是否必要且有用?)
struct Base * subToBase(struct Sub * s){
return (struct Base*)s;
}
struct Sub * baseToSub(struct Base * b){
if(b->id != SUB_ID){
return NULL;
}
return (struct Sub*)b;
}
Run Code Online (Sandbox Code Playgroud)
编辑
我没有计划Base在内部进一步嵌套Sub,而是可以在以后添加其他子类型(直接在下面Base),而无需更改Base …
我对数据抽象的理解是隐藏用户的技术细节并仅显示必要的细节.因此,数据抽象是一种OOP功能.我的问题是:C是否也支持数据抽象?
如果是这样,为什么数据抽象是面向对象的编程语言特性而不是过程语言特性?
如果我的问题的答案是否定的,那么C中的结构,枚举呢?他们还隐藏了用户的详细信息.
有没有人知道const char * const *用两个文字字符串("abcdefg"和"hijklmnop")初始化a的正确方法?我读过很难/不可能从a转换char **,但我可能错了.
任何帮助非常感谢.
考虑以下类
class Foo
{
public:
void* func(void* arg)
{
// how to pass this function to pthread...?!
}
}
Run Code Online (Sandbox Code Playgroud)
后来我想传递func()给pthread_create(), 而不是一个函数:
int main()
{
char * msg = "Hi dude";
Foo * ins = new Foo();
pthread_t pt;
// how to pass ins->func instead of a function?
pthread_create( &pt, NULL, ins->func, (void*)msg );
}
Run Code Online (Sandbox Code Playgroud)
提前致谢。
我有一个int,我想获得一个包含该int的char*.我的代码是:
int getLength(int x) {
int l = 0;
while(x) {
l++;
x /= 10;
}
return l;
}
char* toString(int x) {
int l = getLength(x);
char *str = new char[l];
l--; // the digits will be placed on positions 0 to l-1
while(l >= 0) {
str[l] = x % 10 + '0';
x /= 10;
l--;
}
return str;
}
Run Code Online (Sandbox Code Playgroud)
一些结果:
toString(1009) = 1009Ä
toString(23) = 23L
Run Code Online (Sandbox Code Playgroud)
为什么?我只为l个字符分配空间.(l = int的长度)
以下代码应该使用停止密码加密一个句子,它仍然是WIP我没有考虑空格,小写字母等.
int main(int argc,string argv[])
{
int k = atoi(argv[1]);
string value = GetString();
int n = strlen(value);
for(int i = 0;i<n; i++);
{
char temp = value[i];
int conv = temp - (int)'A';
int cipher = (conv + k)%26;
char final = cipher + (int)'A';
printf("%c\n",final);
}
}
Run Code Online (Sandbox Code Playgroud)
我明白了
caeser.c:15:23 use of undeclared identifier i.
Run Code Online (Sandbox Code Playgroud)
哪个就行了 char temp = value[i];
我目前正在用C#编写一个程序,这是一种我不太熟悉的语言,并且在使用递归时我注意到了一些意想不到的值.我的问题是,在C#中使用递归时,是否在堆栈中抛出函数调用并像在Java中一样处理,或者C#是否尝试并实现fork()/ pthread伪并行以增加运算时间?
我正在尝试将一些字符串写入文件.编译时没有任何警告,但是当我运行a.out时会出现段错误.但它会创建目标文件.我是C的新手,所以我为我的格式和其他缺点道歉.这是我的代码:
#include<stdio.h>
int main (void)
{
FILE *fp_out;
char str1[]="four score and seven years ago our";
char str2[]="fathers broughy forth on this continent,";
char str3[]="a new nation, concieved in Liberty and dedicated";
char str4[]="to the proposition that all men are created equal.";
fp_out=fopen("my_file", "w");
if(fp_out!=NULL)
{
fprintf(fp_out,"%s\n", str1[100]);
fprintf(fp_out,"%s\n", str2[100]);
fprintf(fp_out,"%s\n", str3[100]);
fprintf(fp_out,"%s\n", str4[100]);
fclose(fp_out);
}
else
printf("The file \"my_file\" couldn't be opened\n");
return 0;
}
Run Code Online (Sandbox Code Playgroud)