- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我最近刚刚在类中了解了指针,并且有一个作业,其中程序允许用户通过测试四个函数来实现指针和动态内存分配。
我遇到问题的唯一地方是最后一个函数 subArray。这应该仅显示由起始索引和长度值设置指定的数组的特定部分。
该程序在 Xcode 上运行良好,给出正确的输出并返回 0。这是 Xcode 上的输出:
Testing isSorted:
Test data for array 1: 1 2 3 4 5 6 7 8
Expected result: true
Actual result: true
Test data for array 2: 8 7 6 5 4 3 2 1
Expected result: false
Actual result: false
Test data for array 3: 1 2 3 5 4 6 7 8
Expected result: false
Actual result: false
Testing chain for 15.46 inches:
Expected result: 15.46 feet: 4 inches: 5
Actual result: 15.46 feet: 4 inches: 5
Testing grow:
Test data: 1 2 3 4 5 6 7 8 9
Expected result: 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9
Actual result: 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9
Testing subArray:
Test data: 1 2 3 4 5 6 7 8 9
Start: 5 Length: 4
Expected result: 6 7 8 9
Actual result: 6 7 8 9
Program ended with exit code: 0
但是,我必须在 Linux 上进行编译才能获得满分,并且当我在 Linux 上运行代码时遇到以下问题:
Error in `./exe': free(): invalid pointer: 0x0000000000678060 ***
输出与 Xcode 完全相同,除了测试 subArray 函数中的最后一个“实际结果:”行。
Testing subArray:
Test data: 1 2 3 4 5 6 7 8 9
Start: 5 Length: 4
Expected result: 6 7 8 9
*** Error in `./run': free(): invalid pointer: 0x0000000000678060 ***
======= Backtrace: =========
/lib64/libc.so.6(+0x7c619)[0x7f1da7cd3619]
/lib64/libc.so.6(+0x7d1bb)[0x7f1da7cd41bb]
/lib64/libc.so.6(+0x7dba5)[0x7f1da7cd4ba5]
/lib64/libc.so.6(__libc_malloc+0x4c)[0x7f1da7cd710c]
/lib64/libstdc++.so.6(_Znwm+0x1d)[0x7f1da85911bd]
/lib64/libstdc++.so.6(_Znam+0x9)[0x7f1da85912b9]
./run[0x401376]
./run[0x401327]
./run[0x401097]
/lib64/libc.so.6(__libc_start_main+0xf5)[0x7f1da7c78c05]
./run[0x4009c9]
======= Memory map: ========
00400000-00402000 r-xp 00000000 00:2e 9715713 /home/Students/klw231/run
00601000-00602000 r--p 00001000 00:2e 9715713 /home/Students/klw231/run
00602000-00603000 rw-p 00002000 00:2e 9715713 /home/Students/klw231/run
00678000-006bb000 rw-p 00000000 00:00 0 [heap]
7f1da0000000-7f1da0021000 rw-p 00000000 00:00 0
7f1da0021000-7f1da4000000 ---p 00000000 00:00 0
7f1da7c57000-7f1da7e0f000 r-xp 00000000 fd:00 67288141 /usr/lib64/libc-2.17.so
7f1da7e0f000-7f1da800f000 ---p 001b8000 fd:00 67288141 /usr/lib64/libc-2.17.so
7f1da800f000-7f1da8013000 r--p 001b8000 fd:00 67288141 /usr/lib64/libc-2.17.so
7f1da8013000-7f1da8015000 rw-p 001bc000 fd:00 67288141 /usr/lib64/libc-2.17.so
7f1da8015000-7f1da801a000 rw-p 00000000 00:00 0
7f1da801a000-7f1da802f000 r-xp 00000000 fd:00 67161030 /usr/lib64/libgcc_s-4.8.5-20150702.so.1
7f1da802f000-7f1da822e000 ---p 00015000 fd:00 67161030 /usr/lib64/libgcc_s-4.8.5-20150702.so.1
7f1da822e000-7f1da822f000 r--p 00014000 fd:00 67161030 /usr/lib64/libgcc_s-4.8.5-20150702.so.1
7f1da822f000-7f1da8230000 rw-p 00015000 fd:00 67161030 /usr/lib64/libgcc_s-4.8.5-20150702.so.1
7f1da8230000-7f1da8331000 r-xp 00000000 fd:00 67657412 /usr/lib64/libm-2.17.so
7f1da8331000-7f1da8530000 ---p 00101000 fd:00 67657412 /usr/lib64/libm-2.17.so
7f1da8530000-7f1da8531000 r--p 00100000 fd:00 67657412 /usr/lib64/libm-2.17.so
7f1da8531000-7f1da8532000 rw-p 00101000 fd:00 67657412 /usr/lib64/libm-2.17.so
7f1da8532000-7f1da861b000 r-xp 00000000 fd:00 67150761 /usr/lib64/libstdc++.so.6.0.19
7f1da861b000-7f1da881b000 ---p 000e9000 fd:00 67150761 /usr/lib64/libstdc++.so.6.0.19
7f1da881b000-7f1da8823000 r--p 000e9000 fd:00 67150761 /usr/lib64/libstdc++.so.6.0.19
7f1da8823000-7f1da8825000 rw-p 000f1000 fd:00 67150761 /usr/lib64/libstdc++.so.6.0.19
7f1da8825000-7f1da883a000 rw-p 00000000 00:00 0
7f1da883a000-7f1da885b000 r-xp 00000000 fd:00 67150948 /usr/lib64/ld-2.17.so
7f1da8a47000-7f1da8a4c000 rw-p 00000000 00:00 0
7f1da8a58000-7f1da8a5b000 rw-p 00000000 00:00 0
7f1da8a5b000-7f1da8a5c000 r--p 00021000 fd:00 67150948 /usr/lib64/ld-2.17.so
7f1da8a5c000-7f1da8a5d000 rw-p 00022000 fd:00 67150948 /usr/lib64/ld-2.17.so
7f1da8a5d000-7f1da8a5e000 rw-p 00000000 00:00 0
7ffc48603000-7ffc48624000 rw-p 00000000 00:00 0 [stack]
7ffc4867b000-7ffc4867d000 r-xp 00000000 00:00 0 [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0 [vsyscall]
Actual result: Aborted
我知道函数中某处的指针肯定出了问题,但我尝试了许多可能的解决方案,并且希望更好地了解如何解决此问题。提前致谢!
#include <iostream>
#include <stdlib.h>
using namespace std;
// Function prototypes
bool isSorted(int *arr, int size);
double chain(int totalInches, int *feet, int *inches);
int *grow(int *arr, int size);
int *subArray(int *arr, int start, int length);
int *duplicateArray(int *arr, int size);
void showArray( int *arr, int size);
int main()
{
int *arr = NULL; //to dynamically allocate an array
// Test isSorted
int size = 8;
int testArray[] = {1, 2, 3, 4, 5, 6, 7, 8};
cout << endl;
cout << "Testing isSorted: \n" << endl;
cout << "Test data for array 1: ";
showArray(testArray, size);
cout << "\nExpected result: true " << endl;
cout << "Actual result: " << boolalpha << isSorted(testArray, size);
int testArray2[]= {8, 7, 6, 5, 4, 3, 2, 1};
cout << "\nTest data for array 2: ";
showArray(testArray2, size);
cout << "\nExpected result: false" << endl;
cout << "Actual result: " << boolalpha << isSorted(testArray2, size);
int testArray3[] = {1, 2, 3, 5, 4, 6, 7, 8};
cout << "\nTest data for array 3: ";
showArray(testArray3, size);
cout << "\nExpected result: false" << endl;
cout << "Actual result: " << boolalpha << isSorted(testArray3, size);
cout << endl;
// Test chain
int totalInches = 53;
int inches;
int feet;
double expected = chain(totalInches, &feet, &inches);
cout << "\n\nTesting chain for " << expected << " inches: " << endl;
cout << "\nExpected result: " << expected << " feet: "<< feet << " inches: " << inches << endl;
cout << "Actual result: " << chain(totalInches, &feet, &inches)
<< " feet: " << feet << " inches: " << inches << "\n" << endl;
cout << endl;
// Test grow
int testArray4[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
size = 9; // Size is now larger
cout << "Testing grow: " << endl;
cout << "\nTest data: ";
showArray(testArray4, size);
cout << "\nExpected result: 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 " << endl;
cout << "Actual result: ";
showArray(grow(testArray4, size),size*2);
cout << endl;
// Test subArray
int start = 5;
int length = 4;
cout << "\n\nTesting subArray: " << endl;
cout << "\nTest data: ";
showArray(testArray4, size);
size = length;
cout << "\nStart: " << start << " " << "Length: " << length << endl;
cout << "Expected result: 6 7 8 9 " << endl;
cout << "Actual result: ";
int *c = subArray(testArray4, start, length);
for(int i=0; i<size; i++)
{
cout << *(c + i) << " ";
}
cout << endl;
delete [] c;
c = NULL;
delete arr; // Releasing dynamically allocated space
arr = NULL;
return 0;
}
//************************************************************************
// bool isSorted: determines whether or not elements in the array are sorted
//
// int *arr: A pointer to a dynamically allocated array
// int size: The number of elements in the array
// returns: This function returns either a true or false boolean value.
//************************************************************************
bool isSorted(int *arr, int size)
{
for(int index = 0; index < size - 1; index ++)
{
if(*(arr + index) > *(arr + index + 1))
{
return false;
}
}
return true;
}
//************************************************************************
// double chain: takes in values from main and performs math operations on them
//
// int totalInches: Adds together feet and inches
// int *feet: A pointer to it's respective value in main
// int *inches: A pointer to it's respective value in main
// returns: This function returns the result as an integer pointer.
//************************************************************************
double chain (int totalInches, int *feet, int *inches)
{
*feet = totalInches / 12;
*inches = totalInches % 12;
return *(feet) * 3.49 + *(inches) * 0.30;
}
//************************************************************************
// int *grow: creates a new array twice the size of the argument array
//
// int *arr: A pointer to a dynamically allocated array
// int size: The number of elements in the array
// returns: This function returns the new array as an integer pointer.
//************************************************************************
int *grow (int *arr, int size)
{
int *newArray;
newArray = new int[size*2]; //allocate new array
for(int i=0; i<(size*2); i+=1)
{
*(newArray + i) = *(arr + i / 2);
*(newArray + i + 1) = *(arr + i / 2);
}
return newArray;
}
//************************************************************************
// int *subArray:
//
// int *arr: A pointer to a dynamically allocated array
// int start: Sets the starting element to display
// int length: Sets how long the array segment to display is
// returns: This function returns the result as an integer pointer.
//************************************************************************
int *subArray(int *arr, int start, int length)
{
int *result = duplicateArray(arr + start, length);
return result;
}
//************************************************************************
// int *duplicateArray: copies array and returns only selected values
//
// int *arr: A pointer to a dynamically allocated array
// int size: The number of elements in the array
// returns: This function returns the new array as an integer pointer.
//************************************************************************
int *duplicateArray (int *arr, int size)
{
int *newArray;
if (size <= 0) //size must be positive
return NULL; //NULL is 0, an invalid address
newArray = new int [size]; //allocate new array
for (int index = 0; index < size; index++)
newArray[index] = arr[index]; //copy to new array
return newArray;
}
//************************************************************************
// void showArray: displays the contents of the array
//
// int *arr: A pointer to a dynamically allocated array
// int size: The number of elements in the array
// returns: This function is void and does not return a value.
//************************************************************************
void showArray( int *arr, int size)
{
for(int i=0; i<size; i++)
{
cout << *(arr + i) << " ";
}
return;
}
最佳答案
不幸的是,这个作业是试图教授 C++ 而最终教授 C 的一些突变变体的作业之一。您的问题的原因似乎是这段代码:
int *c = subArray(testArray4, start, length);
for(int i=0; i<size; i++)
{
cout << *(c + i) << " ";
}
返回的数组的长度仅为length
,但迭代器的长度达到size
,因此它会超出分配的末尾并最终触发未定义的行为。这是无效的。 “修复”是这样的:
int *c = subArray(testArray4, start, length);
for(int i=0; i<length; i++)
{
cout << *(c + i) << " ";
}
我通过删除大量不属于问题的代码来找到这个问题。当编译器提示 size
未定义时,我知道这就是问题所在,这是来自其他测试的值在这里被不当使用。这就是为什么保持代码尽可能有条理是关键,当您无法清楚地看到事物时,错误可能会悄悄出现。
更好的方法是为数组定义一个类
以及关联的迭代器以帮助导航。这不仅不那么困惑和不易出错,而且还有一个好处是它与标准库中现有的 C++ 容器工具完全兼容。如果您继续专业地从事这项工作,那么了解如何制作自己的标准库兼容类是一项非常重要的技能。
令我失望的是,很少有类(class)考虑这样做。
关于c++ - 指针/动态内存分配函数错误(Xcode 到 Linux),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49099187/
我刚接触 C 语言几周,所以对它还很陌生。 我见过这样的事情 * (variable-name) = -* (variable-name) 在讲义中,但它到底会做什么?它会否定所指向的值吗? 最佳答案
我有一个指向内存地址的void 指针。然后,我做 int 指针 = void 指针 float 指针 = void 指针 然后,取消引用它们以获取值。 { int x = 25; vo
我正在与计算机控制的泵进行一些串行端口通信,我用来通信的 createfile 函数需要将 com 端口名称解析为 wchar_t 指针。 我也在使用 QT 创建一个表单并获取 com 端口名称作为
#include "stdio.h" #include "malloc.h" int main() { char*x=(char*)malloc(1024); *(x+2)=3; --
#include #include main() { int an_int; void *void_pointer = &an_int; double *double_ptr = void
对于每个时间步长,我都有一个二维矩阵 a[ix][iz],ix 从 0 到 nx-1 和 iz 从 0 到 nz-1。 为了组装所有时间步长的矩阵,我定义了一个长度为 nx*nz*nt 的 3D 指针
我有一个函数,它接受一个指向 char ** 的指针并用字符串填充它(我猜是一个字符串数组)。 *list_of_strings* 在函数内部分配内存。 char * *list_of_strings
我试图了解当涉及到字符和字符串时,内存分配是如何工作的。 我知道声明的数组的名称就像指向数组第一个元素的指针,但该数组将驻留在内存的堆栈中。 另一方面,当我们想要使用内存堆时,我们使用 malloc,
我有一个 C 语言的 .DLL 文件。该 DLL 中所有函数所需的主要结构具有以下形式。 typedef struct { char *snsAccessID; char *
我得到了以下数组: let arr = [ { children: [ { children: [], current: tru
#include int main(void) { int i; int *ptr = (int *) malloc(5 * sizeof(int)); for (i=0;
我正在编写一个程序,它接受一个三位数整数并将其分成两个整数。 224 将变为 220 和 4。 114 将变为 110 和 4。 基本上,您可以使用模数来完成。我写了我认为应该工作的东西,编译器一直说
好吧,我对 C++ 很陌生,我确定这个问题已经在某个地方得到了回答,而且也很简单,但我似乎找不到答案.... 我有一个自定义数组类,我将其用作练习来尝试了解其工作原理,其定义如下: 标题: class
1) this 指针与其他指针有何不同?据我了解,指针指向堆中的内存。如果有指向它们的指针,这是否意味着对象总是在堆中构造? 2)我们可以在 move 构造函数或 move 赋值中窃取this指针吗?
这个问题在这里已经有了答案: 关闭 11 年前。 Possible Duplicate: C : pointer to struct in the struct definition 在我的初学者类
我有两个指向指针的结构指针 typedef struct Square { ... ... }Square; Square **s1; //Representing 2D array of say,
变量在内存中是如何定位的?我有这个代码 int w=1; int x=1; int y=1; int z=1; int main(int argc, char** argv) { printf
#include #include main() { char *q[]={"black","white","red"}; printf("%s",*q+3); getch()
我在“C”类中有以下函数 class C { template void Func1(int x); template void Func2(int x); }; template void
我在64位linux下使用c++,编译器(g++)也是64位的。当我打印某个变量的地址时,例如一个整数,它应该打印一个 64 位整数,但实际上它打印了一个 48 位整数。 int i; cout <<
我是一名优秀的程序员,十分优秀!