|
[Sponsors] |
November 22, 2008, 10:55 |
structures in C++
|
#1 |
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? |
|
November 22, 2008, 12:56 |
Re: structures in C++
|
#2 |
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). |
|
November 22, 2008, 13:18 |
Re: structures in C++
|
#3 |
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! |
|
November 22, 2008, 13:44 |
Re: structures in C++
|
#4 |
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. |
|
November 22, 2008, 19:46 |
Re: structures in C++
|
#5 |
Guest
Posts: n/a
|
Ok, thanks! Maybe that's what I've missed out..
|
|
November 22, 2008, 19:57 |
Re: structures in C++
|
#6 |
Guest
Posts: n/a
|
Got it! So declaring a pointer to structure also needs a 'new' statement. Thanks!
|
|
November 24, 2008, 05:37 |
Re: structures in C++
|
#7 |
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. |
|
November 24, 2008, 10:09 |
Re: structures in C++
|
#8 |
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. |
|
November 24, 2008, 11:28 |
Re: structures in C++
|
#9 |
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.
|
|
November 24, 2008, 22:52 |
structures in C++
|
#10 |
Guest
Posts: n/a
|
hi I work in cfd¨s investigation on laminar flow , and I need your help. I program in c++, this doesn´t 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?
|
|
November 25, 2008, 00:19 |
Re: structures in C++
|
#11 |
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.
|
|
November 25, 2008, 06:48 |
Re: structures in C++
|
#12 |
Guest
Posts: n/a
|
No pointers?
Are you advocating not using dynamic memory allocation (Back to the F77 days)? |
|
November 25, 2008, 14:17 |
Re: structures in C++
|
#13 |
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.
|
|
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
CFD for Desiging structures | MA-Architecture | Main CFD Forum | 0 | September 28, 2011 17:23 |
Group effects & fluid structures | diaw | Main CFD Forum | 1 | October 27, 2006 07:04 |
Unsteady flow structures - time development | diaw | Main CFD Forum | 4 | December 13, 2005 06:15 |
Convection velocity of Coherent structures | Jongdae Kim | Main CFD Forum | 3 | February 5, 2002 05:04 |
CFD package for very-low-speed convective flows in large structures | Matthew Kuperus Heun | Main CFD Forum | 1 | November 18, 1999 22:03 |