我不确定我是否使用了正确的术语,但我很好奇它是如何确定在Java充满时增加Java的大小的多少?我尝试过搜索,但我并没有真正想出任何有用的东西.
所以,如果我有类似的东西
List l = new ArrayList(1);
l.add("1");
l.add("2");
它如何确定增加列表大小的程度?它总是一个设定值,如果是这样,那个值是多少?如果它不同,我也会对BitSet的这些信息感兴趣.
谢谢,让我知道我是否应该澄清任何问题.
我动态地分配了一个结构,在概念上非常类似于矩阵,以保存一组字符串.我在尝试释放内存时遇到了问题.我的代码看起来像这样:
# include <stdio.h>
# include <string.h>
# include <malloc.h>
# define SIZE 2
typedef struct fork{
char** dataPointersArray;
char* dataArray;
}fork;
int main(int argc, char* argv[]){
fork forkDS;
int i;
char* dataArrayPtr;
unsigned char data[255] = "some data"; /* this is actually a function's output */
int PtrIndex;
/* allocate memory for the arrays */
forkDS.dataPointersArray = (char**) calloc(SIZE ,sizeof(char*));
if(forkDS.dataPointersArray == NULL){
printf("couldn't allocate memory \n");
}
forkDS.dataArray = (char*) calloc(SIZE, 255);
if( forkDS.dataArray == NULL){
free(forkDS.dataPointersArray);
printf("couldn't …Run Code Online (Sandbox Code Playgroud) 我一直在尝试调整这个数组的大小,它编译得很好,但是当我运行它时,我得到了这个非常长的错误信息.
这是函数的代码:
void arrayClass_Namespace::arrayClass::resize(int newSize)
{
std::cout << "\nPlease input a new size for the array: ";
std::cin >> newSize;
assert(newSize < MAX_SIZE);
int *resize_arr = new int[newSize];
for(int index = 0; index < size; index++)
{
resize_arr[index] = arr[index];
}
size++;
arr = resize_arr;
delete[] resize_arr;
}
Run Code Online (Sandbox Code Playgroud)
这是现在完美运行的更新代码:
void arrayTools_GabriellaRamirez::arrayTools::resize(int newSize)
{
std::cout << "\nPlease input a new size for the array: ";
std::cin >> newSize;
assert(newSize < MAX_SIZE);
int *resize_arr = new int[newSize];
for(int index = 0; index …Run Code Online (Sandbox Code Playgroud) 我面临一个奇怪的问题。我编写了一个 Parent 抽象类(实现纯虚拟 test() 方法)及其 Child 类(实现 test() 方法)。
class Parent
{
public :
Parent();
virtual ~Parent() = default;
virtual bool test() const = 0;
};
class Child : public Parent
{
public :
bool test() const;
};
Run Code Online (Sandbox Code Playgroud)
然后,我编写了一个“Grid”类,它应该包含指向 Parent 的指针的二维数组。该数组是使用向量库完成的:“_cells”是指向 Parent 的指针的宽度*高度向量。_cells 在 Grid 对象构造期间使用动态分配进行填充,并在析构函数中释放。Operator() (int a, int b) 被重载,以便能够使用以下模式调用 Parent 对象:myGrid(x,y)。
class Grid
{
int _w, _h;
std::vector<Parent*> _cells;
public :
Grid(int w = 0, int h = 0);
~Grid();
Parent* &operator()(int x, int y);
private :
void …Run Code Online (Sandbox Code Playgroud) c++ pure-virtual segmentation-fault dynamic-allocation c++11
所以我被告知要创建一个数组,该数组将接受来自用户的10个整数,将其存储到数组中,并使用指针气泡排序按升序对这些值进行排序.
我相信我已经成功地做了这么多,但我在第二部分遇到了麻烦.
"动态分配另一个10个整数的数组.将元素从第一个复制到第二个,但顺序相反(即降序).按顺序显示第一个和第二个数组的元素,并释放动态分配的数组."
我能够按顺序显示第一个数组,我知道要释放数组你必须使用delete函数,但我不太清楚如何构造动态数组.
*我没有包含这些功能,因为我认为这部分不是必需的,但如果我这样做,那么我也会发布它们.
提前感谢任何建议和澄清.
#include <iostream>
using namespace std;
void sortArray(int * , int);
void showArray(const int * , int);
int binarySearch(const int *, int, int);
int main(void)
{
int const MAX_NUM = 10;
int numbers [MAX_NUM];
int counter;
int findval;
int index;
char again;
cout<< "Please enter 10 integer values."<< endl;
for(counter=0; counter< MAX_NUM ; counter++)
{
cout << "Enter a value for "<< counter+1 << ": ";
cin >> *(numbers+counter);
}
sortArray(numbers, 10);
cout << endl …Run Code Online (Sandbox Code Playgroud) 当我用C malloc系列函数动态分配内存时,是否有一些规则(来自C标准或操作系统的内部工作)关于该内存的初始值是什么?
int* ptr = malloc(sizeof (*ptr));
bool b = *ptr == 0; // always true?
Run Code Online (Sandbox Code Playgroud) 预备要点:我需要做什么改变才能在我的记录中显示正确的头/尾值?
编辑1:一旦有超过1个线程,Record内部的整数数组似乎会填充随机值.
我一直试图调试这几天了.我的代码已完成并完全执行.(众所周知,我是一名学生而不是假装成为一名专业程序员.)
在我的多线程硬币投掷程序中,我试图捕获每个线程中出现头或尾的次数.我正在翻硬币总共100,000,000次.(一亿.)
记录类中数组中的元素没有正确累积.我知道我不需要使用互斥锁,因为每个线程都访问一个独特的线程内存位置.但是线程正在更新错误的值,这不应该发生.
以下是我的调试示例.
下面是完全可执行的代码示例,然后是示例输出.此外,这里还链接到Github上的完整代码:
#include <iostream>
#include <thread>
#include <vector>
#include <mutex>
#include <random>
#include <algorithm>
#include <time.h>
#include <strstream>
#include <random>
using namespace std;
default_random_engine dre;
uniform_int_distribution<int> Tosser(0,1);
struct Record {
Record();
~Record();
int numThreads;
int *heads;
int *tails;
time_t startTime;
time_t stopTime;
time_t duration;
int timesToToss;
};
Record::Record(): numThreads(0), heads(NULL), tails(NULL), startTime(NULL), stopTime(NULL), timesToToss(0){}
Record::~Record() {
startTime = NULL;
stopTime = NULL;
duration = NULL;
timesToToss = NULL;
delete [] heads; …Run Code Online (Sandbox Code Playgroud) 假设我有一个指针char* ptr分配了内存和另一个指针char* arr = ptr
释放内存arr后会发生什么ptr。
让函数为:
char* foo()
{
char* ptr = new char [100];
char* arr = ptr;
delete [] ptr;
return arr;
}
Run Code Online (Sandbox Code Playgroud)
我可以使用这个返回值吗?
它会导致任何编译时/运行时错误吗?或者其他任何东西。
或者如果函数是
char* foo()
{
char* ptr = new char [100];
char* arr = ptr;
delete [] arr;
return ptr;
}
Run Code Online (Sandbox Code Playgroud)
我想以前的输出不会有任何变化,但会有任何变化吗?
如果我有一堂课会发生什么
class Pointer
{
public:
char* ptr;
Pointer()
{
ptr= new char [100];
}
~Pointer()
{
delete [] ptr;
}
};
Run Code Online (Sandbox Code Playgroud)
和功能
Pointer foo() …Run Code Online (Sandbox Code Playgroud) 伙计我正在使用C编程,尝试动态分配类型char,如下所示:
char **word1 = malloc(sizeof(char *)* 1);
char **word2 = malloc(sizeof(char *) * 1);
Run Code Online (Sandbox Code Playgroud)
但它产生了一个错误:从'void*'无效转换为'char**'[-fpermissive]
谢谢每一个帮助我的人.
我无法弄清楚如何将新对象存储到向量中,并能够将该信息拉出来.
我想要做的是,在一系列对象中存储文件中的不同数据,然后浏览这些对象并提取信息.
我正在寻找这样的东西:
vector<myClass> list;
while( i < nFiles)
{
myClass *temp = new myClass;
list.push_back(temp);
temp->setSomething();
i++;
}
Run Code Online (Sandbox Code Playgroud)
我希望每个nFile循环都有一个不同的对象,所以我以后可以遍历每个对象并从每个对象中提取信息.
我试过把温度推到一个矢量但它给我的只是错误.
我正在尝试以编程方式正确吗?我无法理解这一点.任何形式的帮助将不胜感激.谢谢.
c++ ×6
c ×3
arrays ×2
pointers ×2
bitset ×1
c++11 ×1
class ×1
collections ×1
dereference ×1
heap ×1
heap-memory ×1
java ×1
list ×1
malloc ×1
pure-virtual ×1
sorting ×1
vector ×1