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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:03:34+00:00 2026-05-27T19:03:34+00:00

The code is: ( I’ve marked the faulty line with a comment ) #include

  • 0

The code is: (I’ve marked the faulty line with a comment)

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

typedef struct stack_node_type{
    int *nr;
    char *string;
struct stack_node_type *next;
} SNODE;

SNODE * pushStack(SNODE **stack, int *nr, char *string){
SNODE *snode=NULL;
if(nr!=NULL){
    snode = (SNODE *) malloc(sizeof(SNODE));
    aux=snode->nr;
    printf("%d\n", *nr);
    memcpy(snode->nr, nr, sizeof(int)); //THIS IS THE FAULTY LINE
    if(*(&stack)!=NULL){
        snode->next=&(**stack);
    }
    else{
        snode->next=NULL;
    }
    if(string!=NULL){
        snode->string=&(*string);
    }
}
else{
    if(string!=NULL){
        snode = (SNODE *) malloc(sizeof(SNODE));
        if(*(&stack)!=NULL){
            snode->next=&(**stack);
        }
        else{
            snode->next=NULL;
        }
        snode->string=&(*string);
    }
}
if(snode!=NULL){
    return &(*snode);
}
else{
    return &(**stack);
}
}

SNODE * popStack(SNODE **stack, SNODE *pop){
SNODE *snode=NULL;
snode=&(**stack);
if(snode!=NULL){
    if(snode->nr!=NULL){
        pop->nr=(int *) malloc(sizeof(int));
        * (pop->nr) = * (snode->nr);
    }
    if(snode->string!=NULL){
        int strdim = strlen(snode->string);
        pop->string=(char *) malloc(strdim*sizeof(char));
        strcpy(pop->string, snode->string);
    }
    SNODE *to_del=snode;
    snode=snode->next;
    free(to_del);
}
return &(*snode);
}

int main()
{
SNODE *stack=NULL;
SNODE pop;
int nr;
nr=123;
stack=pushStack(&stack, &nr, "banane");
nr=819;
stack=pushStack(&stack, &nr, "portocale");
while(stack!=NULL){
    stack=popStack(&stack, &pop);
    printf("POP: %d, \"%s\"\n", *(pop.nr), pop.string);
}
    return 0;
}

Restating the faulty line:

memcpy(snode->nr, nr, sizeof(int)); //THIS IS THE FAULTY LINE

Memcpy should crack when unavailable memory is being accesed or the source and destination memory blocks overlap, so as far as I’m concerned, none of these problems seem to be valid.
Why does it crack?

  • 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-27T19:03:35+00:00Added an answer on May 27, 2026 at 7:03 pm

    You allocated memory for the structure but didn’t allocate for the members themselves.

    Try:

    snode = (SNODE *) malloc(sizeof(SNODE));
    snode->nr = malloc(sizeof(int));
    

    But if I were you I would change the structure:

    struct stack_node_type{
        int nr;
        char *string;
    };
    
    memcpy(&snode->nr, nr, sizeof(nr));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

code: #include<iostream> using namespace std; template<class T, int N> class point { T coordinate[N];
Code: #include <iostream> using namespace std; #define ADD(x,y) ((x)+(y)) int main( int argc, char**
Code #include <OOLua/oolua.h> class foo { public: int bar(); }; OOLUA_CLASS_NO_BASES(foo)//class has no bases
Code: private final int A0 = 0; private final int A1 = 1; .....
Code: <%=#{time_ago_in_words(comment.created_at)} ago %> What i'd like is for it not to have ABOUT
code below. i'm tryind to obtain string answers like a1, c4 this is what
Code goes first: class A { public: ... int *foo() const { return _px;
Code: static int counter = 0; int add(int x) { counter++; return ++x; }
code: char *m[10]; char * s; int lcounter=0; int sd=0; char mem_buf [ 500
CODE:1 class Ajay { private void display() { System.out.println(Ajay); } public static void main(String

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.