By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,969 Members | 2,360 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,969 IT Pros & Developers. It's quick & easy.

passing a struct by value

P: n/a
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!
Jul 19 '05 #1
Share this Question
Share on Google+
15 Replies


P: n/a
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

Jul 19 '05 #2

P: n/a

"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.
Jul 19 '05 #3

P: n/a
"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
Jul 19 '05 #4

P: n/a
"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
Jul 19 '05 #5

P: n/a
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...
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.


Well, it's not working. It's crashing. And given what we already know about
it, it's not a big setback not to be able to run it.

DW

Jul 19 '05 #6

P: n/a
"David White" <no@email.provided> wrote in message news:<_w****************@nasal.pacific.net.au>...
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...
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.


Well, it's not working. It's crashing. And given what we already know about
it, it's not a big setback not to be able to run it.

DW


I already admitted that. However, in my class, I can't just tell the
teacher, "we already know about it, so I didn't feel like I had to do
the project."

If no one else has any useful input on this problem (or about the .net
question I asked in my last post), then never mind.
Jul 19 '05 #7

P: n/a
dr*******@yahoo.com (Dave) wrote in message news:<1c**************************@posting.google. com>...
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!


I suppose I should make the problem more clear. The problem I'm
having is passing a struct by value gives me a segmentation error.
Why do I get the error and how do I get around it? Keeping in mind
that I am purposely trying to pass it by value, not by reference.
Jul 19 '05 #8

P: n/a
dr*******@yahoo.com (Dave) threw a soggy newspaper against the wall, and
here's what stuck:
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.


Despite the implication of the rather unfortunate name, Visual Studio .NET
does not require the generation of .NET executables. I use VS.NET 2003 all
the time for native C++ development, and I find it to be an excellent
compiler -- certainly far better than previous compilers from Microsoft.

PMP
Jul 19 '05 #9

P: n/a
Dave <dr*******@yahoo.com> wrote in message
news:1c**************************@posting.google.c om...
"David White" <no@email.provided> wrote in message news:<_w****************@nasal.pacific.net.au>...
Well, it's not working. It's crashing. And given what we already know about it, it's not a big setback not to be able to run it.


I already admitted that. However, in my class, I can't just tell the
teacher, "we already know about it, so I didn't feel like I had to do
the project."


I didn't know that running it was a necessary part of your project.
If no one else has any useful input on this problem (or about the .net
question I asked in my last post), then never mind.


Does it have to be 50,000? Reducing that number might work. Otherwise, see
if your compiler has an option for increasing the stack size, which is
almost certainly the reason it's crashing. Also, are other students having
the same problem? What are they doing about it?

