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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T07:38:50+00:00 2026-06-14T07:38:50+00:00

I am writing a simple stack program in C that has a flexible array

  • 0

I am writing a simple stack program in C that has a flexible array of void*’s at the end. When more elements are needed to push into the array, I am using realloc() to make a new stack, free the old one and assign the new to the old one.(I don’t have a pop() function at the moment).

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

typedef struct Stack
{
    int top, length;
    void* start_ptr[];
} Stack;

Stack* init_stack(int n_items)
{
    Stack* stack;
    stack = malloc(sizeof(int) * 3 + sizeof(void*) * n_items);

    stack->top = -1;
    stack->length = n_items;

    return stack;
}

Stack* increase_stacksize(Stack* stack, int n_itemsToAdd)
{
    Stack* newstack;    
    newstack = realloc(stack, sizeof(*stack) + sizeof(void*) * (n_itemsToAdd + stack->length));

    if(newstack != NULL)
         //printf("\nDebug print - array reallocated\n");

    free(stack);
    stack = newstack;

    stack->length += n_itemsToAdd;

    return stack;
}

Stack* push(Stack* stack, void* item)
{
    if(stack->top + 1 == stack->length){

        stack = increase_stacksize(stack, 10);
    }

    int pos = stack->top + 1;

    stack->start_ptr[pos] = item;
    ++(stack->top);

    return stack;
}

void printstack(Stack* stack)
{
     printf("Number of items in the stack = %d\n", stack->top + 1);
     printf("Capacity of the stack = %d\n", stack->length);

     printf("Elements in the stack are: \n");

    int i;

    for(i = 0; i <= stack->top; i++){
        int* item_ptr;
        void* address;

        address = stack->start_ptr[i];

        item_ptr = (int*)address;

        printf("Position = %d, Item = %d \n", i, *item_ptr);
    }
}

int main(void)
{    
    Stack* stack;
    stack = init_stack(5);

    int a[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    int i;

    for(i = 0; i < 10; i++)
    {
        stack = push(stack, (void*)(a+i));
    }

    printstack(stack);

    free(stack);

    return 1;
}

Problem is when I free() the stack in main() after I am all done a message gets printed in stderr :

*** glibc detected *** ./prog: double free or corruption (fasttop): 0x096ee008 ***
======= Backtrace: =========
/lib/libc.so.6[0xb7721fd4]
/lib/libc.so.6(cfree+0x9c)[0xb772387c]
./prog[0x8048731]
./prog[0x8048461] 
 ======= Memory map: ========

The output upto printstack() is fine though. Even if I remove the free(stack) line, I get a runtime error in ideone.com, but there’s no stderr message. I am guessing it might be something due to the zero size array. What could be the issue here?

  • 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-14T07:38:51+00:00Added an answer on June 14, 2026 at 7:38 am

    realloc frees your old block if necessary so you should remove the free(stack) line in increase_stacksize

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

Sidebar

Related Questions

I am writing a simple C program that I'm supposed to attack with a
I'm writing a simple movie database which has a three tier stack of service
I'm writing a simple test of embedding Lua into a C program. I have
I am writing a simple program to filter out stock quotes from a given
I am writing simple parallel program in C++ using OpenMP. I am working on
I'm writing simple program to communicate between smart devices and I receive 11001 when
I'm looking into writing simple graphics code in Android and I've noticed some synchronized()
I am writing simple site that requires users and profiles to be handled. The
I am writing a program for class that is asking us to create a
I am writing a program that will query an MS access database, return the

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.