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 8590917
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T23:24:19+00:00 2026-06-11T23:24:19+00:00

I have a structure that contains a pointer to a byte array. To set

  • 0

I have a structure that contains a pointer to a byte array.

To set the pointer I’ve tried the following two ways:

1 Use malloc then memcpy byte array data (commented out in code below).

2 Simply copy pointer.

#include "stdlib.h"
#include "string.h"
#include "stdio.h"

typedef struct _element
{
    unsigned char *pValue;
    int nLength;
} Element;

Element* ElementCreate(void)
{
    Element *pElement = (Element*)malloc(sizeof(*pElement));

    pElement->pValue = NULL;
    pElement->nLength = 0;

    return pElement;
}

void ElementDestroy(Element **ppElement)
{
    Element *pElement = NULL;

    if (!ppElement)
        return;

    pElement = *ppElement;

    //free(pElement->pValue);
    //pElement->pValue = NULL;

    free(pElement);
    *ppElement = NULL;
}

void ElementSetValue(Element *pElement, unsigned char *pValue, int nLength)
{
    //pElement->pValue = (unsigned char*)malloc(nLength * sizeof(*(pElement->pValue)));
    //if (!(pElement->pValue))
    //    return;

    //memcpy(pElement->pValue, pValue, nLength);

    pElement->pValue = pValue;
    pElement->nLength = nLength;
}

void ElementWriteValue(const Element *pElement)
{
    int nIndex = 0;
    for (; nIndex < pElement->nLength; nIndex++)
        printf("%02X ", pElement->pValue[nIndex]);
}

int main(void)
{
    //unsigned char myValue[] = { 0x01, 0x02, 0x03 };
    //int nLength = sizeof(myValue) / sizeof(myValue[0]);

    Element *pElement = ElementCreate();

    {
        unsigned char myValue[] = { 0x01, 0x02, 0x03 };
        int nLength = sizeof(myValue) / sizeof(myValue[0]);

        ElementSetValue(pElement, myValue, nLength);
    }

    // How come this writes out correct value?
    ElementWriteValue(pElement);

    ElementDestroy(&pElement);

    return 0;
}

(Error checks are omitted for brevity)

Which way is correct?

I’d expect 2 to fail because myValue will be destroyed after the “}” so

printf("%02X ", pElement->pValue[nIndex]);

would write out rubbish data but it seems to work OK. Why?

  • 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-06-11T23:24:21+00:00Added an answer on June 11, 2026 at 11:24 pm

    It is undefined behaviour, of which a subset is to “work correctly”.

    The array myValue is out of scope at the next }. At this point the memory location in which myValue was stored is available to be resused, but it may not be leaving it unchanged and hence the code appears to work.

    The correct approach is to malloc(), memcpy() and free().

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a structure in my program that contains a particular array. I want
I have an xml file that contains a structure.. In this structure, I have
I have an XML file format that contains a structure of questions: <question id=q101>
I have a table that contains articles posted by users. The structure of the
I have a Delphi DLL that contains the following types: type TStepModeType = (smSingle,
I have a book structure that contains a book title, url, and student name.
seeking some advice here. I have a structure which contains a pointer to another
I have an array that can contain any number of elements. Each element contains
I have a unsigned char pointer which contains a structure.Now I want to do
I have a Database that contains data about articles , structures and manufacturers .

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.