passing a struct by value

D

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!
 
D

David White

Dave said:
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
 
J

jeffc

David White said:
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.
 
D

Dave

jeffc said:
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
 
D

Dave

jeffc said:
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
 
D

David White

jeffc said:
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
 
J

jeffc

Dave said:
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?
 
D

Dave

jeffc said:
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!
 
D

Dave

David White said:
I didn't know that running it was a necessary part of your project.


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.
 
?

=?iso-8859-1?Q?Juli=E1n?= Albo

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.
 
D

David White

Dave said:
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
 
D

Dave

Julián Albo said:
Dave escribi :


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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,145
Messages
2,570,826
Members
47,372
Latest member
LucretiaFo

Latest Threads

Top