我知道在c语言中,我需要在分配后释放内存.(我来自java),对此我有几个问题:
当我在做的时候:
int array[30];
Run Code Online (Sandbox Code Playgroud)
(即创建一个大小为30的整数数组)这与做什么一样?
int array[] = malloc(sizeof(int)*30);
Run Code Online (Sandbox Code Playgroud)作为第一个问题的序列,当我在函数内部创建数组(即函数的本地数据而不是整个文件的全局数据)时,我是否需要在创建它的函数内释放此数组的内存?(我没有看到任何其他方法来释放它,因为我无法传递创建回main()函数的所有数组的引用).
所以简而言之,我想知道我何时需要为创建的对象/基元(在函数内部或外部)释放内存.
我正在编写一个基本的CUDA程序,以便更好地理解该语言.我写了一些非常基本的东西,只是并行添加两个向量,并将结果打印到ppm文件.现在,矢量中的值是无关紧要的,因为我计划稍后调整它以产生某种类型的有趣图像.问题是图像的分辨率(实际上是结果向量)导致程序几乎立即崩溃,如果我使它太大.考虑现在的程序:
#include <stdio.h>
#define cols 500
#define rows 50
#define arraySize rows * cols
__global__ void addOnGPU(int *a, int *b, int *c) {
// Only use data at this index
int tid = threadIdx.x + blockIdx.x * blockDim.x;
if (tid < arraySize) c[tid] = a[tid] + b[tid];
}
int main()
{
FILE *ppm_fp;
int a[arraySize], b[arraySize], c[arraySize];
int *dev_a, *dev_b, *dev_c;
int i, j;
int threadsperblock = 256;
int blocks = (arraySize + threadsperblock - 1) / threadsperblock;
printf("1\n");
// …Run Code Online (Sandbox Code Playgroud) 我正在从IL(Compiled C#\ VB代码)创建一个反编译器.有没有办法在C中创建引用?
编辑:
我想要比堆栈之类的指针更快的东西.有这样的事吗?
我想问一下这有什么区别
Tv *television = new Tv();
Run Code Online (Sandbox Code Playgroud)
和
Tv television = Tv();
Run Code Online (Sandbox Code Playgroud) 每当我们从一个类创建一个对象时,它就会在堆上创建占用更多空间,而另一个结构变量占用堆栈上的内存.如果我创建一个Person类和一个具有相同属性的struct P,那么它应该证明我刚才所说的.请检查以下2个代码片段:
#include <iostream.h>
#include <conio.h>
#include <string>
using namespace std;
class Person{
int age;
string hair_color;
float height;
public:
Person::Person(int n)
{
age = n;
}
int Person::getAge()
{
return age;
}
};
struct P{
int age;
};
main()
{
Person person(45);
//Person *person = new Person(45);
P myPerson;
cout<<sizeof(person)<<endl;
cout<<sizeof(myPerson)<<endl;
//cout<<"Age: "<<person->getAge();
getch();
}
Run Code Online (Sandbox Code Playgroud)
当我写这段代码时:
#include <iostream.h>
#include <conio.h>
#include <string>
using namespace std;
class Person{
int age;
string hair_color;
float height;
public:
Person::Person(int n)
{
age = …Run Code Online (Sandbox Code Playgroud)