473,748 Members | 7,608 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

passing copy of a pointer to a variable vs passing the copy

which one do you think is better ? I need to make my program efficient
and in some places I have passed the copy of a variable which makes
life some what easy while writing huge expressions but they do requrie
much more memory than a simple pointer.
Jun 27 '08 #1
7 2155
On May 30, 9:20 am, pereges <Brol...@gmail. comwrote:
which one do you think is better ? I need to make my program efficient
and in some places I have passed the copy of a variable which makes
life some what easy while writing huge expressions but they do requrie
much more memory than a simple pointer.
Can you give an example? What sort of variable?

-David
Jun 27 '08 #2
pereges <Br*****@gmail. comwrote:
which one do you think is better ? I need to make my program efficient
and in some places I have passed the copy of a variable
In C you can only pass copies of variables to functions.
which makes life some what easy while writing huge expressions
I what respect?
but they do requrie much more memory than a simple pointer.
Which variables take much more memory than a simple pointer?
Most variables you can pass to afunction are at least of
comparable size to the one of a pointer. The only exception
is structures, which can definitely be quite a lot larger than
pointers, and that's why they often get passed via a pointer
instead of by value, despite C allowing it.

But I guess I am misunderstandin g your intents. Could you
post some code that illustrates what yiu are doing?

And, of course, the usual warning: don't optimize prematurely
but instead measure where the bottle-necks are in your program.

Regards, Jens
--
\ Jens Thoms Toerring ___ jt@toerring.de
\______________ ____________ http://toerring.de
Jun 27 '08 #3
On May 30, 7:14 pm, j...@toerring.d e (Jens Thoms Toerring) wrote:
Which variables take much more memory than a simple pointer?
Most variables you can pass to afunction are at least of
comparable size to the one of a pointer. The only exception
is structures, which can definitely be quite a lot larger than
pointers, and that's why they often get passed via a pointer
instead of by value, despite C allowing it.

But I guess I am misunderstandin g your intents. Could you
post some code that illustrates what yiu are doing?

And, of course, the usual warning: don't optimize prematurely
but instead measure where the bottle-necks are in your program.
Yes, I'm talking about a structure. For example I want to store a mesh
containing close to 200,000 vertices and 400,000 triangles. Here's my
data structure:

typedef struct vector_struct
{
double x, y, z;
}vector;

typedef vector vertex;

typedef struct triangle_struct
{
int v[3];
vector normal;
}triangle;

/* ======== Data structure for the mesh ========== */

typedef struct object_struct
{
vector *vertexlist;
triangle *trianglelist;
unsigned long int nvert;
unsigned long int ntri;
}object;
Jun 27 '08 #4
On May 30, 7:23 pm, pereges <Brol...@gmail. comwrote:
Yes, I'm talking about a structure. For example I want to store a mesh
containing close to 200,000 vertices and 400,000 triangles. Here's my
data structure:

typedef struct vector_struct
{
double x, y, z;

}vector;

typedef vector vertex;

typedef struct triangle_struct
{
int v[3];
vector normal;

}triangle;

/* ======== Data structure for the mesh ========== */

typedef struct object_struct
{
vector *vertexlist;
triangle *trianglelist;
unsigned long int nvert;
unsigned long int ntri;

}object;
In this mesh structure, vertexlist and triangle list are pointers to
the array of vertices and triangles respectively.
Jun 27 '08 #5
pereges <Br*****@gmail. comwrote:
On May 30, 7:23 pm, pereges <Brol...@gmail. comwrote:
Yes, I'm talking about a structure. For example I want to store a mesh
containing close to 200,000 vertices and 400,000 triangles. Here's my
data structure:

typedef struct vector_struct
{
double x, y, z;

}vector;

typedef vector vertex;

typedef struct triangle_struct
{
int v[3];
vector normal;

}triangle;

/* ======== Data structure for the mesh ========== */

typedef struct object_struct
{
vector *vertexlist;
triangle *trianglelist;
unsigned long int nvert;
unsigned long int ntri;

}object;
In this mesh structure, vertexlist and triangle list are pointers to
the array of vertices and triangles respectively.
If you pass a pointer to the structure to a function you safe
a bit of work in copying it and a bit of memory since no copy
needs to be made (but not that much for such a rather small
structure). The main difference is that you can easily change
all the elements in the original structure without passing it
also back to the caller since you are working on the original
itself and not a copy (although you can prevent that by quali-
fying the pointer as 'const' inthe functions argument list).

I don't see why dealing with a pointer instead of a copy of
the structure would be more difficult, you just have to use
a '->' between the pointer and the elements name you want
to access instead of a '.' between the structures name and
the element.

In the overwhelming majority of programs I have seen pointers
to structures get passed to fucntions instead of the structures
by value, so it's a very common idiom and most people will un-
derstand it quite fine. And, yes it might be a bit more effi-
cient speed- and memory-wise, but I wouldn't expect a doubling
of the speed of your application (or anything near that) just
from using pointers;-)
Regards, Jens
--
\ Jens Thoms Toerring ___ jt@toerring.de
\______________ ____________ http://toerring.de
Jun 27 '08 #6
pereges <Br*****@gmail. comwrites:
On May 30, 7:14 pm, j...@toerring.d e (Jens Thoms Toerring) wrote:
>Which variables take much more memory than a simple pointer?
Most variables you can pass to afunction are at least of
comparable size to the one of a pointer. The only exception
is structures, which can definitely be quite a lot larger than
pointers, and that's why they often get passed via a pointer
instead of by value, despite C allowing it.

