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

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Oct 2006
Posts: 38
Reputation: asifjavaid is an unknown quantity at this point 
Solved Threads: 0
asifjavaid asifjavaid is offline Offline
Light Poster

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

 
0
  #1
Jun 11th, 2009
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

  1. FILE *f_in = NULL;
  2. f_in = fopen(argv[f],"rb"
  3. );
  4. if
  5. ( f_in == NULL )
  6. {
  7. fprintf(fptr,"ERROR: Could not open '%s'\n"
  8. , argv[f] );
  9. fclose(fptr);
  10. return
  11. 1;
  12. }
  13.  
  14. int
  15. buf_size = 1428480;
  16. void
  17. *buf_ptr = NULL;
  18.  
  19. buf_ptr = malloc(buf_size+1);
  20. items = fread(buf_ptr,buf_size,1,f_in);
  21.  
  22. void
  23. * 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
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,968
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is offline Offline
Posting Virtuoso

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

 
1
  #2
Jun 11th, 2009
>Please guide me how can I access the data of a void type buffer?

Normally malloc is used in a way like this:
  1. int *p;
  2. 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 by malloc is casted to an integer pointer
int *p;
p = (int *) malloc( 50 * sizeof( int ) );

If malloc 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:
  1. int *p;
  2. p = (int *) malloc( 50 * sizeof( int ) ); // allocate space for 50 integers
  3.  
  4. if( !p ) {
  5. // out of memory
  6. // the memory could not be allocated
  7. }

BTW, Don't forget to free the allocated memory as well, otherwise you'll have memory leaks in your program

Hope this helps!
Last edited by tux4life; Jun 11th, 2009 at 7:26 am.
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

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

 
0
  #3
Jun 12th, 2009
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:
  1. char* pchar = buf_ptr;
  2. char* ptr = pchar + 20;
  3. ...
  4. ...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).
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 10
Reputation: Beair.GQ is an unknown quantity at this point 
Solved Threads: 2
Beair.GQ Beair.GQ is offline Offline
Newbie Poster

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

 
1
  #4
Jun 13th, 2009
  1. 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
Last edited by Beair.GQ; Jun 13th, 2009 at 2:11 am.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 793
Reputation: siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of 
Solved Threads: 135
siddhant3s's Avatar
siddhant3s siddhant3s is offline Offline
Master Poster

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

 
0
  #5
Jun 13th, 2009
@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
Siddhant Sanyam
(Not posting much)
Migrate to Standard C++ :When to tell your C++ Code is Non-Standard.
Please Read before posting: How To Ask Questions The Smart Way
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,722
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 736
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

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

 
2
  #6
Jun 13th, 2009
>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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 793
Reputation: siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of 
Solved Threads: 135
siddhant3s's Avatar
siddhant3s siddhant3s is offline Offline
Master Poster

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

 
0
  #7
Jun 13th, 2009
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. int main()
  5. {
  6. void* buf_ptr = NULL;
  7. buf_ptr = malloc(1428480);
  8. if(buf_ptr)
  9. {
  10.  
  11. printf("%p \n",buf_ptr);
  12. void* p;
  13. p=buf_ptr+1;
  14. printf("%p \n",p);
  15. }
  16.  
  17. return 0;
  18. }
  1. siddhant3s@Xion:~$ gcc testerc.c -o testerc.exe -ansi -pedantic
  2. testerc.c: In function ‘main’:
  3. testerc.c:11: warning: ISO C90 forbids mixed declarations and code
  4. testerc.c:12: warning: pointer of type ‘void *’ used in arithmetic
  5. siddhant3s@Xion:~$ ./testerc.exe
  6. 0xb7c97008
  7. 0xb7c97009

You win....... The code compiles though. But you are correct.
Siddhant Sanyam
(Not posting much)
Migrate to Standard C++ :When to tell your C++ Code is Non-Standard.
Please Read before posting: How To Ask Questions The Smart Way
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,722
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 736
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

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

 
2
  #8
Jun 13th, 2009
>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.
Last edited by Narue; Jun 13th, 2009 at 4:48 pm.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 38
Reputation: asifjavaid is an unknown quantity at this point 
Solved Threads: 0
asifjavaid asifjavaid is offline Offline
Light Poster

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

 
0
  #9
Jun 16th, 2009
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.
  1. void* buf_ptr = NULL;
  2. buf_ptr = malloc(1428480);
  3. strcpy((char*)buf_ptr,"Asif Javaid");
  4. char *p = (char*)buf_ptr;
  5. 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.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC