我试图使用指针循环一个整数数组,但我得到一个奇怪的值..
int nums[1] = { 1 };
int *p = nums;
while(*p != NULL) {
cout << " LOOPING, p is " << *p << endl;
p++;
}
Run Code Online (Sandbox Code Playgroud)
当我跑步时,我得到下一个输出:
LOOPING, p is 1
LOOPING, p is -858993460
LOOPING, p is 4454504
LOOPING, p is 3032019
Run Code Online (Sandbox Code Playgroud)
为什么我会得到那些奇怪的价值观?我应该只看到"1",因为我循环直到我得到NULL指针,并在每个循环上移动下一个指针.
我这里有一个noob问题,无法理解这里的错误.我在这里有以下代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Run Code Online (Sandbox Code Playgroud)
void copyFrom(char * mStr, char * str);
Run Code Online (Sandbox Code Playgroud)
int main() {
char * srcStr = "Robert bought a good ford car";
char * dstStr = NULL;
copyFrom(srcStr, dstStr);
printf("The string copied here is %s", dstStr);
return 0;
}
void copyFrom(char * str, char * mStr)
{
if(!mStr) {
mStr = (char *)malloc((strlen(str)) + 1);
if(mStr == NULL)
return;
}
while(*mStr++ = *str++) {
;
}
mStr[strlen(str)] = '\0';
}
Run Code Online (Sandbox Code Playgroud)
这不会复制字符串,但是如果使用数组而不是dstStr的char指针,那么一切正常.
你能告诉我这里有什么问题吗?
我可以在C中这样做吗?Valgrind抱怨realloc产生无效免费?
int main(){
void* mem;
// Do stuff
recurfunc(123, mem);
// Do stuff
if(mem)
free(mem);
return 0;
}
void recurfunc(int x, void* mem){
.....
// Do stuff
mem = realloc(mem, newsize);
// Do stuff
recurfunc(x, mem);
.....
}
Run Code Online (Sandbox Code Playgroud) 在没有实现排序的情况下,对java进行排序的最佳方法是什么(首先基于x坐标,如果x在y坐标上是相同的,如果y是相同的则基于z坐标,依此类推.)算法?
在c ++中,它可以在成对的帮助下非常容易地完成(如下所示).
对于2D:
Vector < pair < int,int > > plane;
sort(plane.begin(),plane.end())
Run Code Online (Sandbox Code Playgroud)
对于3D:
Vector < pair < int,pair < int,int > > > space;
sort(space.begin(),space.end());
Run Code Online (Sandbox Code Playgroud)
提前致谢.山塔努
我需要C中的大数组来存储一些数据.我正在研究的事情与DNA测序有关.我正在使用Visual Studio 2013.
首先,我尝试使用全局静态变量
static oligo SPECTRUM[C1][C2]
Run Code Online (Sandbox Code Playgroud)
寡核苷酸结构含有8个整数,C1为100000和C2 500.
但是视觉上说阵列很大.然后我问Google,他说使用矢量是个好主意.所以我通过用下面的代码替换上面的代码来切换到这些
static std::vector<std::vector<oligo>> SPECTRUM;
Run Code Online (Sandbox Code Playgroud)
据说在使用之前调整矢量是一件好事,所以我做了:
SPECTRUM.resize(C1);
for (int i = 0; i < C1; i++)
{
SPECTRUM[i].resize(C2);
}
Run Code Online (Sandbox Code Playgroud)
但现在我在执行上面的代码时抛出了运行时异常(调整大小)
An unhandled exception of type 'System.Runtime.InteropServices.SEHException' occurred in ConsoleApplication1.exe
Run Code Online (Sandbox Code Playgroud)
在文件xmemory0中.视觉显示此处抛出异常
else if (((size_t)(-1) / sizeof (_Ty) < _Count)
|| (_Ptr = ::operator new(_Count * sizeof (_Ty))) == 0)
_Xbad_alloc(); // report no memory
Run Code Online (Sandbox Code Playgroud)
我还想让你知道,我的计算机上有4 GB RAM可用,我估计我的程序不应该使用超过1 GB的RAM.