[SOLVED] Error in accessing to the content of the memory a pointer points to
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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?
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.)
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.
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
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
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
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.
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!
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.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.