对于某些函数我想在函数中创建一个字符串的副本,然后操纵它 - 由于一些奇怪的原因,我不能strcpy工作(给我一个分段错误) - 我也尝试将arg作为字符串传递,这也不起作用(g ++抛出一个错误,说它期望一个char*)
#include <iostream>
#include <cstring>
using namespace std;
void copy_string(char* stri);
int main ()
{
copy_string("sample string");
return 0;
}
void copy_string(char* stri) {
char* stri_copy;
strcpy(stri_copy, stri);
cout << "String: " << stri_copy;
}
Run Code Online (Sandbox Code Playgroud)
我不确定我明白为什么会这样.
所以我的两个问题是:
谢谢!
我创建了一个购物车网站,我有两个抱怨同样的问题,但我无法跟踪主要问题...他们说他们收到消息" 操作中止,Internet Explorer无法打开页面 "无论何时他们导航到任何页面,然后单击"确定"显示"页面无法显示".
有人可以帮帮我吗?这可能是背后的原因..
在IE-6,7中获得此问题.
编辑
我之前从未报告此错误.但现在我经常收到这个错误,我的网站是现场发生的,它是一个osCommerce网站,我不知道在哪里可以在网站上进行变通办法?
这是我的代码:
void reverseStr(char *str)
{
if (str == NULL) return;
int i=0, j=strlen(str) -1;
while(i<j)
{
char temp = str[j]; //i think this is the cause of the problem
str[j] = str[i];
str[i] = temp;
i++;
j--;
}
}
Run Code Online (Sandbox Code Playgroud)
所以这里是它的所在:
int main()
{
char *str = "Forest Gump";
reverseStr(str);
cout << str;
}
Run Code Online (Sandbox Code Playgroud)
这是我的错误:
/Applications/TextMate.app/Contents/SharedSupport/Bundles/C.tmbundle/Support/bin/bootstrap.sh:line 7:1931总线错误"$ 3".out
有什么想法吗?提前致谢.
我已经嗅探了一个IGMP数据包,现在我想在python的帮助下发送它。有什么办法可以发送像
0x0000 01 00 5E 00 43 67 00 02-B3 C8 7F 44 81 00 00 DE ..^.Cg..??D?..?
0x0010 08 00 46 00 00 20 00 01-00 00 01 02 36 4C C0 A8 ..F.. ......6L??
0x0020 00 7B EA 00 43 67 94 04-00 00 16 00 BC 97 EA 00 .{?.Cg”.....?—?.
0x0030 43 67 Cg
Run Code Online (Sandbox Code Playgroud)
没有像流浪者这样的数据包生成器?
UPD好吧,我尝试使用原始套接字,如下所示:
dst = '234.0.67.103'
# Open a raw socket.
s = socket.socket(socket.AF_INET, socket.SOCK_RAW,2)
res=''
temp='01 00 5E 00 43 67 00 02 …Run Code Online (Sandbox Code Playgroud) 所以我在我的程序中使用malloc然后在程序内部的方法中重新分配.在我多次调用这个方法之后,我会得到一个"Segmentation fault(core dumped)".
经过进一步检查,我意识到由于某种原因,当我的指针从0x25d7d60或0x223fae0(或任何由7位数字(0xHHHHHHH)表示的地址)变为0x7f47d370a010(超过7位数)时,会从realloc调用中抛出一个段错误,realloc甚至不会返回NULL.我通过简单地使用malloc然后使用memcpy来修复此问题.但是我很困惑,为什么会发生这种情况,并希望看看stackoverflow的任何用户是否能够解释为什么会发生这种情况.
谢谢
这是相关代码:
unsigned int* myArray;
unsigned int num_ints;
int main()
{
num_ints = 100;
if((myArray =(unsigned int*) malloc(sizeof(unsigned int)*(num_ints)*3))==NULL)
{
std::cout << "Malloc failed!" << std::endl;
return false;
}
.
.
.
//This called when n key is pressed (code left out)
methodName();
return true;
}
void methodName()
{
if((myArray =(unsigned int*) realloc(myArray,sizeof(unsigned int)*(num_ints*4)*3))==NULL)
{
std::cout << "Realloc failed!" << std::endl;
exit(0);
}
}
Run Code Online (Sandbox Code Playgroud) 所以我试图创建一个文件并从中读取,即使它是空的。因此,下次运行该程序时,该文件将已经存在并且其中会有数据。
#this should create the file
filename = open("roombooking.bin","wb")
#this should load the data into a list but since its first created it should be an empty list
monday=pickle.load(open("roombooking.bin","rb"))
Run Code Online (Sandbox Code Playgroud)
这是我得到的错误
Traceback (most recent call last):
monday=pickle.load(open("roombooking.bin","rb"))
File "C:\Python27\lib\pickle.py", line 1378, in load
return Unpickler(file).load()
File "C:\Python27\lib\pickle.py", line 858, in load
dispatch[key](self)
File "C:\Python27\lib\pickle.py", line 880, in load_eof
raise EOFError
EOFError
Run Code Online (Sandbox Code Playgroud) 我有这个简单的代码。
int[][] array = {
new int[] {1,2,3},
new int[] {4,5,6},
};
int yLength = array.GetLength(1);
Run Code Online (Sandbox Code Playgroud)
但是最后一行抛出了一个超出范围的异常。这是为什么?GetLength在这种情况下它不应该返回 3 吗?还是 GetLength 仅适用于方形数组?(T[,] 数组)?
如果是这样,我如何使用循环遍历锯齿状数组for?
#include <iostream>
using namespace std;
int main() {
char choice;
int solution, num1, num2;
cout << "Menu";
cout << "\n========";
cout << "\n+) Add";
cout << "\n-) subtract";
cout << "\n*) Multiplication";
cout << "\n/) Division";
cout << "\nx) Exit";
cout << endl;
cout << "\nEnter your choice: ";
cin >> choice;
switch (choice) {
case '+':
cout << "\nEnter the operands: ";
cin >> num1, num2;
solution = num1 + num2;
cout << num1 << '+' << num2 << …Run Code Online (Sandbox Code Playgroud) 当它们在内存中表示时,C++对象是否与C结构相同?
例如,使用C,我可以这样做:
struct myObj {
int myInt;
char myVarChar;
};
int main() {
myObj * testObj = (myObj *) malloc(sizeof(int)+5);
testObj->myInt = 3;
strcpy((char*)&testObj->myVarChar, "test");
printf("String: %s", (char *) &testObj->myVarChar);
}
Run Code Online (Sandbox Code Playgroud)
我认为C++不允许+为内置char *类型重载运算符.
所以我想创建自己的轻量级字符串类,它没有额外的开销std::string.我认为std::string是连续的代表:
(int)length, (char[])data
Run Code Online (Sandbox Code Playgroud)
我想要完全相同的功能,但没有前缀长度(节省8字节开销).
这是我用来测试的代码,但它导致了段错误
#include <iostream>
using namespace std;
class pString {
public:
char c;
pString * pString::operator=(const char *);
};
pString * pString::operator=(const char * buff) {
cout << "Address of this: " << (uint32_t) this << …Run Code Online (Sandbox Code Playgroud)