Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Continuous buffer accessed by pointers in the struct

Status
Not open for further replies.

Vesseli

Programmer
Sep 1, 2012
3
FI
Hi,

I'm having problems of accessing to continuous buffer with pointers in the struct. My purpose is to allocate
the continuous buffer and access to the buffer with the pointers in the struct.

So it seems that there is something wrong probably with getting the pointers to current_code_ptr
and new_code_ptr.

So my question is that how to assign pointers to current_code_ptr and new_code_ptr ?

The reason why I'm asking help is that I found it very usefull to have continuous buffer accessible with
pointers containing the correct offset.

You can copy paste the code below and try it.

Thanks for your help.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define CODE_LENGTH 10

typedef struct
{
char* current_code_ptr;
char* new_code_ptr;
}CHANGE_DATA_STRUCT;
int main(void)
{
char code[CODE_LENGTH] = "Hello";
CHANGE_DATA_STRUCT* change_code_ptr = malloc(sizeof(CHANGE_DATA_STRUCT) +
CODE_LENGTH +
CODE_LENGTH);
memset(change_code_ptr, 0, sizeof(CHANGE_DATA_STRUCT) + (CODE_LENGTH * 2));

/* Get pointers. Something wrong here ????? */
change_code_ptr->current_code_ptr = (char *)change_code_ptr;
/* Get the pointer to the second offset */
change_code_ptr->new_code_ptr = change_code_ptr->current_code_ptr + CODE_LENGTH;

/* Here Visuall c++ claims about the bad ptr(CXX0030: Error, expression cannot be evaluated)*/
memcpy(change_code_ptr->current_code_ptr, code, CODE_LENGTH);
memcpy(change_code_ptr->new_code_ptr, code, CODE_LENGTH);

/* The execution crashes in above line with the following message.
Unhandled exception at 0x594aedfc in testing.exe: 0xC0000005: Access violation writing location 0x0000006f. */

return 1;
}
 
Yes, that's wrong:
Code:
change_code_ptr->current_code_ptr = (char *)change_code_ptr;
You set current_code_pointer to the origin of the whole structure. Now it points to himself (see your structure definition). The next memset overwrites both pointers: this call writes 10 bytes of code array over 8 bytes of pointers. Now all your pointers in the structure are garbage. Next step: inevitable crash...

Some (good;) advices:
- Don't use all capitals names for types. As usually, such names in C are intended for macros or library types.
- Don't initialize pointer variables via memset calls. Null pointer value is not the same as all zero bytes bit pattern in C.
- Use code tag for your snippets (click ? icon on the posting panel for help on TGML markup).

Good luck!
 
Hi,

Thanks for the tips. I now got the reason for the failure. I was a little bit "wooden eye" :).

I find it handy to have only one buffer and then just provide the corresponding struct to the counterpart
component to access to the data. One malloc and one free instead of several.

The solution is the following.

/* First eight bytes contain the struct pointers then the rest is to the payload */
change_code_ptr->current_code_ptr = (char *)change_code_ptr + 8;
change_code_ptr->new_code_ptr = change_code_ptr->current_code_ptr + CODE_LENGTH;
 
I can't understand roles of current_code_ptr and new_code_ptr. If you want to use this header+paiload container as some kind of self-made heap surrogate, where is max size of paiload part? Where is functions which can allocate and free new chunks in this container?

In other words, what's your true problem (except obvious error in OP code)?..
 
The piece of code I attached is actually out from real context. The container is to be used in an embedded system. The sender allocates and the reseceiver frees the buffer. Now the whole buffer is allocated and freed only once instead of three times.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top