我最近进入图形编程,我注意到许多图形引擎(即Ogre)和许多编码器总体上更喜欢动态地初始化类实例.这是Ogre Basic Tutorial 1的一个例子
//...
Ogre::Entity* ogreHead = mSceneMgr->createEntity("Head", "ogrehead.mesh");
Ogre::SceneNode* headNode = mSceneMgr->getRootSceneNode()->createChildSceneNode("HeadNode");
//...
Run Code Online (Sandbox Code Playgroud)
ogreHead然后将headNode数据成员和方法称为ogreHead->blabla.
为什么要乱用对象指针而不是普通对象?
顺便说一下,我还读到了堆内存分配比堆栈内存分配慢得多的地方.
我有一个简单的C程序,表示控制台内的加载屏幕,但无法隐藏光标。我尝试提高睡眠功能的速度,以便重置游标计时器并且游标消失,但这不起作用。
有关如何隐藏光标的任何提示。
码:
#include <stdio.h>
#include <stdlib.h>
const int TIME = 1;
int main(int argc,char *argv[]){
int i;
while (1){
printf("loading");
for (i=0;i<3;i++){
sleep(TIME);
printf(".");
}
sleep(TIME);
printf("\r");
system("Cls");
sleep(TIME);
}
}
Run Code Online (Sandbox Code Playgroud) 我已经看到了在C99中使用以下表达式分配2D数组的各种建议:
int (*array)[cols] = malloc(rows * sizeof *array);
Run Code Online (Sandbox Code Playgroud)
我想知道三件事:
整个结构是否在堆上分配?或者这实际上是一堆指针(在堆栈上)指向堆上的数组..?
分配的内存是否完全连续?
是否只需要一次调用free(array)来释放整个2D结构?我在某个地方读过这个 - 不记得在哪里 - 它似乎对我有用,但我想明白为什么.
#include <stdio.h>
FILE *openfile()
{
FILE *fp1;
fp1 = fopen("test.c","r");
return fp1;
}
int main()
{
char c;
FILE *fp;
int a = 10;
int b = 1000 - a;
printf("Hello\n");
fp = openfile();
fscanf(fp, "%c", &c);
fclose(fp);
printf("%c", c);
}
Run Code Online (Sandbox Code Playgroud)
在上面的程序中,我在一个函数中打开一个文件描述符并传递给其他函数.这工作正常.
fp1在函数openfile()的本地声明.因为我们传递地址,从主函数我能够使用文件指针结构.
我的问题是FILE结构存储在哪里?结构的范围是什么?
曾经使用Android Studio的任何人都知道它有一个非常有用的代码linting容量,可以帮助程序员避免常见的反模式和错误.
然而,这样的系统有时令人烦恼,在这种特殊情况下,我认为它就是这样.
我有一个AsyncTask像下面这样的人
class MyAsyncTask extends AsyncTask<Void, Void, MyDataType> {
private Context context;
MyAsyncTask(Context _context) {
context = _context;
}
@Override
protected void onPreExecute() {
// Show a progress dialog or something
// to indicate that we're doing some work
// here.
}
@Override
protected MyDataType doInBackground(Void... args) {
return generateData(); // returns `MyDataType` of course
}
@Override
protected void onPostExecute(MyDataType data) {
// Deliver the data and then
context = null;
}
}
Run Code Online (Sandbox Code Playgroud)
当然,Android Studio正在告诉我该context …
我会表明我的意思:
标题:
#ifndef _HASH_H_
#define _HASH_H_
typedef void* pKey;
typedef int (*HashFunc) (pKey key, int size);
#endif
Run Code Online (Sandbox Code Playgroud)
新标题:
#ifndef _DICT_H_
#define _DICT_H_
#include "hash.h"
HashFunc HashWord;
#endif
Run Code Online (Sandbox Code Playgroud)
现在这里我不知道该写什么,我想写这个HashWord函数本身
c文件
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "hash.h"
#include "dict.h"
typedef struct _wordElement {
char* word;
char* translation
} wordElement, *pwordElement;
HashFunc HashWord{
......... here i will want to write the Code
}
Run Code Online (Sandbox Code Playgroud)
现在它给了我一个错误,我该怎么写最后一行?
也许
HashFunc HashWord(pKey theKey,int number){...}
Run Code Online (Sandbox Code Playgroud)
也许
HashFunc HashWord( theKey, number){...}
Run Code Online (Sandbox Code Playgroud)
也许
int HashWord (pKey key, …Run Code Online (Sandbox Code Playgroud) 为什么我不能像这样使用struct?
typedef struct { unsigned char FirstName; unsigned char LastName; unsigned int age; } User;
User UserNick = {Nick, Watson, 24};
NSLog(@"Your paint job is (R: %NSString, G: %NSString, B: %u)",
UserNick.FirstName, UserNick.LastName, UserNick.age);
Run Code Online (Sandbox Code Playgroud)
我的意思是我确实使用了这样的结构:
typedef struct {unsigned char red; unsigned char green; unsigned char blue; } Color;
Color carColor = {255, 65,0};
NSLog(@"Your paint job is (R: %hhu, G: %hhu, B: %hhu)",
carColor.red, carColor.green, carColor.blue);
Run Code Online (Sandbox Code Playgroud) 我在编译 C 代码方面相对缺乏经验。不过,我正在尝试使用 emscripten 将 R 源代码中的算法编译为 JavaScript。该函数位于此处调用的文件中pf.c
天真地,我刚刚进入src目录并尝试:
emcc nmath/pf.c
Run Code Online (Sandbox Code Playgroud)
这导致:
In file included from nmath/pf.c:25:
nmath/nmath.h:38:10: fatal error: 'Rconfig.h' file not found
#include <Rconfig.h>
^
1 error generated.
ERROR root: compiler frontend failed to generate LLVM bitcode, halting
Run Code Online (Sandbox Code Playgroud)
我知道这个标题在哪里:
$ find . -name "Rconfig.h"
./gnuwin32/fixed/h/Rconfig.h
Run Code Online (Sandbox Code Playgroud)
我的直接问题是,我如何告诉编译器这个头文件在哪里?我想最终得到一个pfJavaScript 函数。对这个想法的任何见解将不胜感激。
在fun1()中我得到一个字符串,我需要将这个字符串数据传递给函数fun2(),它接受参数char*
function2原型如下
void fun2(char *p);
Run Code Online (Sandbox Code Playgroud)
我从fun1调用fun2,如下所示
void fun1()
{
fun2((char *)str.c_str());
}
Run Code Online (Sandbox Code Playgroud)
从const char*到char*的转换是否存在任何潜在问题
我编写了一个C程序,用于使用指针显示数组的值.这是代码:
#include <stdio.h>
int main()
{
int a[] = {1, 1, 1, 1, 1};
int *ptr = a;
for (int i = 0 ; i < 5; i++)
printf("%d ", *ptr++);
printf("%d", *ptr);
}
Run Code Online (Sandbox Code Playgroud)
正如您在终止循环后所看到的那样,指针将数值的内存地址保存在数组之外.因为它没有初始化最后一个输出,它应该是一个垃圾值.但是,每次显示5是数组的大小.然后,我认为数组的已分配内存的下一个内存地址包含数组的大小.但是,双类型数组不会发生这种情况.
Output for int array : 1 1 1 1 1 5
Output for double array : 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 0.000000
Run Code Online (Sandbox Code Playgroud)
有人会解释输出吗?