How to preserve the value contains by char * pointer

Please support our C++ advertiser: Intel Parallel Studio Home
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 preserve the value contains by char * pointer

 
0
  #1
Sep 24th, 2008
hi guys,

i have declared a char pointer. i am using this in itoa() function to convert integer data into hex data e.g
  1. char * hexData= NULL;
  2. itoa(1484820,hexData,16);
this results in equalent of 1484820 in hex decimal and store it in hexData.

now when I want to some process in hexData(char pointer). when I pass it to my function for further process, the value of hexData(char pointer is changed now contains garbage) see the code below

  1. main()
  2. {
  3. char * hexData=NULL;
  4. itoa(148420,hexData,16);
  5. printf("%s",hexData);---------------------//output is 243C4
  6. writeinFile(hexData,fptr);
  7. }
  8.  
  9. writeinFile(char* str, FILE *fptr)
  10. {
  11.  
  12. printf("%s",hexData);---------------------//output is 2434C but some time a garbage value
  13. char *hex = new char[30];
  14. printf("%s",hexData);---------------------//output is always garbage value after any new
  15. //char *pointer declaration.
  16.  
  17. ----
  18. ----
  19. ----
  20. }
  21.  
  22. }


I am confused whats problem going on here.
can any tell me its solution?


Asif
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 119
Reputation: kux is on a distinguished road 
Solved Threads: 10
kux kux is offline Offline
Junior Poster

Re: How to preserve the value contains by char * pointer

 
0
  #2
Sep 24th, 2008
Originally Posted by asifjavaid View Post
hi guys,

i have declared a char pointer. i am using this in itoa() function to convert integer data into hex data e.g
  1. char * hexData= NULL;
  2. itoa(1484820,hexData,16);
this results in equalent of 1484820 in hex decimal and store it in hexData.

now when I want to some process in hexData(char pointer). when I pass it to my function for further process, the value of hexData(char pointer is changed now contains garbage) see the code below

  1. main()
  2. {
  3. char * hexData=NULL;
  4. itoa(148420,hexData,16);
  5. printf("%s",hexData);---------------------//output is 243C4
  6. writeinFile(hexData,fptr);
  7. }
  8.  
  9. writeinFile(char* str, FILE *fptr)
  10. {
  11.  
  12. printf("%s",hexData);---------------------//output is 2434C but some time a garbage value
  13. char *hex = new char[30];
  14. printf("%s",hexData);---------------------//output is always garbage value after any new
  15. //char *pointer declaration.
  16.  
  17. ----
  18. ----
  19. ----
  20. }
  21.  
  22. }


I am confused whats problem going on here.
can any tell me its solution?


Asif
well, hexData points to unallocated memory ...
use:
char hexData[BUFFSIZE];
or
char * hexData = (char*)malloc( BUFFSIZE* sizeof( char));

u get garbage because u use unallocated memory that can be overwritten at any time
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 preserve the value contains by char * pointer

 
0
  #3
Sep 24th, 2008
Alas, you write itoa result to nowhere. Just before itoa call YOU say: my hexData pointer points to NOWHERE (NULL is this NOWHERE).
You must pass a real memory pointer to itoa. Declare a char array or allocate memory chunk by new operator.
Or (better) reread your C or C++ textbook...
Reply With Quote Quick reply to this message  
Reply

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



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC