功能是这样的:
Set::Set(Multinumber* tempArray[], int tempSize)
{
numElements = tempSize;
capacity = tempSize*2;
setArray = new Multinumber*[capacity];
for (int i=0; i<numElements; i++)
{
addElement(tempArray[i]);
}
}
Run Code Online (Sandbox Code Playgroud)
变量setArray在我的标题中声明为Multinumber**类型
每当我用它调用它时会出现段错误:
Multinumber* carr[2];
carr[0]=c4;
carr[1]=c5;
Set setb(carr,2);
Run Code Online (Sandbox Code Playgroud)
c4和c5已经被声明为指向正确类型的对象的指针.
任何帮助将非常感激.
编辑:下面的代码是addElement函数(为缩进道歉)
const Set Set::operator+(const Set& rhs) const
{
Set result;
int i=0, j=0;
while ((i < numElements) && (j < rhs.numElements))
{
Multinumber* toadd=new Multinumber;
toadd=*(setArray[i]) + *(rhs.setArray[j]);
result.addElement(toadd);
i++;
j++;
}
while ((i < numElements))
{
result.addElement(setArray[i]);
i++;
}
while ((j < rhs.numElements))
{ …Run Code Online (Sandbox Code Playgroud) 这个问题困扰了我一段时间.
int* a = new int[n]例如,如果我这样做,我只有一个指向数组a开头的指针,但C/C++是如何知道的n?我知道如果我想将这个数组传递给另一个函数,那么我必须用它传递数组的长度,所以我猜C/C++并不知道这个数组有多长.
我知道我们可以char*通过查找NUL终结符来推断字符数组的结尾.但其他数组是否有类似的机制,比如int?同时,char可以不仅仅是一个字符 - 您也可以将其视为整数类型.那么C++如何知道这个数组的结束呢?
当我开发嵌入式Python时,这个问题开始让我更加困扰(如果你不熟悉嵌入式python,你可以忽略这个段落并回答上面的问题.我仍然会欣赏它).在Python中有一个"ByteArray",将这个"ByteArray"转换为C/C++的唯一方法是使用PyString_AsString()将其转换为char*.但是如果这个ByteArray中有0,那么C/C++会认为char*数组会提前停止.这不是最糟糕的部分.最糟糕的是,比方说我做了
char* arr = PyString_AsString(something)
void* pt = calloc(1, 1000);
Run Code Online (Sandbox Code Playgroud)
如果st恰好从0开始,那么C/C++几乎可以保证消灭arr中的所有内容,因为它认为arr在出现NULL之后就会结束.然后它可能只是通过为pt分配一个内存树干来消灭arr中的所有内容.
非常感谢您的宝贵时间!对此,我真的非常感激.
是否可以创建一个泛型类作为任何类型的对象的动态数组?我想要一个基本上这样做的课程:
MyObj * obj1 = new MyObj();
MutableArray * arr = new MutableArray();
arr->addObject(obj1);
MyObj * obj2 = arr->objectAtIndex(0);
// obj1 and obj2 now points to the same object
Run Code Online (Sandbox Code Playgroud)
这就是代码的样子.我知道这不起作用,但你明白了.我需要的是一个对象的泛型类型.数组本身只是由指针组成,因此对象的大小无关紧要,对吧?
那么,这可能吗?
.H文件
class MutableArray
{
private:
class * objs;
int length;
int capacity;
public:
MutableArray();
void add(class * obj);
class objectAtIndex(int index);
};
Run Code Online (Sandbox Code Playgroud)
CPP文件
MutableArray::MutableArray()
{
length = 0;
capacity = 0;
}
void MutableArray::add(class * obj)
{
if(length >= capacity)
{
this->enlarge();
}
objs[length] = obj;
length++;
}
void …Run Code Online (Sandbox Code Playgroud) 阅读一些文献,我能够掌握realloc一个void指针和一个大小变量,并重新分配void指针所指向的块的内存.
如果realloc在具有int *字符大小的整数指针()上调用会发生什么?反之亦然.
这有什么可能的应用?(一个例子肯定会有所帮助.)
我开始使用malloc进行更多练习,尽管它正确执行的代码没有任何问题.
int *arr = (int *)malloc(x * y * sizeof(int));
int i, j, r;
for(i=0; i<x; i++){
for(j=0; j<y; j++){
r = 0 + rand() % 7;
*(arr + i*y + j) = r;
//I dont understand the left hand portion of the above line.
//x and y are both 5000
Run Code Online (Sandbox Code Playgroud)
我在网上发现它,在我找到它之前,我曾尝试做同样的事情,但我猜我的语法错了.无论如何,我需要帮助理解旁边有注释的行
c malloc pointers multidimensional-array dynamic-memory-allocation
我不知道我做错了什么,或者我的概念是否有些错误
#include<stdio.h>
#include<stdlib.h>
int main()
{
int *p;
p=calloc(3,sizeof(int));
p[0]=10;
p[1]=15;
p[2]=30;
printf("\n%d\n%p\n%d\n%p\n%d\n%p\n\n",p[0],p,p[1],p+1,p[2],p+2);
free(p);
//p=NULL;
printf("\n%d\n%p\n%d\n%p\n%d\n%p\n\n",p[0],p,p[1],p+1,p[2],p+2);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
当第二个,printf()运行时,它显示p [2] = 30,而p [0] = p [1] = 0(在gcc ubuntu和Code :: Blocks窗口中的一些任意值).我有两个问题.
我是初学者所以请耐心等待.我用malloc()尝试了同样的事情,同样的事情发生了.
我是动态内存管理的新手,并使用Fsanitise标志来查找内存管理问题.我不能使用vector来存储数据 - 我需要使用原始数组,以及"new"和"delete"来管理堆对象.
当我尝试运行EuclideanVectorTester编译的程序但是不确定问题是什么时,我得到以下错误,请有人开导我吗?
weill % make
g++ -std=c++14 -Wall -Werror -O2 -fsanitize=address -c EuclideanVectorTester.cpp
g++ -std=c++14 -Wall -Werror -O2 -fsanitize=address -c EuclideanVector.cpp
g++ -fsanitize=address EuclideanVectorTester.o EuclideanVector.o -o EuclideanVectorTester
weill % ./EuclideanVectorTester
1
=================================================================
==15341==ERROR: AddressSanitizer: heap-buffer-overflow on address 0xb59007e0 at pc 0x8048ca7 bp 0xbfb47388 sp 0xbfb4737c
WRITE of size 8 at 0xb59007e0 thread T0
#0 0x8048ca6 in main (/tmp_amd/kamen/export/kamen/3/z3386180/cs6771/evec/EuclideanVectorTester+0x8048ca6)
#1 0xb6ecae45 in __libc_start_main (/lib/i386-linux-gnu/i686/cmov/libc.so.6+0x16e45)
0xb59007e0 is located 0 bytes to the right of 16-byte region [0xb59007d0,0xb59007e0)
allocated by thread …Run Code Online (Sandbox Code Playgroud) c++ constructor heap-memory dynamic-memory-allocation address-sanitizer
我在一个线程中分配一些内存.当我打电话pthread_detach(pthread_self());这是分配给这个线程通过Linux的内存应该被释放后,线程被终止pthread_exit();或pthread_cancel().
我的问题是,我是否还应该释放我用malloc分配的内存?另外,如果线程中的其他函数分配了一些空间,我是否也应该释放这个空间,即使线程终止并 pthread_detach(pthread_self());在线程中调用?
void *test_thread(void * arg)
{
pthread_detach(pthread_self());
int *c = malloc(2048);
pthread_exit(NULL);
}
Run Code Online (Sandbox Code Playgroud) 我正在定义一个新的C++类,其what方法返回一个char*类型,其值为整数作为构造函数传递.
最初我是用stringclass 做的,并从中返回字符串数据what.
然后我尝试char*在以下代码中使用type:
/* Define the exception here */
class BadLengthException: public exception
{
public:
BadLengthException(int strLength)
{
strLen = strLength;
res = (char*)malloc(strLength+1);
int resultSize = sprintf(res, "%d", strLen);
}
~BadLengthException() throw()
{
free(res);
}
virtual const char* what() const throw()
{
return res;
}
private:
int strLen;
char* res;
};
Run Code Online (Sandbox Code Playgroud)
但是在释放malloc分配的变量时我遇到了问题:它给出了这个异常:
pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug …Run Code Online (Sandbox Code Playgroud) malloc()函数形成一个内存块(例如,将20个字节类型转换为int),那么如何将其用作int类似于calloc()函数的块数组?它不应该用于int在整个20个字节(20 * 8位)中仅存储一个值吗?
c ×6
c++ ×5
malloc ×3
free ×2
pointers ×2
calloc ×1
conceptual ×1
constructor ×1
destructor ×1
exception ×1
heap-memory ×1
linux ×1
python ×1