I'm not sure what you mean by 'framework' regarding MS .NET. If you go to a
command prompt and type 'vcvars32' (look for VCVARS32.BAT if it wasn't
found), you should be able to use it as an ordinary command-line compiler.
Then, the command CL /? will probably (can't check right now) give you all
the compiler's command-line options.

DW

Jul 19 '05 #10

P: n/a

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

I suppose I should make the problem more clear. The problem I'm
having is passing a struct by value gives me a segmentation error.


I think the point that was made back to you was: how do you know it's
passing the struct by value that's giving you the error, rather than the
size of the array (50,000)? If the "segmentation error" is caused merely by
passing a struct by value, then you have a severely buggered compiler, and
there is little hope. But presumably your instructor has already written
and tested the assignment (we can only hope). Thus the question: are you
sure it has to be 50,000?
Jul 19 '05 #11

P: n/a
"jeffc" <no****@nowhere.com> wrote in message news:<3f********@news1.prserv.net>...
"Dave" <dr*******@yahoo.com> wrote in message
news:1c**************************@posting.google.c om...

I suppose I should make the problem more clear. The problem I'm
having is passing a struct by value gives me a segmentation error.


I think the point that was made back to you was: how do you know it's
passing the struct by value that's giving you the error, rather than the
size of the array (50,000)? If the "segmentation error" is caused merely by
passing a struct by value, then you have a severely buggered compiler, and
there is little hope. But presumably your instructor has already written
and tested the assignment (we can only hope). Thus the question: are you
sure it has to be 50,000?


He has done the project, but he uses SlickEdit. I did some other
research about Dev-C++ and this seems to be a problem with the
compiler itself.

I'm sure about the segmentation error because if I comment out that
code that tries to pass the struct by value, it runs just fine.

Also, 50,000 is the specification, although he reccomends increasing
the number if the system allows. Possibly, it's just too much to do
on this system. I hadn't thought of that. But the question is, why
would it work with visual studio .net 2003 if it were a system
limitation?

Regardless, it's due today so he'll get it compiled with VS .net.
Thanks again for your help!
Jul 19 '05 #12

P: n/a
"David White" <no@email.provided> wrote in message news:<49****************@nasal.pacific.net.au>...
Dave <dr*******@yahoo.com> wrote in message
news:1c**************************@posting.google.c om...
"David White" <no@email.provided> wrote in message

news:<_w****************@nasal.pacific.net.au>...
Well, it's not working. It's crashing. And given what we already know about it, it's not a big setback not to be able to run it.


I already admitted that. However, in my class, I can't just tell the
teacher, "we already know about it, so I didn't feel like I had to do
the project."


I didn't know that running it was a necessary part of your project.
If no one else has any useful input on this problem (or about the .net
question I asked in my last post), then never mind.


Does it have to be 50,000? Reducing that number might work. Otherwise, see
if your compiler has an option for increasing the stack size, which is
almost certainly the reason it's crashing. Also, are other students having
the same problem? What are they doing about it?

I'm not sure what you mean by 'framework' regarding MS .NET. If you go to a
command prompt and type 'vcvars32' (look for VCVARS32.BAT if it wasn't
found), you should be able to use it as an ordinary command-line compiler.
Then, the command CL /? will probably (can't check right now) give you all
the compiler's command-line options.

DW

I'll check on the stack size thing. That thought hadn't occured to me
:).

By framework, I refer to the .net framework. If I compile using
VisualStudio.net, the program is compiled using the .net framework and
therefore requires it to run. Is this not the case? I'm quite new to
the VS.net environment, so that may explain this question.
Jul 19 '05 #13

P: n/a
Dave escribió:
Also, 50,000 is the specification, although he reccomends increasing
the number if the system allows. Possibly, it's just too much to do
on this system. I hadn't thought of that. But the question is, why
would it work with visual studio .net 2003 if it were a system
limitation?


Windows use his virtual memory mapping functions to manage the stack,
and the stack grow automatically for normal increments. Functions that
grow the stack for more than two pages can fail if not includes code to
explcity grow the stack. Probably one compiler in his default mode
insert that code in the function and the other not. See the options of
your compilers or ask in a windows newsgroup.

Regards.
Jul 19 '05 #14

P: n/a
Dave <dr*******@yahoo.com> wrote in message
news:1c**************************@posting.google.c om...
By framework, I refer to the .net framework. If I compile using
VisualStudio.net, the program is compiled using the .net framework and
therefore requires it to run. Is this not the case? I'm quite new to
the VS.net environment, so that may explain this question.


Although I have VS .NET I've hardly used it so far because my project uses
VC++ 6.0. However, I understand from my colleagues that you have the option
of using the .NET framework, the MFC framework (like VC++ 6.0), or just
standard C++. If you create a console application project you can confine
your program to standard C++, or you can do this from the command line as I
previously explained.

DW

Jul 19 '05 #15

P: n/a
Julián Albo <JU********@terra.es> wrote in message news:<3F***************@terra.es>...
Dave escribi :
Also, 50,000 is the specification, although he reccomends increasing
the number if the system allows. Possibly, it's just too much to do
on this system. I hadn't thought of that. But the question is, why
would it work with visual studio .net 2003 if it were a system
limitation?


Windows use his virtual memory mapping functions to manage the stack,
and the stack grow automatically for normal increments. Functions that
grow the stack for more than two pages can fail if not includes code to
explcity grow the stack. Probably one compiler in his default mode
insert that code in the function and the other not. See the options of
your compilers or ask in a windows newsgroup.

Regards.


Great information!

Thanks again all three of you for your input!

Dave
Jul 19 '05 #16

This discussion thread is closed

Replies have been disabled for this discussion.