LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-23-2022, 11:20 PM   #1
sanfter
LQ Newbie
 
Registered: Jan 2022
Posts: 4

Rep: Reputation: 0
Unhappy Error in accessing to the content of the memory a pointer points to


Dear friends,

I have some arrays of various data types and after converting them into a generic form I want to write them into a file byte by byte.
To do that, I refer to them using a void pointer and then I try
to write the content of it using a character pointer.

Actually I've written something like the following code but I get the segmentation fault error.


Code:
long int  *li_ptr;
char  *c_ptr;
void  *v_ptr;

char buffer[100];

*(li_ptr+0)=100;
*(li_ptr+1)=200;
*(li_ptr+2)=300;



v_ptr=li_ptr;   //  point to the content of long int pointer

c_ptr =(char *)v_ptr;  // point to the content of void pointer

buffer[0]= *(c_ptr+0); //  I get error on this line
Could you please give me a suggestion and help me solve the issue?

regards

Last edited by sanfter; 01-23-2022 at 11:51 PM.
 
Old 01-23-2022, 11:45 PM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,863
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
In this example you don't assign value to your pointers so they don't point to any data. Provide a more complete example. (Also please use [code] and [/code] tags.)
 
1 members found this post helpful.
Old 01-23-2022, 11:53 PM   #3
sanfter
LQ Newbie
 
Registered: Jan 2022
Posts: 4

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by NevemTeve View Post
In this example you don't assign value to your pointers so they don't point to any data. Provide a more complete example. (Also please use [code] and [/code] tags.)
Thank you for the guidance. I edited and updated my question.
 
Old 01-24-2022, 12:06 AM   #4
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,264
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
Welcome to LQ and the Programming forum!

Please wrap your code and data snippets inside [CODE]...[/CODE] tags. Doing so will preserve indentation and provide other visual clues which make it easier for others to comprehend. You may write those yourself as shown, or use the # button available with Advanced edit options. (A complete list of BBCode tags is always available via a link near the bottom of every thread view).

Quote:
Originally Posted by sanfter View Post
I have some arrays of various data types and after converting them into a generic form I want to write them into a file byte by byte.
Please explain exactly what you mean by this statement, particularly "generic form".


Quote:
Originally Posted by sanfter View Post
To do that, I refer to them using a void pointer and then I try
to write the content of it using a character pointer.
You can only access the data by its actual type, so casting a void pointer to a char * will only work it the pointer actually points to a char object.

Code:
long int  *li_ptr;
char  *c_ptr;
void  *v_ptr;

char buffer[100];

v_ptr=li_ptr;   //  point to the content of long int pointer

c_ptr =(char *)v_ptr;  // point to the content of void pointer

buffer[0]= *(c_ptr+0); //  I get error on this line
The first problem is that you never initialized any of those pointers, so any attempt to dereference them will result in undefined behavior - usually resulting a segmentation fault - attempt to reference illegal memory!

But even assuming that you had initialized li_ptr to point to an actual long integer type object, you cannot simply cast it to a char * and dereference it and get a meaningful result.

Please try to explain what you really want to accomplish with example data.

Last edited by astrogeek; 01-24-2022 at 12:07 AM. Reason: tpoys
 
Old 01-24-2022, 12:22 AM   #5
sanfter
LQ Newbie
 
Registered: Jan 2022
Posts: 4

Original Poster
Rep: Reputation: 0
Smile

Quote:
Originally Posted by astrogeek View Post
Welcome to LQ and the Programming forum!

Please wrap your code and data snippets inside [CODE]...[/CODE] tags. Doing so will preserve indentation and provide other visual clues which make it easier for others to comprehend. You may write those yourself as shown, or use the # button available with Advanced edit options. (A complete list of BBCode tags is always available via a link near the bottom of every thread view).



Please explain exactly what you mean by this statement, particularly "generic form".




You can only access the data by its actual type, so casting a void pointer to a char * will only work it the pointer actually points to a char object.

