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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:00:35+00:00 2026-05-27T09:00:35+00:00

I am having a problem with the opendir function in C. Here is the

  • 0

I am having a problem with the opendir function in C. Here is the code:

Declaration of rvm:

rvm_t func()
{
   rvmBlock=(rvm_t)malloc(sizeof(rvm_t));
   return rvmBlock;
}

rvm_t rvm;
rvm=func();

printf("rvm->backingStore=%s\n", rvm->backingStore); 
if( (dir = opendir(rvm->backingStore)) !=NULL )
{
   printf("rvm->backingStore inside if=%s\n", rvm->backingStore);
}

The output i am getting for this is:

rvm->backingStore=rvm_segments/
rvm->backingStore inside if=rvm_segments!? 

"!?" are some garbage characters that are appearing for some reason.

Can someone explain what is going wrong.

Here is the rvm structure:

struct rvm_info
{

   char backingStore[20];
   struct memSeg * memSegs[20];
   long int storage_size;
   int memSeg_count;
   FILE * log_fd;
};

typedef struct rvm_info* rvm_t;
  • 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-27T09:00:36+00:00Added an answer on May 27, 2026 at 9:00 am

    This is your problem:

    rvm_t func()
    {
       rvmBlock=(rvm_t)malloc(sizeof(rvm_t));
       return rvmBlock;
    }
    

    rvm_t is defined as a pointer to a struct rvm_info, therefore you’re passing an incorrect size to malloc. sizeof(rvm_t) equates to the size of a pointer (usually 4 or 8 bytes) and NOT the size of a struct rvm_info (which is well over 4 or 8 bytes). You want the size to be that of struct rvm_info, NOT a pointer. Change that call to:

    rvmBlock = malloc( sizeof(*rvmBlock) );
    

    Which just means:

    rvmBlock = malloc( sizeof(struct rvm_info) );
    

    Otherwise, you’re causing undefined behaviour since you haven’t allocated enough memory for a whole struct rvm_info. Therefore you’ll be storing that string in a part of memory that hasn’t been allocated for rvm, and any other part of the program could allocate that memory.

    It just so happens that a call to opendir cause some memory on the heap to be modified, it doesn’t directly/on purpose modify the string passed to it, especially since the argument is of type const char*.

    EDIT: As Keith mentioned in the comments, when using C (not C++) it can be considered bad to cast the result of malloc. This question has discussion on the topic.

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

Sidebar

Related Questions

Im having problem's adding map markers to gmaps using jquery. Here is my code
I'm having problem restricting a query in mdx, using except function at where clause.
Having problem with this bit of code qith jQuery. it should pick the values
I am having problem with the title of a header in my jquery code.
I'm having problem with SHGetFileInfoW function I'm using. It's a quite slow and first
i having problem in converting this C# code into VB.net. The loadLecturer seem to
I'm having problem getting all the values in datalist here is the problem: I
I´m having problem with the anova function in the rms package: require(rms) getHdata(prostate) mod1<-cph(Surv(dtime,status!=Alive)~stage+rx+age+wt,data=prostate,x=T,y=T)
Iam having problem creating postgresql function in plperl CREATE OR REPLACE FUNCTION zm_json (TEXT,
am having problem in Saving some data in my Java code. I have like

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.