按值传递结构 [英] passing a struct by value

查看:58
本文介绍了按值传递结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开展一个小型项目(很赞赏我的CS课程)

比较传递值和通过引用传递

之间的时差。我正在传递一个50000 int'的数组。但是,由于在
C ++中默认情况下通过引用传递数组,我需要将

数组嵌入到结构中,以便按值传递它。问题是

这样做时会出现分段错误。我正在使用Dev-c ++

编译器。有什么想法吗?

这里是基本来源:

....(代码)......


无效byval(struct Array a);


struct Array

{

int data [50000];

};


....(代码)......


byval(a);

......(代码)......


void byval(struct Array a)

{

//什么都不做

}


提前致谢!

解决方案

Dave< dr ******* @ yahoo.com>在留言中写道

新闻:1c ************************** @ posting.google.c om ...

我目前正在开展一个小型项目(很赞赏我的CS课程)
比较传递值和传递
之间的时差。我正在传递一个50000 int'的数组。但是,由于在C ++中默认情况下通过引用传递数组,我需要将
数组嵌入到结构中,以便按值传递它。问题是,这样做时会出现分段错误。我正在使用Dev-c ++
编译器。任何想法?




堆栈溢出可能。你真的需要做这样的测试吗?我们已经知道通过值传递将复制参数,这意味着复制

50,000英镑,并且通过引用传递不会。你不需要把它运行到

知道会有很大的不同。


DW




David White < no@email.provided>在消息中写道

新闻:A8 *************** @ nasal.pacific.net.au ...

Dave<组成; dr ******* @ yahoo.com>在消息中写道
新闻:1c ************************** @ posting.google.c om ...

我目前正在开展一个小型项目(很赞赏我的CS课程)
比较传递值和传递
之间的时差。我正在传递一个50000 int'的数组。但是,由于在C ++中默认情况下通过引用传递数组,我需要将
数组嵌入到结构中,以便按值传递它。问题是,这样做时会出现分段错误。我正在使用Dev-c ++
编译器。任何想法?



堆栈溢出可能。你真的需要做这样的测试吗?我们已经知道通过值传递将复制参数,这意味着复制5万英寸,并且通过引用传递不会。你不需要运行它来知道会有很大的不同。




考虑到他正在为一堂课做这件事,并试图为自己学习,

是的 - 他确实。


" jeffc" <无**** @ nowhere.com>在消息新闻中写道:< 3f ******** @ news1.prserv.net> ...

" David White" < no@email.provided>在消息中写道
新闻:A8 *************** @ nasal.pacific.net.au ...

Dave< dr ** *****@yahoo.com>在消息中写道
新闻:1c ************************** @ posting.google.c om ...

我目前正在开展一个小型项目(很赞赏我的CS课程)
比较传递值和传递
之间的时差。我正在传递一个50000 int'的数组。但是,由于在C ++中默认情况下通过引用传递数组,我需要将
数组嵌入到结构中,以便按值传递它。问题是,这样做时会出现分段错误。我正在使用Dev-c ++
编译器。任何想法?



堆栈溢出可能。你真的需要做这样的测试吗?我们已经知道通过值传递将复制参数,这意味着复制5万英寸,并且通过引用传递不会。你不需要运行它来知道会有很大的不同。



考虑到他正在上课,并试图学习对于他自己,
是的 - 他确实。



确切地说 - 因为这个原因我做了。我已经知道有一个很大的

差异。事实上,我必须在学校的一台计算机上迭代传递参考

函数大约400000次

让它在任何时候都可以注册。我有这个问题,因为

相同的代码适用于VisualStudio.net编译器,但不适用于

Dev-C ++。我想我可以和Borland一起尝试。


因为.net框架,我不想使用VisualStudio.net。

除非你能告诉我如何在一个
VisualStudio.net编译中排除框架(是托管的c ++?) - 这也可以工作




Dave


I''m currently working on a small project (admitedly for my CS class)
that compares the time difference between passing by value and passing
by reference. I''m passing an array of 50000 int''s. However, since in
C++ an array is passed by reference by default I need to embed the
array into a struct in order to pass it by value. The problem is that
I get a segmentation error when doing so. I''m using the Dev-c++
compiler. Any ideas?
Here''s the basic source:
....(code)...

void byval(struct Array a);

struct Array
{
int data[50000];
};

....(code)...

byval(a);

....(code)...

void byval(struct Array a)
{
// do nothing
}

Thanks in advance!

解决方案

Dave <dr*******@yahoo.com> wrote in message
news:1c**************************@posting.google.c om...

I''m currently working on a small project (admitedly for my CS class)
that compares the time difference between passing by value and passing
by reference. I''m passing an array of 50000 int''s. However, since in
C++ an array is passed by reference by default I need to embed the
array into a struct in order to pass it by value. The problem is that
I get a segmentation error when doing so. I''m using the Dev-c++
compiler. Any ideas?



A stack overflow maybe. Do you really need to do such a test? We already
know that passing by value will copy the argument, which means copying
50,000 ints, and passing by reference won''t. You don''t need to run it to
know there will be a huge difference.

DW



"David White" <no@email.provided> wrote in message
news:A8***************@nasal.pacific.net.au...

Dave <dr*******@yahoo.com> wrote in message
news:1c**************************@posting.google.c om...

I''m currently working on a small project (admitedly for my CS class)
that compares the time difference between passing by value and passing
by reference. I''m passing an array of 50000 int''s. However, since in
C++ an array is passed by reference by default I need to embed the
array into a struct in order to pass it by value. The problem is that
I get a segmentation error when doing so. I''m using the Dev-c++
compiler. Any ideas?



A stack overflow maybe. Do you really need to do such a test? We already
know that passing by value will copy the argument, which means copying
50,000 ints, and passing by reference won''t. You don''t need to run it to
know there will be a huge difference.



Considering that he''s doing it for a class, and trying to learn for himself,
yes - he does.


"jeffc" <no****@nowhere.com> wrote in message news:<3f********@news1.prserv.net>...

"David White" <no@email.provided> wrote in message
news:A8***************@nasal.pacific.net.au...

Dave <dr*******@yahoo.com> wrote in message
news:1c**************************@posting.google.c om...

I''m currently working on a small project (admitedly for my CS class)
that compares the time difference between passing by value and passing
by reference. I''m passing an array of 50000 int''s. However, since in
C++ an array is passed by reference by default I need to embed the
array into a struct in order to pass it by value. The problem is that
I get a segmentation error when doing so. I''m using the Dev-c++
compiler. Any ideas?



A stack overflow maybe. Do you really need to do such a test? We already
know that passing by value will copy the argument, which means copying
50,000 ints, and passing by reference won''t. You don''t need to run it to
know there will be a huge difference.



Considering that he''s doing it for a class, and trying to learn for himself,
yes - he does.


Exactly - for that reason I do. I already know that there''s a big
difference. In fact, I have to iterate the passing by reference
function about 400000 times on one of the school''s computer just to
get it to register any time at all. I have the question because the
same code works with the VisualStudio.net compiler, but not with
Dev-C++. I suppose I could try it with Borland.

I don''t want to use VisualStudio.net because of the .net framework.
Unless you can tell me how to exclude the framework in a
VisualStudio.net compilation (is that managed c++?) - that would work
too.

Dave


这篇关于按值传递结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