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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T18:33:00+00:00 2026-06-17T18:33:00+00:00

I’m having problems writing a structure to shared memory because when I do, the

  • 0

I’m having problems writing a structure to shared memory because when I do, the code only writes the first part of the structure.

Here’s my data structure:

struct shmData{
    char number[4];
    char description[1020];
};

Here’s my code:

//Structure Pointer - placed on SHM
struct shmstruct *ptr;
char description_to_write[1024] = {0};
struct shmData *data_ptr;
data_ptr = (struct shmData*) description_to_write;

//Shared Memory init
create_shm(&ptr);
printf("Started\n");

//Define Offsets
//init_shm(ptr);

//#########################

//Check if number exist
    if (!(strcmp("NO SUCH CODE\0", get_description(ptr, 6)))) {
        //-> If not, get next offset (nput) where to write new data
//      sem_wait(&ptr->mutex);
//      long offset = ptr->msgoff[ptr->nput];
//      printf("OFFSET: %li\n", offset);
//      if (++(ptr->nput) > NMESG)
//          ptr->nput = 0; /* circular buffer */
//      sem_post(&ptr->mutex);
//      printf("Buffer: %s", argv[2]);

    snprintf(data_ptr->number, 4, "%s", "6");
    snprintf(data_ptr->description, 1020, "%s\n", argv[2]);

    printf("DATA: %s\n", data_ptr->number);
    printf("DATA: %s\n", data_ptr->description);

    printf("description_to_write: %i\n", description_to_write);

    //strcpy(&ptr->msgdata[offset], );
    //sem_post(&ptr->nstored);
    //printf("Wrote...\n\n");
} else {
    //-> Else get offset of this number and put data to it
}

How should I write this? In the code above, I tried simply to display the description array but I only get the number back.

I need it to be written to shared memory as follows:

Number(space)(space)(space)Description

Shared Memory Dump looks like:

00003b0: 3120 2020 496e 7075 7420 4572 726f 720a  1   Input Error.
00003c0: 0000 0000 0000 0000 0000 0000 0000 0000  ................
*
00007b0: 3220 2020 4f75 7470 7574 2045 7272 6f72  2   Output Error
00007c0: 0a00 0000 0000 0000 0000 0000 0000 0000  ................
00007d0: 0000 0000 0000 0000 0000 0000 0000 0000  ................

From debugger:

description_to_write[0] char    54 '6'  
description_to_write[1] char    0 '\000'    
description_to_write[2] char    0 '\000'    
description_to_write[3] char    0 '\000'
description_to_write[4] char    83 'S'  
description_to_write[5] char    111 'o' 
description_to_write[6] char    109 'm' 
description_to_write[7] char    101 'e' 
description_to_write[8] char    84 'T'  
description_to_write[9] char    101 'e' 
description_to_write[10]char    120 'x' 
description_to_write[11]char    116 't' 
description_to_write[12]char    10 '\n' 

Ugly solution:

int i = 0;
        while(i < 4){
            if(data_ptr->number[i] == '\000'){
                data_ptr->number[i] = 32;
            }
            i++;
        }
  • 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-17T18:33:01+00:00Added an answer on June 17, 2026 at 6:33 pm

    The 4 passsed to snprintf specifies the MAXIMUM number of chars to write. Not the amount to write. snprtinf retursn the number of chars it did write. So you are only printing 1 char, which leaves 3 nulls after it.

    Just do:

    snprintf(data->number,4,"%s    ","6");
    

    That way it will try and print the string followed by 4 spaces, and print a max of 4 charcacters.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
We're building an app, our first using Rails 3, and we're having to build
I have this code to decode numeric html entities to the UTF8 equivalent character.
I am using the SimpleRSS gem to parse a WordPress RSS feed. The only
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I want to construct a data frame in an Rcpp function, but when I

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.