But I guess I am misunderstandin g your intents. Could you
post some code that illustrates what yiu are doing?

And, of course, the usual warning: don't optimize prematurely
but instead measure where the bottle-necks are in your program.

Yes, I'm talking about a structure. For example I want to store a mesh
containing close to 200,000 vertices and 400,000 triangles. Here's my
data structure:

typedef struct vector_struct
{
double x, y, z;
}vector;

typedef vector vertex;

typedef struct triangle_struct
{
int v[3];
vector normal;
}triangle;

/* ======== Data structure for the mesh ========== */

typedef struct object_struct
{
vector *vertexlist;
triangle *trianglelist;
unsigned long int nvert;
unsigned long int ntri;
}object;
On one implementation I tried, your type "object" has a size of 16
bytes; that's probably typical. On most systems, it's probably going
to be about 4 times the size of a pointer.

Passing the structure itself will obviously impose some overhead
because you're passing more information.

On the other hand, it might save some code space because, within the
function, you save a pointer dereference every time you access a
member of the structure. Or, depending on how member access is
implemented by your compiler, it might not help at all.

Measure it.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jun 27 '08 #7
On May 30, 8:35 pm, j...@toerring.d e (Jens Thoms Toerring) wrote:
If you pass a pointer to the structure to a function you safe
a bit of work in copying it and a bit of memory since no copy
needs to be made (but not that much for such a rather small
structure). The main difference is that you can easily change
all the elements in the original structure without passing it
also back to the caller since you are working on the original
itself and not a copy (although you can prevent that by quali-
fying the pointer as 'const' inthe functions argument list).

I don't see why dealing with a pointer instead of a copy of
the structure would be more difficult, you just have to use
a '->' between the pointer and the elements name you want
to access instead of a '.' between the structures name and
the element.

In the overwhelming majority of programs I have seen pointers
to structures get passed to fucntions instead of the structures
by value, so it's a very common idiom and most people will un-
derstand it quite fine. And, yes it might be a bit more effi-
cient speed- and memory-wise, but I wouldn't expect a doubling
of the speed of your application (or anything near that) just
from using pointers;-)
Well those are not the only structures that my ray tracing program is
using. There is also a structure for BSP tree(binary space
partitioning) which takes up a lot of memory. The list of rays also
consume a lot of memory. My program works very well for smaller number
of rays < 400 K or 500 K but I have seen it fails beyond that. I was
thinking fo storing the the list of rays in a file rather than as an
array in the memory. Probably that could help. I haven't used unions
either.
Jun 27 '08 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
36412
by: Andy | last post by:
Hi Could someone clarify for me the method parameter passing concept? As I understand it, if you pass a variable without the "ref" syntax then it gets passed as a copy. If you pass a variable with the "ref" syntax then it gets passed as a reference to the object and any changes to
58
10170
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of code... TCHAR myArray; DoStuff(myArray);
20
6237
by: joe | last post by:
Hi all! I just have quick, possibly stupid question.... is it possible to do the following: int func(){ int *pointer; foo(pointer); } int foo(int *pointer){
11
4465
by: truckaxle | last post by:
I am trying to pass a slice from a larger 2-dimensional array to a function that will work on a smaller region of the array space. The code below is a distillation of what I am trying to accomplish. // - - - - - - - - begin code - - - - - - - typedef int sm_t; typedef int bg_t; sm_t sm; bg_t bg;
17
9390
by: LP | last post by:
Hello, Here's the scenario: Object A opens a Sql Db connection to execute number of SqlCommands. Then it needs to pass this connection to a constructor of object B which in turn executes more commands on the same connection. I have an understanding that if SqlConnection is passed as "value" (unboxed), object B will create its own copy of SqlConnection, so when object A closes its connection, it remains open for object B's copy. Is this
11
8128
by: John Pass | last post by:
Hi, In the attached example, I do understand that the references are not changed if an array is passed by Val. What I do not understand is the result of line 99 (If one can find this by line number) which is the last line of the following sub routine: ' procedure modifies elements of array and assigns ' new reference (note ByVal) Sub FirstDouble(ByVal array As Integer()) Dim i As Integer
7
3305
by: TS | last post by:
I was under the assumption that if you pass an object as a param to a method and inside that method this object is changed, the object will stay changed when returned from the method because the object is a reference type? my code is not proving that. I have a web project i created from a web service that is my object: public class ExcelService : SoapHttpClientProtocol {
11
6167
by: =?Utf-8?B?U3VqZWV0?= | last post by:
If there are long strings (like 1MB or 2MB) is it more performant to pass those by ref to methods or by value?
5
1475
by: CapCity | last post by:
I'm sure I'm missing something simple - I do not code in C regularly, and the breaks are long enough for me to forget. The situation I have is I need to create an array but I do not know the dimension until runtime. I pass the pointer to a function which then determines the size and then creates and populates it. I can walk the array OK in the function, but the app crashes when I try to do so in the calling routine. Here's a small...
0
8989
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8828
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9537
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9367
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9243
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6795
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
3309
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2780
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2213
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.