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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T02:43:34+00:00 2026-06-16T02:43:34+00:00

OS:Windows Vista(x86) Compiler: Code::Blocks I am currently writing a program that opens a specified

  • 0
  • OS:Windows Vista(x86)
  • Compiler: Code::Blocks

I am currently writing a program that opens a specified directory and reads the contents of it. Rather than using printf() to display the file names immediately after they are found. I hold them in memory and display them later. I use the following if statement to trigger memory reallocation. I have also included the declarations of relevant variables.

//Represents what the new index will be after the current file name is added
//to 'stack.ptr'
#define NEW_INDEX (stack.index+(strlen(ptr_dirent->d_name)))

//Contains the pointer that points to the directory's contents 'stack.ptr',
//the size of 'stack.ptr' which is 'stack.size', and the current index
//'stack.index'
struct stack
{
  int index;
  char *ptr;
  int size;
};struct stack stack;

//Sets the index to 0 and allocates 256 bytes of memory for 'stack.ptr'
stack.index = 0; stack.size = 256;
stack.ptr = malloc(sizeof(char)*stack.size);

if(NEW_INDEX > stack.size)
{
  char *temp; stack.size *= 2;
  temp = realloc(stack.ptr, sizeof(char)*stack.size);
  if (temp == NULL)
  {
    printf("ERROR: %i Bytes of memory could not be allocated.\n", stack.size);
    free(stack.ptr); closedir(dirp); return '\000';
  }
  else {stack.ptr = temp;}
}

The program works perfectly until I set the initial value of ‘stack.size'(which is the array size) to 2 rather than 256 (so that the program HAS to reallocate memory). My program crashed because realloc() returned NULL but I had plenty of memory available. I know that realloc() did work a couple of times because
‘stack.size’ was 16 when it crashed (‘stack.size’ is doubled every time memory is reallocated). I tried setting ‘stack.size’ to a couple of different values and I found that setting ‘stack.size’ to 1 or 2 causes a crash and it always happens when ‘stack.size’ reaches 16. Can anyone explain this to me? I am worried that even if I set ‘stack.size’ to 256 my program may crash if a directory is large enough to trigger memory reallocation. Also in an unrelated note, I read that openddir(“.”); will open the current directory and I have found that it does but for some reason not all of the files in the current directory are in ‘stack.ptr’ and a . and .. are displayed when I output the contents of ‘stack.ptr’ to stdout.

  • 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-16T02:43:35+00:00Added an answer on June 16, 2026 at 2:43 am

    You didn’t show us the line that contains the bug. It probably looks like:

    strcpy(stack.ptr+stack.index, ptr_dirent->d_Name);
    

    The problem here is that strcpy() copies strlen() + 1 bytes. You are writing the terminating NUL char beyond the end of your allocated array.

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

Sidebar

Related Questions

Possible Duplicate: C# - How to get Program Files (x86) on Windows Vista 64
On Windows Vista, with UAC enabled, everytime a program that requires administrative priviledges starts,
Possible Duplicate: C# - How to get Program Files (x86) on Windows Vista 64
Windows provides only GetTickCount up to Windows Vista and starting from that OS also
I am using Windows Vista x86 + VSTS 2008. When creating new Windows Media
If I am using Windows Vista x86 Enterprise as development environment, and I have
I am using windbg 6.12.0002.633 X86 on Windows Vista to analyze memory dumps for
I have some code that effectively does this : File file = new File(C:\\Program
Until Windows Vista, ATI and nVidia supported a feature called horizontal span, which combined
since Windows Vista there is an new Win32-API call CreateSymbolicLink to create a symbolic

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.