Thread: void * -> int

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    104

    void * -> int

    hello,

    i am having the following problem: in my code, i have an array of void*, and to that void* i am currently adding int data-types, but after i cast them as an int and print them out, the output is: 2293596

    i would give you my whole code but it is very, very long. i'll write up an example to illustrate my problem:

    Code:
    #include <stdio.h>
    
    void **v;
    int i = 0;
    
    void set(void *vv)
    {
        v[i++] = vv;
    }
    
    void *get(int index)
    {
        return v[index];
    }
    
    int main()
    {
        int a = 5;
        set((void *)&a);
        printf("%d\n", (int)get(0));
        getchar();
        return 0;
    }
    my expected output would be 5, but it is 2293596.

    any help would be greatly appreciated!

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You need to dereference the pointer, not print the memory address it contains.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    I couldn't even get this to compile, and when I did it seg faulted. But I made a couple changes and now the answer is 5:

    Code:
    #include <stdio.h>
    
    void **v;
    int i = 0;
    
    void set(void *vv)
    {
        v = vv;
    }
    
    void *get(int index)
    {
        return v[index];
    }
    
    int main()
    {
        int a = 5;
        set((void *)&a);
        printf("%d\n", (int*)get(0));
        getchar();
        return 0;
    }
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by abraham2119
    i am having the following problem: in my code, i have an array of void*, and to that void* i am currently adding int data-types, but after i cast them as an int and print them out, the output is: 2293596
    The first problem is that there is no array in the example. The next problem is that you cast the return value of get(), which is a pointer, directly to an int, and hence you are merely converting an address to int. MK27's example is on the right track, but that converts a void* to an int* and then prints the address. What you want to do is take it a step further and dereference the converted pointer:
    Code:
    #include <stdio.h>
    
    void *v[10]; /* Some arbitrarily sized array of void pointers. */
    int i = 0;
    
    void set(void *vv)
    {
        v[i++] = vv;
    }
    
    void *get(int index)
    {
        return v[index];
    }
    
    int main()
    {
        int a = 5;
        set(&a);
        printf("%d\n", *(int*)get(0));
        getchar();
        return 0;
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Part of the problem stems from the fact that you're not dereferencing the pointer after casting it to an int, as others have pointed out, and part is equating global void **v with local void *v. After function set() returns its stack frame is popped; linkage to vv is broken; and it becomes the case of the dangling pointer. Changes to original are shown in red:
    Code:
    #include <stdio.h>
    
    void **v;
    void *vv;
    int i = 0;
    
    void set(void *x)
    {
        vv = x;
        v = &vv;
    }
    
    void *get(int index)
    {
        return v[index];
    }
    
    int main()
    {
        int a = 5;
        set((void *)&a);
        printf("%d\n", *(int *) get(0));
        getchar();
        return 0;
    }

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by itCbitC View Post
    Part of the problem stems from the fact that you're not dereferencing the pointer after casting it to an int, as others have pointed out, and part is equating global void **v with local void *v. After function set() returns its stack frame is popped; linkage to vv is broken; and it becomes the case of the dangling pointer. Changes to original are shown in red:
    Code:
    #include <stdio.h>
    
    void **v;
    void *vv;
    int i = 0;
    
    void set(void *x)
    {
        vv = x;
        v = &vv;
    }
    
    void *get(int index)
    {
        return v[index];
    }
    
    int main()
    {
        int a = 5;
        set((void *)&a);
        printf("%d\n", *(int *) get(0));
        getchar();
        return 0;
    }
    And where is **v pointing?

    Note also that storing the address of a local variable is possibly dangerous - if you leave the function that has the local variable, then the stack-space that holds the variable will be freed up and reused by other functions, so you will get random changes to the value (yes, I know, you will not leave main before the program finishes - but in case someone thinks that this pattern can be used without restriction).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Dec 2008
    Posts
    104
    thanks for all your help!

    but i try doing this in my actual program and it's still printing out the same value. i managed to get some of my program's code onto here.

    Code:
    #include <stdlib.h>
    
    typedef struct {
        int index;
        int max;
        void **a;
    } list;
    
    list arraylist()
    {
        list alist;
    
        alist.a = malloc(10);
        alist.max = 10;
        alist.index = 0;
    
        return alist;
    }
    
    void add(list *l, void *obj)
    {
        if (l->index > l->max)
            realloc(&(l->a), l->max*2);
     
        l->max *= 2;
        l->a[l->index++] = obj;
    }
    
    void delete(list *l, int index)
    {
        l->a[index] = NULL;
    }
    
    void *get(list *l, int index)
    {
        return l->a[index];
    }
    
    void clear(list *l)
    {
        free(l->a);
    }
    
    int main()
    {
        list l = arraylist();
        int i = 0;
        for (; i < 100; i++) 
            add(&l, &i);
    
        printf("COMPONENTS ADDED\n");
    
        for (i = 0; i < 100; i++)
            printf("%d\n", (int *) get(&l, i));
    
        clear(&l);
    
        getchar();
            
        return 0;
    }

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, you are not dereferencing the memory address stored in add when you get it out with get().

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by matsp View Post
    And where is **v pointing?
    global "v" points to local too, workaround to the dangling pointer issue as main() gets popped only when the process ends.
    alternatively the local variable "a" can be pushed to the data segment but figure I will leave that to the OP to decide.
    Quote Originally Posted by matsp View Post
    Note also that storing the address of a local variable is possibly dangerous - if you leave the function that has the local variable, then the stack-space that holds the variable will be freed up and reused by other functions, so you will get random changes to the value (yes, I know, you will not leave main before the program finishes - but in case someone thinks that this pattern can be used without restriction).

    --
    Mats
    I agree

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by itCbitC
    global "v" points to local too, workaround to the dangling pointer issue as main() gets popped only when the process ends.
    alternatively the local variable "a" can be pushed to the data segment but figure I will leave that to the OP to decide.
    If you read my response in post #4 and abraham2119's own post #7, you will see that the real problem is that there is no array to begin with, despite the introductory phrase "i have an array of void*". abraham2119 corrected this in the actual code, but did not add tabstop's suggestion (which I unknowingly echoed) to dereference the pointer.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    Dec 2008
    Posts
    104
    what a stupid mistake of mine not to dereference the pointer! thank you all!

    fixed like this:
    printf("%d\n", *((int *) get(&l, i)));

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can someone help me with these errors please code included
    By geekrockergal in forum C Programming
    Replies: 7
    Last Post: 02-10-2009, 02:20 PM
  2. Replies: 3
    Last Post: 05-13-2007, 08:55 AM
  3. linked list problem
    By kzar in forum C Programming
    Replies: 8
    Last Post: 02-05-2005, 04:16 PM
  4. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 05:26 PM
  5. A Simple (?) Problem
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 10-12-2001, 04:28 AM