C++ :: Storing Data As Void And Dereferencing Them Later?
Jan 15, 2013
I have a set of functions at work which are incredibly useful, however it only supports labels that come from a specific database because that database contains information on the type. I'd like to re-create it to make it more applicable to any member/static/global variables but don't know how to store the type.
I've already re-written it to an extent, but it only accepts int types. I'd like to template it to accept any type. The trick is storing that type so that the pointer can be dereferenced at a later time which I don't know how to do.
Interface:
typedef int T; // The goal is to remove this line!
namespace TimerDelay {
void SetAfterDelay ( T* lpLabelAddress, float delay, T target = T(1)); // Queues the set
void ManageDelays ( float dt ); // sets the labels when appropriate
}
Source:
#include <vector>
namespace TimerDelay{
struct DelayObject {
void* address; // I will probably need to add a container
void* target; // to hold the type, but how can this be done?
[code]....
Edit:Is it possible to store a std::iterator_traits<> struct as a member of my structure? The g_list isn't templated as it needs to accept all types at the same time. That means that DelayObject cannot be templated. I think that means that I cannot use a templated member class as the size may be inconsistant.
I could understand void pointers I created the following program:
Code: #include <stdio.h> #include <string.h> int main(void) {
char word[] = "Zero"; int number = 0; void *ptr = NULL;
[Code] .....
The program works fine, however i really want to fully understand what is going on with the dereferencing of the void pointer, for example: With the following code:
Code: ptr = &number; *((int *)ptr) = 1;
Why can't you just do:
Code: ptr = &number; *(int *)ptr = 1;
And again with this code, (i'm guessing it's becuase its a pointer to a pointer?):
I want to know how to dereference a void pointer through the way of typing it.
Lets just say that I malloc'd a huge bunch of memory and i can do whatever i want
void* randomData = malloc ( 1000000 );
And i decide to make my own virtual 'int'
I am not sure how to do this.
*( int* ) ( randomData + 10 ) = ( int ) 323453 //323453 can be an int variable aswell
Im not sure if this is the right way to do perform a dereference.
This is an overview of what has to be done: -The pointer has to be dereferenced -Cast the pointer as an int pointer so we can change it like a normal 4-byte int -Perform pointer arithmetic, so that the int can be placed anywhere we want
I am new to C++ ... My question is if I were to pass some data or values from the main() function through a void function (which main purpose is to display the data in a certain manner); how should one go about doing so?
For instance:
Code: // example.cpp -- poorly written, just trying to learn #include <iostream> void format() { using namespace std; cout << "Time: " << min << hrs << endl;
[Code] .....
Obviously this is really poorly written, and confusing for the user. My main goal is to learn how to pass through values to a void function.
Also a bit off-topic, but is there a downside to place "using namespace std;" outside a function say if out of 100 functions only 10 of them use it? Would it make the program slower/unstable in any way or is this something one could do without any downsides?
I then realized that the order that the second method gave me will make it very hard for me to calculate the RGBs. Because they will be calculated like wise..
P3 600 339 255 44 5 8 = sum 44 5 8 = sum 43 4 7 = sum 42 3 6 = sum
I have a file that can range from 100 rows to 10000+ that is comma delimited with 8 columns. The first 32 rows (also comma delimited) will always be ignored (geographical header information). I will be wanting the data from column2 and column3.
For this I believe I would need (2) For Loops as such:
I am trying to store the Title, Artist, and date published of a list of CD's. I can't seem to be able to get it to print the list or not sure if it is actually storing it. This is what i have so far.
#include <iostream> #include <fstream> #include <cstdlib> #include <string> using namespace std; int main() { char names[5][100];
I am trying to create a generic map container which shell contain data of different datatypes. I already found a solution in this forum here:
[URL]...
Introducing a container with a Base Class as content type and inserting objectes of Derived Class types from that Base Class suites my implementation very well. But it is not really working. I implemented it this way:
class MyType { public: MyType() {} virtual ~MyType() {} }; template <class PT> class ParseType : public MyType
[Code]...
Then I insert one element.
// index being an object of type Parser<string> ParseType<string>* test = new ParseType<string>( index ); // and index.val(0) = "-n" iMap.insert( pair< string, MyType* >( index.id(0), test ) );
Now I think I should be able to call
const string key("-n"); iMap.at(key)->content->val(n); Or iMap.at(key)->get_val(n);
But neither one compiles with the error that "class MyType" (as the map container is pointing to a MyClass object) has no member/member function "content"/"get_val".
I also tried introducing member and member function "content" and "get_val" in "class MyType", which I thought should be hidden while constructing ParseType<string>. This would compile but it does not call the member "content or member function "get_val" from class ParseType.
A second try was to remove templatization of "class ParseType" and introduce a specific, let's say, "class ParseString" for objects of type Parser<string>. But the problems remain the same either the compiler complains due to missing member/member function or retreiving the map content will not call the derived class member/member function.
And all of the above information is one person. As is "John,Peter,23" is first name, last name, age. When there are multiple people in a csv file how can i parse through and separate the information in a string and int for later use?
Im tasked with reading a data file, this is an example snippet
list of trophy winners year facup leaguecup 1stdiv 2ndiv 1960/61 Tottenham Hotspur Aston Villa Tottenham Hotspur Ipswich Town 1961/62 Tottenham Hotspur Norwich City Ipswich Town Liverpool 1962/63 Manchester Utd Birmingham City Everton Stoke City
The file starts in 1892 and is up to 2011/12, there is data missing for some years due to the wars etc,
once ive read the file, i need to store the data, for me to re-use.
There are a lot of useful link regarding reading data in, but they tend to be with very small files with say 10 lines of numbers.
I'm redesigning some code and I'm wondering what the best ways to store and access certain data is. The characteristics are as followed:
1) Based on data from a file, a distance matrix (N x N) is calculated. 2) Once the matrix is complete, the data will never change (unless a new file is read, but I can work around that by iteratively calling the problem with a new datafile on as command line parameter). 3) The data from the matrix is accessed billions of times from pretty much every other line of code.
In my old version, I had a class "Data" which a sub-class "Data::Distance" and I would put a reference in every other class that needed it. Now, my class hierarchy will be much flatter (basically all logic will be in one class; other classes will be POD structs).
Given the characteristics of the Distance table, is there a way to store them in a very efficiently-accessible way? Does it matter if it's stored in the main class where all the action happens in contrast to being a different class? Does making it static improve the performance? Casting it to const? Anything?
Again, the data is accessed billions of times so even minor differences can save a lot of time.
I have to store large amount of data and retrieve the same data then write into file in C++. Currently I am using vector to store and retrieve. But vector is taking more time to store and retrieve the element. Is any other best data structure to store and retrieve large amount of data in unordered way?
I'm having trouble reading my data from a .txt file into a structure of the format shown in my code. I've made my student database in the program below based on user input and I didn't have a problem with that, but now it's come to input from a file it's making it difficult.
My three tasks are:
(1) A table containing 1 row per student, containing the student ID number and all of the student's marks.
(2) Another table, containing 1 row per student, containing the student ID number and the average mark obtained by that student.
(3) The average mark for each subject.
The assumptions to be made are: The student ID can begin with a zero, and should therefore be read in as a string.The test data will contain 20 students but the program should be able to deal with up to 100 students.Assume there are no more than 4 different subjects.
So based on the first assumption I've arranged the data in the file in an order in which the student ID begins with a zero:
...for twenty students, 80 lines of data. Now, on the assumption they must be read in as strings, this is what's making it tricky to store in the structure because, I've got 80 ID numbers but 20 repeat themselves 4 times. Once I've got this data in the structure below the tasks I won't have a problem with because I can just base it on a user input program but the data's already stored instead.
Below is my code for user input associated with task (1). In this example the IDs are stored as ints but for the file they will be strings. It compiles fine, displays the data as shown in the assignment sheet, but I don't know how to get the data into my structure. I can store the data in a structure of three arrays using fscanf() no problem, but it's not very "workable" for what I need to do with it.
I am trying to read a file use the data line by line to create into an object. The current file I have is like this and the code reading the file will be found below.
1223 Fake1 Name1 60 70 80 24 89 add1 Male 1224 Fake2 Name2 61 70 81 80 24 add2 Male 1225 Fake3 Name3 63 70 82 80 89 add3 Male 1226 Fake4 Name4 63 70 83 80 88 add4 Male
The problem I am having is that I need to put delimiters in the file so that a person can have more than one name and also the address can now hold multiple strings until the delimiter.
I would like to change my file to this;
1223 : Fake1 Name1 : 60 : 70 : 80 : 24 :89 : This will be address1 : Male 1224 : Fake2 Name2 : 61 : 70 : 81 : 80 :24 : This will be address2 : Male 1225 : Fake3 Name3 : 63 : 70 : 82 : 80 :89 : This will be address3 : Male 1226 : Fake4 Name4 : 63 : 70 : 83 : 80 :88 : This will be address4 : Male
How can I update the code below so that it can use the delimiters to create an object?
void loadFile(Person people[], int* i) { ifstream infile("people2.txt"); if ( !infile.is_open()) { // The file could not be opened cout << "Error";
Following function is from a bank account, when new account is created it won't store it in the array or i guess it does but find_acct function wont be able to find it.
find_acct Function:
int findacct(int acctnum_array[], int num_accts, int requested_account) { for (int index = 0; index < num_accts; index++) if (acctnum_array[index] == requested_account) return index; return -1;
I am reading data from a text file into a program. I am well aware of the subtle distinctions in the mode of data input/entry when using the stream extraction operator, the get() function, and the getline() function.
My problem is that all of them do not read and/or store the newline character alongside the data read!
Any function that reads and stores data and the terminating newline character together??
struct Wine { string wineName; int vintage; int rating; double price; };
how can i store the file data below in an array with the structure type???
Dow Vintage Port ;2011;99;82 Mollydooker Shiraz Carnival of Love ;2012;95;75 Prats & Symington Douro Chryseia ;2011;97;55 Quinta do Vale Meão Douro ;2011;97;76 Leeuwin Chardonnay River Art Series ;2011;96;89
keep getting "deferencing pointer to incomplete type" on the bold lines:
main: int main(int argc, char *argv[]) { printf("Please think of an animal. I will try to find out what it is by asking you some yes/no questions."); struct treenode *root = mkTreeNode("Is it a reptile? ", NULL, NULL); struct treenode *selectedNode = root; root->left = mkTreeNode("Does it have legs?
I've been writing the math functions for a 3d game and tried compiling it at about 30 functions in. I get this error related to my pointers to my structures. it affects almost everything in all my functions (as youll see by looking at how i do the math in the function below). The compiler gives me the error
"error: dereferencing pointer to incomplete type"
on all my struct Type4D pointers but referencing the values in my struct TypeMatrix4X4 using pointers seems to work fine i think (it doesn't seem to complian explicitly about it. so here is the important code...
I am having trouble with this program I get the error dereferencing pointer to incomplete type in the populate function I am using BloodShed's Dev C++ compiler v4.9.9.2 I copied this program out of a book because I was having a problem with a linked list in a similar program. I think there is a problem with the compiler not supporting these types of pointer's in a function.