C/C++ :: Segfault When Accessing Member Of A Struct
Mar 18, 2014
The program I'm working on is a very basic relational database. I've isolated my problem for simplicity. I get a segfault right here when I try to access db->relationCount. I tried printing db->relationCount from within loadDB and that worked,
I'm currently programming a server which uses multiple threads- I have a class for one map in the game. Each map has a thread for timed events(tile regeneration, NPC regeneration, etc.), and a thread for handling NPCs(movement, combat, etc.). A basic structure of the class looks like this:
class Region { public: /* game values are here, they are public so they can be accessed from outside of the class inside of packet-handling functions and such */ int value; void *Function();
[Code] ....
The program crashes when I use a member of the same class the function is located in- in the context I have shown about it would crash on "value++".
error: request for member 'character' in '* ptr', which is of non-class type 'datastructure*' error: request for member 'character' in '* ptr', which is of non-class type 'datastructure*'
These errors are related to " *ptr->character='a'; printf("Ptr: %c",*ptr->character); "
I want to access "character" data inside the structure "trial" by a pointer to pointer "ptr" inside function "function",but I couldn't find a way to do this.
I've been working for some number of days on this code to take information about movies from both a file and the user, store the infos in an array of structs, and at the end, write all the info out to a file. I'm having some problems with an error message reading:
"prog.c:102:11: error: subscripted value is neither array nor pointer nor vector"
this error occurs in many lines (which I will label specifically below -- they are everywhere where I am trying to access/modify an individual element of a struct element of the array).
A few examples of where I am having the problems are lines: 39, 52-55, 70, 72, and 86 (and more of the same exact variety).
I am obviously rather systematically doing something wrong, but I am quite certain all of these are the exact same mistakes.
I pull up also 2 or 3 other errors, but I don't think they are related and should be able to fix them quickly once I work out this conundrum.
#include <string.h> #include<stdlib.h> #include <stdio.h> int moviecount =0; typedef struct{ int year;
I've been reading the tutorials on Friendship and Inheritance [URL] ..... but I still don't understand why I can't access members of the same struct type.
The code above is located in a source file, where the function isAlphanumeric passes a char value, and Message is the struct containing the string I want to access. Below is the declaration of the struct and string located in the corresponding header file.
My frustration comes when I try to call and assign messageText like the tutorial does to its private members, but I keep getting an error saying I can't access the string because it is a private member. Is there a way to access the string without having to pass it through the function wordBeginsAt?
I have a vector (structures) in a struct (instances). I make a declaration of this struct called instance. The vector is a 3-layer vector of pointers, like so:
vector < vector < vector<scene::IAnimatedMeshSceneNode*> > > structures; (The type is from Irrlicht 3D). I have 3 nested "for" loops which looks similar to the following:
for (int a = 0; a < instance.structures.size(); a++) { /*note:vector size previously set*/ for (int b = 0; b < instance.structures[a].size(); b++){ for (int c = 0; c < instance.structures[a][b].size(); c++) {
if (1) { //checking value of variable not included in snippet
These are currently referencing the pointers, it seems. The program compiles but crashes at this point. I need them to reference the values of the pointers. Problem is, I don't know where to put the dereference operator (*). Where should it go?
I currently have a file which allows inputs to record different transistor types. I then have the task of accessing this structure, find a certain manufacturer ID, and print the information about this particular transistor.
My problem is accessing the array to search through.
Here is my code:
Code: #include "stdio.h" const int IDLEN=30; //All constant values defined const int POLARITYLEN=3; const int MAXSTOCKITEMS=10; //First structure defined struct TransistorRec {
[Code]......
The errors I am currently getting are on line 54 'expected primary-expression before "struct"' and on line 60 ' 'maunfacturersID' undeclared'
I'm currently working on the ioquake3 engine . The ioquake3 engine is separated into 2 different main threads at runtime: the gamecode and the engine. Both are communicating but not all information and my problem resides here.
In the gamecode, there's a struct called gentity_t which contains a lot of fields:
Code: typedef struct gentity_s gentity_t; struct gentity_s { entityState_ts;// communicated by server to clients entityShared_tr;// shared by both the server system and game // DO NOT MODIFY ANYTHING ABOVE THIS, THE SERVER // EXPECTS THE FIELDS IN THAT ORDER! //================================ struct gclient_s*client;// NULL if not a client
[Code] ....
This whole entity is passed to the engine at runtime, but only the first two fields are declared for the engine:
Code: // the server looks at a sharedEntity, which is the start of the game's gentity_t structure typedef struct { entityState_ts;// communicated by server to clients entityShared_tr;// shared by both the server system and game } sharedEntity_t;
My problem is that I need to access the health field of gentity_t from the engine. Technically, this is possible, but the health field is not declared in sharedEntity (which is the same memory address than gentity_t in the gamecode), so this is not straightforward.
I am looking for an elegant way to do this, and my constraint is that I must not edit the gamecode, only the engine.
The solutions I've thought:
- Just copy the whole gentity_t fields into sharedEntity_t. This would work I think but would be redundant, and I would like to avoid copying this huge set of fields.
- Include the two headers files declaring the gentity_t and sharedEntity_t structs, and create a Getter and a Setter functions that would cast a gentity_t over a sharedEntity_t and return/set a field. The problem is that I can't simply include them because they are both including some common headers files and this produce a recursive include error (and I can't modify the files to add a check, these are normally in the gamecode).
- Directly access the health field using a clever memory pointer, but I don't even know if that's possible given the huge number of fields prior health with many different types?
I have two classes, a Package class and a Person class. The Package class has two Person objects has member variables, a Sender and a Receiver. While overloading the << operator for the Package class so that it will make an output label from everything in the Package class. Here is my code...
class Package{ public: Person Sender; Person Reciever; int weight; double cost; friend ostream &operator<<(ostream &out, Package &pack);
[Code] .....
So my problem is on that last output line, I am unable to call Sender.getName()... etc. Is there a proper syntax so that I can access the members of the Person class while overloading the << operator for the Package class?
I am parsing a binary data file by casting a buffer to a struct. It seems to work really well apart from this one double which is always being accessed two bytes off, despite being in the correct place.
If I attempt to print GROSS using printf("%f", row->GROSS) I get 0.0000. However, if I change the type of GROSS to char[8] and then use the following code, I am presented with the correct number...
Code:
typedef struct example { double d; }
example; example *blah = (example*)row->GROSS; printf("%f", blah->d);
I am modifying a set of static variables inside of the class's member function. The static variables are private. An example of what I'm doing is as below,
utilities.h ----------- class utilities { private: static int num_nodes;
public: void parse_details(char* );
[Code] ....
I get a compilation error in the function void utilities::parse_details(char* filename)
which says: undefined reference to `utilities::num_nodes'
The problem occurs in here, I get access violations, is there a way to this while keeping Display const or is this code valid and my problem is somewhere else and not being caught in the debugger? I tried to make the return types const - but that didn't work .....
//Getters need const twice for this to work? const char* Player::GetName() const {return m_name;} const int Player::GetGrade() const {return m_grade;} const double Player::GetGPA() const {return m_gpa;}
struct Speaker { static int numElem; string name; int number; // Phone string topic; float fee; };
// IN main() FUNCTION Speaker s[10];
The goal is for numElem to keep track of how many of the 10 elements are in use. However, I'm not sure the proper way to access the element, if it's even possible.
Code: typedef struct _a { int id; } a; typedef struct _b { a my_a; my_a.id = 1; // error: expected specifier-qualifier-list before "my_a" } b;
I get error: expected specifier-qualifier-list before "my_a"
I must set the id for the kind of struct created inside the struct def because main() will be casting based on this id. Thats how I will know which structure b contains by it's id, there could be hundards of different structs with different values I will cast to the correct one and know it's members by it's id. How do I ?
How to get relative memory address of members of Class or Structure ? I want to auto scan the members of Class/Struct, and show the address/value like the "watch window" in debug mode of popular C/C++ IDE software.
WAVEFORMATEX InputTest::StandardWaveFormat { //Instantiate WaveFormat -- PCM standards StandardWaveFormat.wFormatTag = WAVE_FORMAT_PCM; StandardWaveFormat.cbSize = 0; //extra information sent over stream. Usually ignored in PCM format.
[Code] ....
I get the following errors starting with the header file:
Error1error C2146: syntax error : missing ';' before identifier 'StandardWaveFormat' Error2error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
both associated with the "const WAVEFORMATEX StandardWaveFormat; " line.
Here's a link to the WAVEFORMATEX struct: [URL] .....
Then the cpp source code is probably way off. Let me know if you'd like to see the errors associated with that.
Is it possible with a struct? How to express this to search engines so I haven't been able to find anything about it. If this is not possible with a struct, is there a way to define something that can do all the following things:
SomeStruct test = {0.5, 0.5, 0.5, 1}; test.g = 1.0; test[0] = 0.0; // test[0] would be equivalent to calling test.r float somevalue = test[3]; // test[3] would be equivalent to calling test.a
What are the workarounds for accessing the non-static member variables of some class(Say A) inside static member functions of another class(Say B)? I am coding in c++. Class A is derived with public properties of class B. Any pointers?
I work on a system that communicates with other systems via messages. Those messages are defined in a spec and every word must be exactly as defined. To accomplish this Ada allows me to define the fields of my record to specific words in memory.
Code: word = 4; type Msg_Type1 is record x: Some_Type; y: Another_Type; z: Another_Type; end record;
I wrote a program to detect if a graph is tree or not. Initially it was using static memory. Later I changed it to use memory dynamically using malloc().My problem is that, my program it works great for case when graph is not tree but fails if it is.
Code:
#include <stdio.h> #include <stdbool.h> #include <stdlib.h> int main() }
I am trying to send a packet across a client/server communication. I am getting a seg fault (while running the program(It compiles fine)) when I try to read the neighbor file, You should be able to see this below the comment /***** Read neighbor file***/ :