473,324 Members | 2,356 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

passing a struct by value

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
15 4636
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

"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
"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
"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
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
"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
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
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
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

"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
"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
"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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: kazack | last post by:
I am a little confused with code I am looking at. My c++ book does not go into passing a structure to a function so I pulled out a c book which does. and I do not understand the prototype verses...
9
by: Just Me | last post by:
PARAFORMAT2 is a structure that SendMessage will return stuff in. Is the "ref" correct or since only a pointer is being passed should it be by value? Suppose I was passing data rather then...
17
by: Christopher Benson-Manica | last post by:
Does the following program exhibit undefined behavior? Specifically, does passing a struct by value cause undefined behavior if that struct has as a member a pointer that has been passed to...
2
by: jconnort | last post by:
I'm trying to write a function to get the current system time, so I can use it when I need to output it to a log, below is the code I'm testing: #include "include.h" #include <stdlib.h> ...
12
by: manochavishal | last post by:
Hi, I am having strange problem in my Program. I cannot paste the whole program as it is huge so just pasting the lines i think are necessary. I am passing a integer array pointer to a...
12
by: Mike | last post by:
Consider the following code: """ struct person { char *name; int age; }; typedef struct person* StructType;
6
by: Roman Mashak | last post by:
Hello, I belive the reason of problem is simple, but can't figure out. This is piece of code: struct timeval { long tv_sec; /* seconds */ long tv_usec; /* microseconds */ };
8
by: S. | last post by:
Hi all, Can someone please help me with this? I have the following struct: typedef struct { char *name; int age; } Student;
4
by: arnuld | last post by:
I am passing an array of struct to a function to print its value. First I am getting Segfaults and weired values. 2nd, is there any elegant way to do this ? /* Learning how to use an array...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.