Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Should I use pointers or use the struct "directly"? 1

Status
Not open for further replies.

taskler

Programmer
Sep 7, 2010
5
0
0
SE
What's the advantage of using pointers vs using the structs direcxtly in this program?
Ie should it be
vector* vector_make_from(int vsize, double* d);
instead?


_____________



#include <math.h>
#include <stdlib.h>
#include "vector.h"
#include <stdio.h>

vector vector_make(int size)
{
vector vr;// = malloc(sizeof(vector));
vr.size = size;
vr.v = malloc(size * sizeof(double));
return vr;
}

vector vector_make_from(int vsize, double* d)
{
vector vr;// = malloc(sizeof(vector));
vr.size = vsize;
int i;
for(i=0; i<vsize; i++) {
vr.v = d;
}
return vr;
}

void vector_destroy(vector vr)
{
free(vr.v);
//free(vr);
}

/* double vector_length(double* xs, int size) */
/* { */

/* sqrt(sum); */
/* } */

double vector_get(vector vr, int index)
{
return vr.v[index];
}

void vector_set(vector vr, int index, double d)
{
vr.v[index] = d;
}

double vector_dot_product(double* xs, double* ys, int size)
{
int i;
int sum = 0;
for(i=0; i<size; i++) {
sum += xs * ys;
}
return sum;
}

double vector_euclidean_distance(vector v1, vector v2)
{
int i;
double sum = 0;
for(i=0; i<v1.size; i++) {
sum += pow(v1.v - v2.v, 2);
}
return sqrt(sum);
}











#include <stdio.h>
#include "vector.h"
#include <stdlib.h>


int main(void)
{

vector a = vector_make(3);
vector b = vector_make(3);

double z[3] = {5,1,3};

vector c = vector_make_from(3,z);
printf("\nWhere is jsjsjerror?");
vector_set(c,2,4);
printf("\nz[2] is:%f\n", z[2]);

double x[3] = {1,2,3};
double y[3] = {2,3,5};
a.v = x;
b.v = y;



return 0;
}
 
The main advantage of using pointers to struct instead of struct is that you don't move on and off the stack big quantities of data. A struct can be a large piece of data in memory. When you call a function and give a struct parameter, it will be copied on the stack and the function will operate on the copy, not on the original struct (in C, parameters to functions are passed by-value). If you use pointers to struct as parameters, then the original struct also remains where it is but it isn't copied on the stack. Instead, it's address is copied on the stack (but the address is a very small quantity, like 4 bytes or the like) and the function accesses the struct via this address. This also means that any modification that the function makes to the struct will remain permanent because it is made on the real struct, not on a temporary copy on the stack.

So, to summarize, using pointers instead of structs is quite desirable if your struct is large, because only the struct's address gets copied on the stack. Also, since the function only has a pointer to the struct instead of having the whole struct (which, by the way, would be only a copy and not the original), access to the struct will be slightly slower, because first the value of the pointer is accessed and then the value at the address the pointer points to (which would be the actual struct data)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top