Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7181299
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T17:31:43+00:00 2026-05-28T17:31:43+00:00

I’m trying to use the memory allocated beyond the size of a struct to

  • 0

I’m trying to use the memory allocated beyond the size of a struct to mimic a ‘payload’
and to allow that payload contain a pointer to another struct. Can someone tell me if this is possible or if what i’m trying to do is not feasible.

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

#define ptrsize sizeof(char*)

typedef struct s{
  int i;
  short j;
  long k;
}S;

S *salloc(int sz,int i,short j,long k){
  S *m=malloc(sizeof(S)+sz);
  m->i=i;m->j=j;m->k=k;
  return m;
}

char *goToData(S *m){
  char* dataloc=(char*)m+sizeof(S);
  return dataloc;
}

int main(int argc,char **argv){

  char a[]={"ABCDEFGHIJKLMNOPQRSTUVWXYZ"};
  S *mys=salloc(26,2,3,100);              // struct * to size 26+sizeof(S) & set struct vars
  char *mydp=goToData(mys);               // get the address of the payload 
  memcpy(mydp,a,sizeof(a));               // copy a into the payload

  S *mysc=salloc(ptrsize,1,2,3);          // allocate a container struct

  char *datapw=goToData(mysc);            // go to the first byte of the payload of mysc
  (*(S**)datapw)=mys;                     // want to point at mys -- is this possible?

  printf("%d\n",ptrsize);
  printf("addr mys              %x\n",(unsigned int)mys);
  printf("addr mysc             %x\n",(unsigned int)mysc);
  printf("addr mysc | *datapw   %x\n",(unsigned int)*datapw);  // from here would like to be indirectly reference mys

  return 0;

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T17:31:44+00:00Added an answer on May 28, 2026 at 5:31 pm

    The goToData() function seems to be sound. The memory allocation is sound, though you don’t record how big the space is that was allocated after the structure.

    There’s a buffer overflow here:

    char a[] = {"ABCDEFGHIJKLMNOPQRSTUVWXYZ"};
    S *mys = salloc(26,2,3,100);              // struct * to size 26+sizeof(S) & set struct vars
    char *mydp = goToData(mys);               // get the address of the payload 
    memcpy(mydp, a, sizeof(a));               // copy a into the payload
    

    You allocated 26 bytes but you’re copying 27 (sizeof(a) == 27 because sizeof() counts the NUL '\0' at the end). That is a recipe for disaster. Don’t use elegant variation in C; use consistency. Either use 26 in both places or sizeof(a), but not a mixture.


    If there’s a problem, it is with the line:

    (*(S**)datapw)=mys;                     // want to point at mys -- is this possible?
    

    I’m not even sure I understand what you’re trying to do here, but it doesn’t look good at all.

    Although datapw is aligned for use as an S *, you haven’t allocated enough space for that to be used. I’m not clear that you should be converting it to a S** before dereferencing.

    If you are trying to make the space after the structure pointed at by mysc contain a pointer to the structure pointed at by mys, then you had better have a really good reason for not including the pointer in the structure. A really, really good reason.

    However, that code is accurate, despite my strong misgivings. But it is extremely opaque.

    I think you should get the result you want with:

    *((S **)datapw) = mys;
    

    I hate to think what the strict-aliasing implications are, though you might be OK since there is an exemption of some sort for char *.

    So, revisit that statement and work out what you are trying to do – because what you’ve written doesn’t do it, whatever it was.

    printf("addr mysc | *datapw   %x\n",(unsigned int)*datapw);
    

    This has some issues. Since datapw is a char *, *datapw is going to be a character. Probably not what you had in mind. (unsigned int)*(S **) is what you need, I think.

    The rest is OK-ish, though on a 64-bit system, addresses are too big for %x. You’ll get away with on a 32-bit system, but you should use either %p with (void *) casts, or "%" PRIXPTR from <inttypes.h> with a (uintptr_t) cast.

    I ended up with:

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <inttypes.h>
    
    #define ptrsize sizeof(char*)
    
    typedef struct s
    {
        int   i;
        short j;
        long  k;
    } S;
    
    static S *salloc(int sz, int i, short j, long k)
    {
        S *m = malloc(sizeof(S) + sz);
        m->i = i;
        m->j = j;
        m->k = k;
        return m;
    }
    
    static char *goToData(S *m)
    {
        char *dataloc = (char*)m + sizeof(S);
        return dataloc;
    }
    
    int main(void)
    {
        char a[] = {"ABCDEFGHIJKLMNOPQRSTUVWXYZ"};
        S *mys = salloc(sizeof(a), 2, 3, 100);     // struct * to size 26+sizeof(S) & set struct vars
        char *mydp = goToData(mys);                // get the address of the payload 
        memcpy(mydp, a, sizeof(a));                // copy a into the payload
        S *mysc = salloc(ptrsize, 1, 2, 3);        // allocate a container struct
        char *datapw  = goToData(mysc);            // go to the first byte of the payload of mysc
        *((S **)datapw) = mys;                     // want to point at mys -- is this possible?
    
        printf("%zu\n", ptrsize);
        printf("addr mys              %" PRIXPTR "\n", (uintptr_t)mys);
        printf("addr mysc             %" PRIXPTR "\n", (uintptr_t)mysc);
        printf("addr mysc | *datapw   %" PRIXPTR "\n", (uintptr_t)*(S **)datapw);
    
        return 0;
    }
    

    (I compile with -Wmissing-prototypes; the static in front of the functions prevents the compiler warning me about these functions. Since you aren’t using the arguments to main(), I replaced argc and argv with void for the same reason – to avoid compiler warnings.)

    When I ran it under valgrind on a Mac (MacOS X 10.7.2, GCC 4.2.1, Valgrind 3.7.0), I got a clean run with the data output:

    8
    addr mys              100005120
    addr mysc             100005190
    addr mysc | *datapw   100005120
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I'm trying to create an if statement in PHP that prevents a single post
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I am doing a simple coin flipping experiment for class that involves flipping a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.