954,164 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How to index the void pointer buffer in VC++?

Hi all,
I working in VC++. I have a void pointer. I am assigning a memory block to it using malloc().
void* buf_ptr = NULL;
buf_ptr = malloc(1428480);

I am filling this buffer using fread(). and I want to index this buffer (buf_ptr) to acces its data. How can I do that.
I am facing the following errors.

Error 24 error C2036: 'void *' : unknown size c:\AMT_DLL_tester.cpp
Error 25 error C2440: '=' : cannot convert from 'void' to 'void *' c:\AMT_DLL_tester.cpp


Here is my source code

FILE *f_in = NULL;
f_in = fopen(argv[f],"rb"
);
if
( f_in == NULL )
        {
            fprintf(fptr,"ERROR: Could not open '%s'\n"
, argv[f] );
            fclose(fptr);
            return
 1;
        }

int
 buf_size = 1428480;
void
 *buf_ptr = NULL;

buf_ptr = malloc(buf_size+1);
items = fread(buf_ptr,buf_size,1,f_in);

void
* ptr = buf_ptr[20]; //----------->error

Please guide me how can I access the data of a void type buffer?

Your help will be highly appreciated..

Many Thanks.


Asif Javaid

asifjavaid
Light Poster
40 posts since Oct 2006
Reputation Points: 10
Solved Threads: 0
 

>Please guide me how can I access the data of a void type buffer?

Normally <strong>malloc</strong> is used in a way like this:

int *p;
p = (int *) malloc( 50 * sizeof( int ) );  // allocate space for 50 integers


As you can see in the example above/below, the void pointer which is returned bymalloc is casted to an integer pointer

int *p;
p = <strong>(int *)</strong> malloc( 50 * sizeof( int ) );


Ifmalloc couldn't allocate the memory, then it returns a NULL-pointer, but using a NULL-pointer will almost certainly crash your whole program, that's why it's generally recommended to check the return value of malloc before using the pointer where you've tried to assign memory to...

Checking whether the allocation has succeeded can be achieved by something like this:

int *p;
p = (int *) malloc( 50 * sizeof( int ) );  // allocate space for 50 integers

if( !p ) {
    // out of memory
    // the memory could not be allocated
}


BTW, Don't forget tofree the allocated memory as well, otherwise you'll have memory leaks in your program :)

Hope this helps!

tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243
 

In C you can assign void* type pointer value to the pointer of any type without casting. So simply declare a proper type pointer and set it to the buffer:

char* pchar = buf_ptr;
char* ptr = pchar + 20;
...
...pchar[20] the same as *ptr...

It's impossible to subscript void* pointer because sizeof(void) is not defined (type void has an empty set of values).

ArkM
Postaholic
2,001 posts since Jul 2008
Reputation Points: 1,234
Solved Threads: 348
 
void* ptr = buf_ptr[20]; //----------->error

what i want to say is "buf_ptr[20]" is not a pointer,it's
the value of the 20th in buf_ptr

Beair.GQ
Newbie Poster
9 posts since Dec 2007
Reputation Points: 40
Solved Threads: 2
 

@Tux4life
Your beautiful post has nothing to do with OP's problem. The construct use for allocating space using malloc is absolutely valid in C.

The only problem is on line:
void* ptr = buf_ptr[20];
as Beair.GQ said, you are assigning a void to void*. The type of buf_ptr[i] is not that of a pointer.
You should perhaps try this:
void* ptr=but_ptr+20;//this is good

siddhant3s
Practically a Posting Shark
816 posts since Oct 2007
Reputation Points: 1,486
Solved Threads: 140
 

>You should perhaps try this:
>void* ptr=but_ptr+20;//this is good

Perhaps you should try it first. You have no excuse for giving an answer that will never compile on a conforming C compiler.

ArkM gave the correct answer (and corrected tux's misconception about casting malloc). Alternatively, you can cast the pointer to void to a pointer to an appropriate type, but regardless of how you do it, void* has to be converted to T* to do anything meaningful with it, where T is a non-void type.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 
#include<stdio.h>
#include<stdlib.h>

int main()
{
    void* buf_ptr = NULL;
    buf_ptr = malloc(1428480);
    if(buf_ptr)
    {

        printf("%p \n",buf_ptr);
        void* p;
        p=buf_ptr+1;
        printf("%p \n",p);
    }

    return 0;
}
siddhant3s@Xion:~$ gcc testerc.c -o testerc.exe -ansi -pedantic  
testerc.c: In function ‘main’:
testerc.c:11: warning: ISO C90 forbids mixed declarations and code
testerc.c:12: warning: pointer of type ‘void *’ used in arithmetic
siddhant3s@Xion:~$ ./testerc.exe
0xb7c97008 
0xb7c97009


You win....... The code compiles though. But you are correct.

siddhant3s
Practically a Posting Shark
816 posts since Oct 2007
Reputation Points: 1,486
Solved Threads: 140
 

>The code compiles though.
What you're seeing is a silent extension where the compiler treats the size of void as 1. GCC is wrong to allow it in pedantic mode as it's a constraint violation in the standard.

>warning: ISO C90 forbids mixed declarations and code
This should be an error as well.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

Hi all, Thanks very much for alls participation.
Arkm, yes you are right I can access the contents of void* buf_ptr by doing the following.

void* buf_ptr = NULL;
buf_ptr = malloc(1428480);
strcpy((char*)buf_ptr,"Asif Javaid");
char *p = (char*)buf_ptr;
printf("%c",*(p+1));


siddhant3s, your code can compile in gcc compiler but I am using Microsoft VC++9.0 comipler and it is generating an error of unknown size, as I do void*p = buf_ptr+1;

Many Thanks all.

asifjavaid
Light Poster
40 posts since Oct 2006
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You