当你在C中声明静态变量时,
说你写了这样一个程序:
int *c;
void foo(){
c = (int *) malloc (10*sizeof(int));
c = &c[3];
*c = *&c[3];
}
Run Code Online (Sandbox Code Playgroud)
有什么意义*c?我的意思是我理解这*意味着它指的是什么,但*c做了什么?
而且,为什么需要转换(int *)返回值malloc()?
#include <stdio.h>
#include <stdlib.h>
int main() {
system("clear");
int *pt = malloc(2 * sizeof *pt);
int *tmp = NULL;
int i;
pt[0] = 44;
pt[1] = 9;
printf("pt[0] : %d\n", pt[0]);
printf("pt[1] : %d\n", pt[1]);
tmp = realloc(pt, 3 * sizeof *pt);
if (!tmp) {
printf("merde alors\n");
} else {
pt = tmp;
for (i = 0; i < 5; i++) {
pt[i] = i + 1;
printf("pt[%d] : %d\n", i, pt[i]);
}
}
//the compiler should give me an …Run Code Online (Sandbox Code Playgroud) 我有两个问题.
freeC 中的函数如何工作?这是我的代码:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a;
int *p;
p = (int *)malloc(sizeof(int));
*p = 10;
int *q = p;
printf("p:%d\n", p);
printf("q:%d\n", q);
printf("*p:%d\n", *p);
printf("*q:%d\n", *q);
free(p);
printf("Memory freed.\n");
p = (int *)malloc(sizeof(int));
*p = 19;
printf("p:%d\n", p);
printf("q:%d\n", q);
printf("*p:%d\n", *p);
printf("*q:%d\n", *q);
}
Run Code Online (Sandbox Code Playgroud)
为什么输出是这样的?
p:2067804800
q:2067804800
*p:10
*q:10
Memory freed.
p:2067804800
q:2067804800
*p:19
*q:19
Run Code Online (Sandbox Code Playgroud) 抱歉天真,有人可以帮助我这个c ++程序如何工作?它应该将十进制转换为二进制.为什么在这里使用左移和右移?
void static inline unsignedToBinary(unsigned x, char*& bin)
{
bin = (char*) malloc(33);
int p = 0;
for (unsigned i = (1 << 31); i > 0; i >>= 1)
bin[p++] = ((x&i) == i) ? '1' : '0';
bin[p] = '\0';
}
Run Code Online (Sandbox Code Playgroud) 我试图测试我的程序的一部分是否正常工作,但在运行以下代码时,
Car *carList;
carList = (Car*) malloc (length * sizeof(Car));
carList[0].setMake("a");
carList[0].setModel("b");
carList[0].setYear("c");
carList[0].setColor("d");
carList[0].printCar();
Run Code Online (Sandbox Code Playgroud)
该程序在第一个函数调用setMake中遇到问题.这是我的车类:
class Car {
private:
string cmake;
string cmodel;
string cyear;
string ccolor;
public:
Car(){};
Car(string *cmake, string *cmodel, string cyear, string *ccolor);
void printCar(){
cout << "Make: " << cmake << endl;
cout << "Model: " << cmodel << endl;
cout << "Year: " << cyear << endl;
cout << "Color: " << ccolor << endl << endl;
return;
};
string getMake(){return cmake;};
string …Run Code Online (Sandbox Code Playgroud) Malloc 函数成功时返回指向分配的内存位置的指针,失败时返回空值。如果我将 malloc() 放入无限 while 循环中,该循环会停止吗?malloc() 分配全部可用内存后,它会停止执行还是继续返回空值?
我将首先用C++发布我的测试程序:
#include <iostream>
using namespace std;
class X
{
int x;
public:
X()
{
cout<<"constructing\n";
x=0;
}
int& getx()
{
return x;
}
~X()
{
cout<<"destroying\n";
}
};
int main()
{
X* p=(X*)malloc(sizeof(X));
++p->getx();
p->getx()*=5;
cout<<p->getx();
free(p);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出:
5
Run Code Online (Sandbox Code Playgroud)
现在,在任何人抱怨为什么我在C++程序中使用malloc&之前free,我想重申它只是一个测试程序,即使使用operator new&也可以完成上述操作operator delete.但我的问题仍然是:
malloc or operator new我们如何访问类X的变量x?free & operator delete也不要破坏对象并执行纯粹的分配.如果我new使用operator delete or free而不是使用而创建一个对象会发生什么delete?我的对象是否仍然存在并且仍然可用吗?人们只需停留在UB即可删除此主题...
我已经使用gcc prog.c -Wall -Wextra -pedantic -std=gnu11命令在GCC上编译了以下代码.它不会产生任何警告或错误.
码:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i = 10;
int *ptr = malloc(1);
ptr = &i; // is it legal?
printf("%d\n", *ptr);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
显示的输出10.
这里,为ptr指针使用malloc函数分配动态内存,然后ptr保存ivarable 的地址.
ptr = &i;用C语写是否合法?
编辑:
那么,编译器是否可以生成有关内存泄漏的警告?
我希望能够让用户输入他们想要做的事情.还有其他选择,但现在我正在研究"插入".另外两个选项是"搜索"和"删除".
int main() {
char *input = malloc(sizeof(char) * 6);
printf("%s", "WELCOME TO USER'S SKIP LIST!\n\nWhat do you wish to do? ");
scanf("%6s", input);
if (strcmp(input, "insert") == 0) {
printf("%s", "What is the item? ");
input = realloc(input, (size_t)scanf("%s", input));
}
}
Run Code Online (Sandbox Code Playgroud)
我最初分配足够的内存,input有6个字符.这是因为其他两个选项也只有6个字符.进入后,他们insert必须输入一个具有任意数量字符的项目,因此我想input根据他们为项目输入的内容重新分配内存.因此,如果他们进入Nintendo Switch,将重新分配15个字符.我的执行realloc()方式是否正确?