CFD Online Discussion Forums

CFD Online Discussion Forums (http://www.cfd-online.com/Forums/)
-   Main CFD Forum (http://www.cfd-online.com/Forums/main/)
-   -   structures in C++ (http://www.cfd-online.com/Forums/main/16041-structures-c.html)

Steven November 22, 2008 10:55

structures in C++
 
Hi, I have this problem. I have created a struct to store my datapoints and values of a certain domain. I placed this inside a header file, solverarraycfd.h:

#ifndef SOLVERARRAYCFD_H

#define SOLVERARRAYCFD_H

struct ssCFD{

int nCFDDim; // dimension

int nCFDOrder; // order of element

int nCFDDOF; // no. of DOF's

.... };

#endif

I will be using this struct during function calls, which will be found in numerous .cpp files.

In main(), I have declared:

#include "solverarraycfd.h" ssCFD* dgCFD;

But when i write this after the above declaration:

dgCFD->nCFDDim=2;

There is a segmentation fault during runtime.

I ran valgrind for my executable, and the message it gave was

Use of uninitialised value of size 4 ==8974== at 0x805A864: main (main.cpp:145)

which was the line: dgCFD->nCFDDim=2;

Can anyone guide me and advice me what went wrong? Do I have to perform an extern somewhere, or something like that?


Jed November 22, 2008 12:56

Re: structures in C++
 
The line
ssCFD* dgCFD; just creates a pointer to a structure of type <code>ssCFD</code>. While you can write ssCFD dgCFD; at global scope to create the object, it is much better design to explicitly pass it to functions that need it (or make those functions methods of the class, if you know what that means).

Steven November 22, 2008 13:18

Re: structures in C++
 
Yes, I know what you mean.

But this structure is not an all-compassing entity that governs its related functions. Also, I have functions that require this struct as an argument, and this struct is by no means small in size, so I would like to pass the pointer to structure instead.

I also hope to keep the modularity in terms of storing the struct definition in a header file. So now the main problem is what have I missed out during this process, I guess..

Thanks for your advice!

c++ November 22, 2008 13:44

Re: structures in C++
 
Try this:

ssCFD* dgCFD = new ssCFD;

Then you can get the pointer to this object. But don't forget to delete this later.

However, I think you are better off creating an object in some permanent scope and pass by reference through your functions. Don't forget that pass by reference is preferred mechanism in C++ compared to pass by value or pointer arguments.


Steven November 22, 2008 19:46

Re: structures in C++
 
Ok, thanks! Maybe that's what I've missed out..

Steven November 22, 2008 19:57

Re: structures in C++
 
Got it! So declaring a pointer to structure also needs a 'new' statement. Thanks!

Steve November 24, 2008 05:37

Re: structures in C++
 
There is more to it than this. In C or C++, the following statement declares a pointer to a myStruct:

myStruct *p;

It creates an entity that's typically 4 or 8 bytes in size (depending on architecture) that can contain the address of a myStruct type. It does not allocate any memory for that type and the pointer itself probably points to some random area of memory (it is not initialised). If you then try to use the pointer to set the memory it points to, all hell could break loose:

p->someIntElement=666;

this overwrites some (possibly dangerous) memory with a value. Hopefully your OS is good enough to report it as a seg fault (accessing illegal memory).

To be able to use "p" to set memory requires that it point to something valid.

myStruct s; /* Declare a structure */ myStruct *p; /* Declare a pointer to a structure */ p = &s; /* Point the pointer to the structure */ p->someIntElement=666; /* No harm done */

More commonly, you'd dynamically allocate the memory for the structure and then point the pointer to it, like this:

p=(myStruct)malloc(sizeof(myStruct)); /* C */ ... free(p);

p=new myStruct; /* C++ */ ... delete(p);

--------------

Unless you fully understand the above, you'll never write sensible C or C++ code.

Steven November 24, 2008 10:09

Re: structures in C++
 
Hi Steve,

Yes, I understand what pointers do, and I should be aware of the potential hazards it could bring. I should initialise pointers properly and clean them up properly as well. Thanks for your warning.

c++ November 24, 2008 11:28

Re: structures in C++
 
This is all true but the best thing is to avoid pointers altogether and switch to using references. Of course, pointers and memory allocation are required but at least in this particular case creating an object and taking the reference to it is a better option. Since all fields in this struct are public and contain only integer and possible some float or double data (this could be a template argument if one wishes so), you could rely on compiler to create proper constructors and destructors for you. This means that there is no need to cleanup memory by hand as in the case of using new() or c-style malloc() and moreover, you can pass reference to this object in all of your functions just as if you'd pass a pointer.

Oscar Mauricio Santos Martinez November 24, 2008 22:52

structures in C++
 
hi I work in cfds investigation on laminar flow , and I need your help. I program in c++, this doesnt have visualization and it is for that reason I ask for your help. how can I visualize the answers from c++ in a 3-dimensions plot?

Steven November 25, 2008 00:19

Re: structures in C++
 
Use other visualisation software. If you can get hold of Tecplot, that'll be good. If not, install Paraview. It's free and very good too. There are data files everywhere for reference.

Steve November 25, 2008 06:48

Re: structures in C++
 
No pointers?

Are you advocating not using dynamic memory allocation (Back to the F77 days)?

c++ November 25, 2008 14:17

Re: structures in C++
 
No I am not. Pointers must be used for memory allocation especially for arrays. What I said here is that in this particular situation references could be used instead of pointers. That's all.


All times are GMT -4. The time now is 20:39.