CFD Online Logo CFD Online URL
www.cfd-online.com
[Sponsors]
Home > Forums > Main CFD Forum

structures in C++

Register Blogs Members List Search Today's Posts Mark Forums Read

Reply
 
LinkBack Thread Tools Display Modes
Old   November 22, 2008, 09:55
Default structures in C++
  #1
Steven
Guest
 
Posts: n/a
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?

  Reply With Quote

Old   November 22, 2008, 11:56
Default Re: structures in C++
  #2
Jed
Guest
 
Posts: n/a
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).
  Reply With Quote

Old   November 22, 2008, 12:18
Default Re: structures in C++
  #3
Steven
Guest
 
Posts: n/a
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!
  Reply With Quote

Old   November 22, 2008, 12:44
Default Re: structures in C++
  #4
c++
Guest
 
Posts: n/a
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.

  Reply With Quote

Old   November 22, 2008, 18:46
Default Re: structures in C++
  #5
Steven
Guest
 
Posts: n/a
Ok, thanks! Maybe that's what I've missed out..
  Reply With Quote

Old   November 22, 2008, 18:57
Default Re: structures in C++
  #6
Steven
Guest
 
Posts: n/a
Got it! So declaring a pointer to structure also needs a 'new' statement. Thanks!
  Reply With Quote

Old   November 24, 2008, 04:37
Default Re: structures in C++
  #7
Steve
Guest
 
Posts: n/a
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.
  Reply With Quote

Old   November 24, 2008, 09:09
Default Re: structures in C++
  #8
Steven
Guest
 
Posts: n/a
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.
  Reply With Quote

Old   November 24, 2008, 10:28
Default Re: structures in C++
  #9
c++
Guest
 
Posts: n/a
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.
  Reply With Quote

Old   November 24, 2008, 21:52
Default structures in C++
  #10
Oscar Mauricio Santos Martinez
Guest
 
Posts: n/a
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?
  Reply With Quote

Old   November 24, 2008, 23:19
Default Re: structures in C++
  #11
Steven
Guest
 
Posts: n/a
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.
  Reply With Quote

Old   November 25, 2008, 05:48
Default Re: structures in C++
  #12
Steve
Guest
 
Posts: n/a
No pointers?

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

Old   November 25, 2008, 13:17
Default Re: structures in C++
  #13
c++
Guest
 
Posts: n/a
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.
  Reply With Quote

Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
CFD for Desiging structures MA-Architecture Main CFD Forum 0 September 28, 2011 16:23
Group effects & fluid structures diaw Main CFD Forum 1 October 27, 2006 06:04
Unsteady flow structures - time development diaw Main CFD Forum 4 December 13, 2005 05:15
Convection velocity of Coherent structures Jongdae Kim Main CFD Forum 3 February 5, 2002 04:04
CFD package for very-low-speed convective flows in large structures Matthew Kuperus Heun Main CFD Forum 1 November 18, 1999 21:03


All times are GMT -4. The time now is 00:44.