Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Continuous buffer accessed by pointers in the struct

Continuous buffer accessed by pointers in the struct

Continuous buffer accessed by pointers in the struct


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;
int main(void)
char code[CODE_LENGTH] = "Hello";
CHANGE_DATA_STRUCT* change_code_ptr = malloc(sizeof(CHANGE_DATA_STRUCT) +
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;

RE: Continuous buffer accessed by pointers in the struct

Yes, that's wrong:


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!

RE: Continuous buffer accessed by pointers in the struct


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;

RE: Continuous buffer accessed by pointers in the struct

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)?..

RE: Continuous buffer accessed by pointers in the struct

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.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close