由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - C++ Q87: What is wrong with this swap function?
相关主题
为什么我这段简单的程序segment faultBloomberg(financial software developer)第一轮面试
怎么理解递归解决的“swap every two elements in a linked list”?[C++]请问哪些变量在heap创建?
paging和 segmentation有什么区别?问道面试体(software)
今天晚上要不然研究一下这题?问一个C的简单问题
分享今天做的一道基础题Bloomber 面试题
再问个简单的C问题问道C内存的题?
请教G家那题 abc123->a1b2c3【为什么我写的reverse string总出错】
virtual table存在memory的哪块啊?悲剧啊,没学cs真的很悲剧啊。
相关话题的讨论汇总
话题: int话题: swap话题: what话题: q87话题: c++
进入JobHunting版参与讨论
1 (共1页)
c**********e
发帖数: 2007
1
void swap(int *a, int *b) {
int *c;
*c=*a;
*a=*b;
*b=*c;
return;
}
int main() {
int x=5, y=10;
swap(&x, &y);
cout << x << " " << y << endl;
return 0;
}
The correct result is printed, but there is a segmentation fault. Is it
because *c is a local variable?
c**********e
发帖数: 2007
2
void swap(int *a, int *b) {
int c;
c=*a;
*a=*b;
*b=c;
return;
}
This one is always correct. But isn't the other one only assigned a value of
*c? I do not understand why the other one causes data Segmentation fault.
Help please.
c****x
发帖数: 61
3
*c没初始化

【在 c**********e 的大作中提到】
: void swap(int *a, int *b) {
: int c;
: c=*a;
: *a=*b;
: *b=c;
: return;
: }
: This one is always correct. But isn't the other one only assigned a value of
: *c? I do not understand why the other one causes data Segmentation fault.
: Help please.

m******e
发帖数: 353
4
没给c分配内存啊

【在 c**********e 的大作中提到】
: void swap(int *a, int *b) {
: int c;
: c=*a;
: *a=*b;
: *b=c;
: return;
: }
: This one is always correct. But isn't the other one only assigned a value of
: *c? I do not understand why the other one causes data Segmentation fault.
: Help please.

c**********e
发帖数: 2007
5
You are right. The following statement solves the problem.
int *c =new int;

【在 m******e 的大作中提到】
: 没给c分配内存啊
1 (共1页)
进入JobHunting版参与讨论
相关主题
悲剧啊,没学cs真的很悲剧啊。分享今天做的一道基础题
cisco 硅谷招人再问个简单的C问题
急问amazon面试邀请是不是有模板的啊?请教G家那题 abc123->a1b2c3
微软SDE招人virtual table存在memory的哪块啊?
为什么我这段简单的程序segment faultBloomberg(financial software developer)第一轮面试
怎么理解递归解决的“swap every two elements in a linked list”?[C++]请问哪些变量在heap创建?
paging和 segmentation有什么区别?问道面试体(software)
今天晚上要不然研究一下这题?问一个C的简单问题
相关话题的讨论汇总
话题: int话题: swap话题: what话题: q87话题: c++