我编写此代码将文件'vetores'的内容复制到char矩阵,但我认为动态分配存在问题.有人可以帮帮我吗?
void cpyarqvetores( char** arqvetores, FILE* varquivo);
void freearqvetores( char** arqvetores );
int main()
{
FILE* varquivo;
varquivo = fopen("vetores.txt", "r");
char** arqvetores;
arqvetores = (char**) malloc(10*sizeof(char*));
cpyarqvetores( arqvetores, varquivo);
printf("%s\n", *(arqvetores + 4));//try prints this line
freearqvetores( arqvetores );
fclose( varquivo );
return 0;
}
//copy the contents of file 'vetores' to 'arqvetores'
void cpyarqvetores( char** arqvetores, FILE* varquivo){
char aux[440];
int strtam;
int i, j;
for( i = 0; i < 10; i++){
fgets( aux, 440, varquivo);
strtam …Run Code Online (Sandbox Code Playgroud) 我有以下结构
typedef struct DeviceInfo
{
char[30] name;
char[30] serial Number;
}DeviceInfo;
I am doing this
DeviceInfo* m_DeviceInfo = new DeviceInfo[4];
// Populate m_DeviceInfo
Run Code Online (Sandbox Code Playgroud)
然后我想重新调整大小m_DeviceInfo 为6,并希望保留Previous 4 Value.
怎么用c ++做?
C++中的伪代码
char* data = new char[determine_size()];
// ... do some stuff with data
delete[] data;
// ... repeat process
Run Code Online (Sandbox Code Playgroud)
所以基本上,每次都会将数据重复分配给具有不同大小的新数组.虽然每次先前的分配总是先取消分配.
这会导致内存碎片吗?
我对C++中的内存管理不是很熟悉; 我的直觉告诉我这不是一个好主意.
void fun()
{
A *a = new A; //Here A is a class
} //a should be deleted in fun()'s scope
int main()
{
fun();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
创建的对象存在于免费存储中,main()函数无法使用.为什么要在免费商店上创建对象.是的,我们可以将对象引用传递给main函数,但我们甚至可以传递对象的副本(即使不使用new运算符创建).那么new和delete运算符的确切用法是什么?
请阅读问题而不是假设这是重复的.
在我正在处理的代码库中,我有一个Foo对象列表:
private List<Foo> fooList;
Run Code Online (Sandbox Code Playgroud)
有人可以向我解释这行代码之间的区别:
Foo foos[] = fooList.toArray(new Foo[fooList.size()]);
Run Code Online (Sandbox Code Playgroud)
这条线?
Foo foos[] = (Foo[]) fooList.toArray();
Run Code Online (Sandbox Code Playgroud) 我试图malloc在初始化另一个动态分配的数组后再次调用,但我的程序无法运行(尽管它可以通过编译).我的部分代码如下.
table = (Node **)malloc(m * sizeof(Node*));
for(i=0; i<=m; i++)
table[i] = NULL;
table2 = (Node *)malloc(n * sizeof(Node));
Run Code Online (Sandbox Code Playgroud)
错误信息如下:
malloc.c:2372: sysmalloc: Assertion `(old_top == (((mbinptr) (((char *) &((av)
->bins[((1) - 1) * 2])) - __builtin_offsetof (struct malloc_chunk, fd)))) && old_si
ze == 0) || ((unsigned long) (old_size) >= (unsigned long)((((__builtin_offsetof (st
ruct malloc_chunk, fd_nextsize))+((2 *(sizeof(size_t))) - 1)) & ~((2 *(sizeof(size_t
))) - 1))) && ((old_top)->size & 0x1) && ((unsigned long) old_end & pagemask) == 0)'
failed.
Run Code Online (Sandbox Code Playgroud)
最奇怪的是,我发现我的程序在删除上面代码中的第二行和第三行后可以成功运行,其中 …
我正在努力刷新我的C技能.假设我正在尝试执行a malloc和a calloc:
void* allocate_array(size_t member_size, size_t nmember,bool clear)
if(clear){
void *cal = calloc(nmember, sizeof(member_size));
return cal;
} else if(!clear) {
void *mal = (void *)malloc(sizeof(member_size));
return mal;
}
Run Code Online (Sandbox Code Playgroud)
我想我在这里正确使用calloc,但我不确定我是否正确使用malloc.当我需要返回虚空时,它会让我感到困惑.
我是新来的c&我遇到了一个新问题..
file1.c-
#include <stdio.h>
#include <stdlib.h>
extern int sec();
char *ptr=NULL;
int main(){
char *ptr=NULL;
ptr=(char*)calloc(sizeof(char),8);/*8 chars.*/
*(ptr+0)='0'; /*first char set to 0.*/
printf("%c\n",*ptr);
*(ptr+0)='r';
*(ptr+1)='o';
*(ptr+2)='i';
*(ptr+3)='L';
printf("%c %c %c %c \n",*(ptr+0),*(ptr+1),*(ptr+2),*(ptr+3));
sec();
return 0;}
Run Code Online (Sandbox Code Playgroud)
和file2.c-
#include <stdio.h>
#include <stdlib.h>
extern char *ptr;
void sec(void){
puts("before.");
*(ptr+0)='L';/*CARSH HERE.*/
*(ptr+1)='i';
*(ptr+2)='o';
*(ptr+3)='r';
puts("after.");
printf("%c %c %c %c ",*(ptr+0),*(ptr+1),*(ptr+2),*(ptr+3));
free(ptr);}
Run Code Online (Sandbox Code Playgroud)
我之前做过类似的事(有一点不同),但现在它崩溃了.是什么原因 ?.如果我将在源文件中构建一个函数,在哪里分配了内存,它将被解决?
我正在编写一个接受数组大小作为参数的程序,然后应该为该大小动态分配内存.
我希望我的程序返回已分配的内存大小.目前我正在使用argv读取输入值,然后mmap用于动态分配.我还使用for循环来显示在数组的每个索引中保存的数字.
我知道分配可以完成malloc,但我无法使用它.我想我需要从最后一个元素开始并检查所有单元格,直到我得到一个SEGFAULT信号.
谢谢你的帮助.
编辑:没有库等的短代码...
input = (atoi(argv[1]));
int* arr = mmap(0, input, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, fd, 0);
for (i = 0; i < input; i++) {
arr[i] = i + 1;
}
Run Code Online (Sandbox Code Playgroud) 我有一个简单的问题..我可以在运算符+其他对象中分配和释放这样的内存
delete [] p.name;
p.name = new char[strlen(a.name) + 1];
看着那(这 operator+
class PlDrustvo{
private:
char *name;
public:
// Constructors and destructors are implemented.
PlDrustvo operator+(const PlDrustvo &a)
{
PlDrustvo p(*this);
if(///////){
delete [] p.name;
p.name = new char[strlen(a.name) + 1];
strcpy(p.name, a.name);
}
return p;
}
};
Run Code Online (Sandbox Code Playgroud)