在以下代码中:
int main(int argc,char * argv[]){
int * ptr;
ptr = 0; // tried also with NULL , nothing changes
ptr = new int[10]; // allocating 10 integers
ptr[2] = 5;
ptr[15] = 15; // this should cause error (seg fault) - but it doesn't
cout << ptr[2] << endl;
cout << ptr[15] << endl; // no error here
delete [] ptr;
cout << ptr[2] << endl; // prints the value 5
cout << ptr[15] << endl; // prints the …Run Code Online (Sandbox Code Playgroud) 我将如何在此函数中动态分配内存到char**列表?
基本上这个程序的想法是我必须从文件中的单词列表中读取.我不能假设最大字符串或最大字符串长度.
我必须用C字符串做其他的东西,但那些东西我应该没问题.
谢谢!
void readFileAndReplace(int argc, char** argv)
{
FILE *myFile;
char** list;
char c;
int wordLine = 0, counter = 0, i;
int maxNumberOfChars = 0, numberOfLines = 0, numberOfChars = 0;
myFile = fopen(argv[1], "r");
if(!myFile)
{
printf("No such file or directory\n");
exit(EXIT_FAILURE);
}
while((c = fgetc(myFile)) !=EOF)
{
numberOfChars++;
if(c == '\n')
{
if(maxNumberOfChars < numberOfChars)
maxNumberOfChars += numberOfChars + 1;
numberOfLines++;
}
}
list = malloc(sizeof(char*)*numberOfLines);
for(i = 0; i < wordLine ; i++)
list[i] …Run Code Online (Sandbox Code Playgroud) 所以我正在学习结构,并且正在尝试输入带有动态内存分配的字符串.
这是我到目前为止:
typedef struct{
char foo[81];
} TYPE;
void function(TYPE get[]){
char a[81];
scanf("%s", a);
get->foo = malloc(strlen(a)+1*sizeof(char)); //this line produces an error that it is not assignable
strcpy(get->foo,a);
return;
}
Run Code Online (Sandbox Code Playgroud)
我不确定该声明有什么问题,任何帮助都会得到很好的赞赏.
在动态分配struct之后,我认为在最后添加'delete'是谨慎的.但它给了我一个运行时错误.虽然编译好了.因此,如果我摆脱'删除',它运行正常.但我担心可能会有内存泄漏.处理此代码的安全方法是什么?
#include <iostream>
#include <vector>
using namespace std;
typedef struct
{
vector<char*> vstr;
vector<int> vint;
}VecStrInt;
int main()
{
VecStrInt * mtx = new VecStrInt();
mtx[0].vstr.push_back("Hello");
mtx[0].vint.push_back(1);
cout<<mtx[0].vint.at(0)<<endl;
cout<<mtx[0].vstr.at(0)<<endl;
//delete [] mtx;
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我被要求编写两个函数来在C++中分配和释放int数组.
int* allocate(int size){
return new int[size];
}
void deallocate(int *pt){
delete pt;
pt = NULL;
}
Run Code Online (Sandbox Code Playgroud)
我想出了上面的两个功能.
有谁知道有更好的方法来编写在C++中分配/解除分配 int数组的函数?
c++ pointers new-operator dynamic-allocation delete-operator
在下面给出的示例程序中(来源:http ://www.cplusplus.com/reference/unordered_map/unordered_map/rehash/ )
// unordered_map::rehash
#include <iostream>
#include <string>
#include <unordered_map>
int main ()
{
std::unordered_map<std::string,std::string> mymap;
mymap.rehash(20);
mymap["house"] = "maison";
mymap["apple"] = "pomme";
mymap["tree"] = "arbre";
mymap["book"] = "livre";
mymap["door"] = "porte";
mymap["grapefruit"] = "pamplemousse";
std::cout << "current bucket_count: " << mymap.bucket_count() << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出变为:
current bucket_count: 23
Run Code Online (Sandbox Code Playgroud)
为什么桶数变成23?对堆大小有什么影响?堆分配什么时候完成?在存储桶重新散列上还是在实际插入上?动态释放何时完成?何时clear()使用或erase()两者都使用?
我的目标是动态分配二维数组,以便它提示用户输入他们要创建的矩阵数组的行和列的大小。在动态分配行和列的大小之后,用户将输入所需的值。以下是我的C ++代码:
#include <iostream>
using namespace std;
int main()
{
int* x = NULL;
int* y = NULL;
int numbers, row, col;
cout << "Please input the size of your rows: " << endl;
std::cin >> row;
cout << "Please input the size of your columns: " << endl;
std::cin >> col;
x = new int[row];
y = new int[col];
cout << "Please input your array values: " << endl;
for (int i = 0; i<row; i++)
{
for (int …Run Code Online (Sandbox Code Playgroud) 刚开始使用C ++(几天后),我就有C背景。
我有一个类,主要包含一个指向带有以下代码的int数组的指针:
class Array
{
private:
int * _arr;
int _size;
public:
Array();
Array(int size);
Array(const Array& obj); // copy constructor
~Array();
void readInValues();
void mult(int num);
void add(int num);
void printArray();
};
Run Code Online (Sandbox Code Playgroud)
_arr是指向int数组的指针,当使用复制构造函数创建新实例时,我会在堆上创建一个新的int数组(我认为)。在复制构造函数中:
Array::Array( const Array & obj )
{
_arr = new int[_size];
for(int i=0;i<_size;i++)
*_arr[i] = *obj._arr[i];
}
Run Code Online (Sandbox Code Playgroud)
我要做的第一件事是为新数组分配内存(_size是原始类型,因此据我所知会自动复制)。我想做的下一件事是使用循环复制数组本身。该部分无法通过编译说非法间接。我不确定为什么...
我有以下代码:
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#include <sys/stat.h>
void print_usage()
{
printf("%s\n", "usage");
}
int file_exist (char *filename)
{
struct stat buffer;
return (stat (filename, &buffer) == 0);
}
int parse_parameters(int argc, char *argv[], char** in)
{
unsigned int i1 = 1; // 0 is the filename
for (; i1 < argc; ++i1)
{
if( 0 == strcmp("-h", argv[i1]) )
{
print_usage();
return 0;
}
else if( 0 == strcmp("-i", argv[i1]) )
{
*in = malloc( sizeof(char) …Run Code Online (Sandbox Code Playgroud) 有哪些可能的情况可以使以下代码执行if以下代码段中的条件?就我而言,我无法将任何原因if与执行语句联系起来.
#include <stdio.h>
#include <stdlib.h>
void main(void){
int Nod = 1024 * 8; //Nod contains the number of nodes
double *MM; //MM is a square matrix it can contain very large number of data 10^10
MM = calloc(8 * Nod * 8 * Nod, sizeof(double));
if (MM == NULL)exit(0);
//then MM will then be passed to some other functions say
eigenvalue(MM);}
Run Code Online (Sandbox Code Playgroud)
我正在使用一个FEM代码,它在一个非常大的程序中间进行了这项检查.有趣的是,当我运行代码时,它会显示异常行为.有时程序会在这里停止.有时它只是工作正常.值得一提的是,当程序以粗网格运行时,即当Nod计算节点数量较少时,程序运行正常.但是当使用精细网格时,程序崩溃不幸.该程序在具有128GB Ram的迷你工作站中运行.该程序占用1GB(左右)的RAM.
c++ ×6
arrays ×4
c ×4
c-strings ×1
calloc ×1
g++4.9 ×1
heap-memory ×1
malloc ×1
memory ×1
new-operator ×1
parameters ×1
pointers ×1
valgrind ×1
vector ×1