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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T14:08:52+00:00 2026-05-25T14:08:52+00:00

I have the following code which you can try using c99 filename.c; ./a.out #include

  • 0

I have the following code which you can try using c99 filename.c; ./a.out

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

typedef unsigned long long int se_t;  // stack element type

se_t stack_size = 0;
se_t *bottom_of_stack = NULL;
#define top_of_stack (bottom_of_stack + stack_size * sizeof(se_t))

#define stack_infix(op) stack_push(stack_pop() #op stack_pop())
#define do_times(x) for(int _i=0; _i<x; _i++)

void stack_push(se_t v) {
    bottom_of_stack = realloc(bottom_of_stack,
                              ++stack_size * sizeof(se_t));
    *top_of_stack = v;
}

void stack_print() {
    printf("stack(%d): \n", (int)stack_size);
    for(se_t *i = bottom_of_stack;
             i <= top_of_stack;
             i += sizeof(se_t)) {
        printf("%p: %d \n", (void*)i, (int)*i);
    }
}

int main() {
    int i = 2;
    do_times(3) {
        stack_push(i*=i);
        stack_print();
    }
}

I reallocate stack every time I push something to it. Here is the output (with my comments):

stack(1): 
0x105200820: 0  // realloc successfully allocated some memory for the first time 
0x105200860: 4 
stack(2): 
0x105200820: 0  // extended the memory range without moving it somewhere else
0x105200860: 4 
0x1052008a0: 16 
stack(3): 
0x105200830: 0  // reallocated the memory to some other region (see the address) 
0x105200870: 0  // and failed for some reason to copy the old data!
0x1052008b0: 0  // why?!
0x1052008f0: 256 
  • 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-25T14:08:53+00:00Added an answer on May 25, 2026 at 2:08 pm

    Use this:

    #define top_of_stack (bottom_of_stack + (stack_size - 1))
    

    As it is, you’re storing data past the end of the allocated space.

    Oh, and change this line too:

    i += sizeof(se_t)) {
    

    Should be:

    i++) {
    

    Because of what pmg said about pointer arithmetic.

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

Sidebar

Related Questions

I'm trying to use opengl in C#. I have following code which fails with
I have following Code Block Which I tried to optimize in the Optimized section
I have the following code which works just fine when the method is POST,
I have the following code which works fine. However, I only want to return
Okay so I have the following Code which appends a string to another in
I have the following JQuery code which does similar functionality like Stackoverflow where the
I have the following piece of code which replaces template markers such as %POST_TITLE%
I have the following JavaScript code: Link In which the function makewindows does not
I have the following code, which will not work. The javascript gives no errors
I have the following code, which splits up a Vector into a string vector

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.