C++ :: Dereferencing Void Pointers Through The Way Of Typing It
Jan 11, 2014
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 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 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 am trying to add data to a queue with the following simplified code:
Code: typedef struct Queue { void * data; int head; int tail; int elementSize;
My question is, how do I move the queue->data pointer to the correct memory location in order to copy given data to head? The code above inside memcpy gives med the error: "expression must be a pointer to a complete object type".
Do I need an extra pointer to be able to navigate between the queue's head and tail, and keep queue->data as a reference to the first byte of the allocated memory, or is it possible with only queue->data?
Edit. Just noticed I have mixed up head and tail. The enqueued data should probably go to the Queue's tail and not the head. However, the problem is still the same.
I'm having issues with pointers and relationship operators in C.
I need to find a max and min value in a void function using pointers. max and min would work if they had values. mul works, because you can just do math operations with pointers.
There are 0 errors and warnings; but max and min are never going to work as is.
Clearly I'm missing something.
#include <stdio.h> #include <stdlib.h> void max(int *a, int *b, int *c, int *d, int *result); void min(int *a, int *b, int *c, int *d, int *result); void mul(int *a, int *b, int *c, int *d, int *result); int main()
[Code]...
Your job will be to create a program that uses pointers. Your output must be done in the main function and the calculations MUST be done in the three functions. Therefore you MUST use pointers correctly.
You must declare and implement the following 3 functions. Below are the three prototypes that you must use in this program.
void max(int *a, int *b, int *c, int *d, int *result); void min(int *a, int *b, int *c, int *d, int *result); void mul(int *a, int *b, int *c, int *d, int *result);
The functions have the following meaning:
max finds the max value of a,b,c,d and stores the largest value in result. min finds the min value of a,b,c,d and stores the largest value in result. mul multiplies a * b * c and divides by d. Stores that value in result.
Below is an example input/output. This input will be read in via the keyboard (use scanf).
input output (note that user input is shown in bold) 1 2 3 4 Enter the 4 numbers: 1 2 3 4 The max is 4. The min is 1. (a * b * c) / d = 1 100 3 201 103 Enter the 4 numbers: 100 3 201 103 The max is 201. The min is 3. (a * b * c) / d = 585
Your output MUST match exactly the output below for the input from above. Your program must compile, failure to do so will result in 0 points. */
I am trying to find the distance between two void pointers, so I can follow this distance to a certain pointer in a vector when given only the previous element in that vector.
int distance = (char*) prev - (char*) first; next = (char*) cv->elems + cv->elemsz + distance;
Basically, prev and first are void pointers. I am trying to cast them into a char, subtract the first element in the vector from the previous one, and then use this distance to determine what the next element in the vector is. However, it is not working. I am not sure how to do this. To complicate matters, prev is a const void *.
That this will execute those binary instructions in hexadecimal notation BUT WHY? I don't get why that works since that's an array of data not a function?
i have read a lot of about lists but i dont understand this. I know its something like dogs on leash where we have
dog1->dog2->dog3->.... and Code: struct DOG {char* (name of a dog of first leash) DOG* (next dog ) } I have written something like this but this doesnt work as i wanted Code: #include <iostream> using namespace std; struct line {
[Code]....
I wanted to make program where i can type XX numbers , then cout those numbers without changing the order, and my next exercise is to change order in this programme from end to start.
Everytime I type a character, the number 1 appears in the next line. And i just keep getting the message "Wrong! I have more than that." even when I type a number bigger than 1023
Code:
#include <stdio.h> #include <stdlib.h> #include <time.h> int main(void) { srand(time(NULL));
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.
This is the main header with three classes, in summary ADNodeInstance is a data holder, ADNode is encapsulating a pointer to ADNodeInstance, and ADGraphBuilder is a main class which holds all the ADNodeInstances and manages them.:
From this all I could infer is that in the funcreateGradientMessage on the switch for TANH the segfault occurs for the expression: directGradient * child * (1 - child). From the output I can see that this is what happens in order:
Unary negation on node 5 resulting in 15Addition of node 16 and 15 (e.g. the brackets) resulting in 17trying to multiply 14 and 5 - SEGFAULT something wrong with 5
So my question is what exactly is happening? I tried to understand but can't.
I am a little confused while comparing char pointers to integer pointers. Here is the problem:
Consider the following statement; char *ptr = "Hello"; char cArr[] = "Hello";
When I do cout << ptr; it prints Hello, same is the case with the statement cout << cArr;
As ptr and cArr are pointers, they should print addresses rather than contents, but if I have an interger array i.e. int iArr[] = {1, 2, 3};
If I cout << iArr; it displays the expected result(i.e. prints address) but pointers to character array while outputting doesn't show the address but shows the contents, Why??
I have a void that needs to end a program but a break and return 0 both won't work. Instead I have it cout (1/0). It works but is there an alternative?
Why is it not okay to return void? Most compilers will probably let you (gcc does) but it gives you a warning that you aren't supposed to. Most languages allow you to return void.
Something like
Code: void log(const std::string & txt){ std::cout << txt << std::endl; } //C++ way to do it void bar(int i){