Code:
long int  *li_ptr;
char  *c_ptr;
void  *v_ptr;

char buffer[100];

v_ptr=li_ptr;   //  point to the content of long int pointer

c_ptr =(char *)v_ptr;  // point to the content of void pointer

buffer[0]= *(c_ptr+0); //  I get error on this line
The first problem is that you never initialized any of those pointers, so any attempt to dereference them will result in undefined behavior - usually resulting a segmentation fault - attempt to reference illegal memory!

But even assuming that you had initialized li_ptr to point to an actual long integer type object, you cannot simply cast it to a char * and dereference it and get a meaningful result.

Please try to explain what you really want to accomplish with example data.

Dear Astrogeek

after updating my question, the li_ptr is pointing to some real data and my aim is to get various data types in a function in form of an array (pointer to long int, int, double and ... )and write the input data byte by byte in a file.
 
Old 01-24-2022, 12:23 AM   #6
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,264
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
Quote:
Originally Posted by sanfter View Post
Code:
long int  *li_ptr;
char  *c_ptr;
void  *v_ptr;

char buffer[100];

*(li_ptr+0)=100;
*(li_ptr+1)=200;
*(li_ptr+2)=300;
That is not a valid use of the li_ptr pointer - it still has never been initialized to point to any object of type long int, but you are trying to dereference it to initialze some value. You also appear to be treating it as an array, but it was not defined to be an array.

Here is a simple example which defines two objects of type long int named li_val and li_val2, and a pointer to long int named li_ptr. It then initializes li_val with a value of 100, and initializes the pointer with the address of the object. It then dereferences the pointer to get the value of li_val to assign it to li_val2.

Code:
long int li_val, li_val2;
long int *li_ptr;

li_val = 100;
li_ptr = &li_val;

li_val2 = *li_ptr;
But before we make too any guesses about what you are trying to accomplish, an example would be helpful!
 
Old 01-24-2022, 02:01 AM   #7
sanfter
LQ Newbie
 
Registered: Jan 2022
Posts: 4

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by sanfter View Post
Dear friends,

I have some arrays of various data types and after converting them into a generic form I want to write them into a file byte by byte.
To do that, I refer to them using a void pointer and then I try
to write the content of it using a character pointer.

Actually I've written something like the following code but I get the segmentation fault error.


Code:
long int  *li_ptr;
char  *c_ptr;
void  *v_ptr;

char buffer[100];

*(li_ptr+0)=100;
*(li_ptr+1)=200;
*(li_ptr+2)=300;



v_ptr=li_ptr;   //  point to the content of long int pointer

c_ptr =(char *)v_ptr;  // point to the content of void pointer

buffer[0]= *(c_ptr+0); //  I get error on this line
Could you please give me a suggestion and help me solve the issue?

regards



Dear Friends,

using your suggestions I managed to solve the problem.

Thank you all so much
 
Old 01-24-2022, 02:13 AM   #8
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,264
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
Glad that helped!

LQ is all about sharing, so it would be in the spirit of things to share your solution with others who might land on this thread when looking for help with similar problems.

Good luck and welcome to LQ!
 
Old 01-24-2022, 04:23 PM   #9
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,225

Rep: Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320
I have no idea what you're trying to do or why you're trying to do it, but I wonder if a standard serialization format (like Protocol Buffers or MessagePack) might be worth looking into.

Last edited by dugan; 01-24-2022 at 09:29 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
play free (World of WarCraft , xBox Live, Live Points, Wii Points, Free Habbo) laraaj Linux - Games 1 02-09-2007 05:50 PM
Script for accessing public access points... L1nuxR0x Linux - Wireless Networking 2 04-23-2005 12:40 PM
Pointer to a Pointer Akuma no Houkon Programming 3 06-06-2004 03:51 PM
pointer to pointer question in c lawkh Programming 2 01-29-2004 10:26 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:08 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration