473,327 Members | 1,930 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

Stack implementation question

Hi CLCers,
I am getting Segmentation fault, core dump error from the
following program. The program is a stack implementation
with push and pop functions(at present only push is
implemented). I dont know where i am erring. Here is the
program:

#include<stdio.h>
int main()
{
int * stackptr;

void push(int **pushint, int i);

/*Memeory allocation for stack*/
stackptr=(int *)malloc(100 * sizeof(int));

push(&stackptr, 10);

printf("The stack is %d\n",*stackptr);
return 0;
}

void push(int **stackptr, int i)
{
*stackptr=i;
}
Thanks in advance.
--Shan

Nov 14 '05 #1
2 2037
an*******@coolgroups.com wrote:
I am getting Segmentation fault, core dump error from the
following program. The program is a stack implementation
with push and pop functions(at present only push is
implemented). I dont know where i am erring. Here is the
program: #include<stdio.h>
int main()
{
int * stackptr; void push(int **pushint, int i); /*Memeory allocation for stack*/
stackptr=(int *)malloc(100 * sizeof(int));
You're missing an include of <stdlib.h> and you're keeping the
compiler from complaing about that by casting the return value
of malloc() (which usually doesn't make too much sense anyway).
push(&stackptr, 10); printf("The stack is %d\n",*stackptr);
It's probably crashing here. When you look at what value
'stackptr' has you will find that it's 10 and not the
address you got from malloc() anymore.
return 0;
} void push(int **stackptr, int i)
{
*stackptr=i;


Here you overwrite the address the 'stackptr' pointer from main()
is pointing to (i.e. the address of the memory you allocated in
main()) instead of writing the integer into the memory at that
address. Your compiler should warn you about that (some warning
about a type mismatch in the assignment) Try again with

**stackptr=i;

The question of course is why you pass the address of the pointer
to the function if you don't want to change that address...

Regards, Jens
--
\ Jens Thoms Toerring ___ Je***********@physik.fu-berlin.de
\__________________________ http://www.toerring.de
Nov 14 '05 #2
<an*******@coolgroups.com> wrote in message
news:b6******************************@news.scbiz.c om...
Hi CLCers,
I am getting Segmentation fault, core dump error from the
following program. The program is a stack implementation
with push and pop functions(at present only push is
implemented). I dont know where i am erring. Here is the
program:

#include<stdio.h>
int main()
{
int * stackptr;

void push(int **pushint, int i);

/*Memeory allocation for stack*/
stackptr=(int *)malloc(100 * sizeof(int));

push(&stackptr, 10);

printf("The stack is %d\n",*stackptr);
return 0;
}

void push(int **stackptr, int i)
{
*stackptr=i;
}
Thanks in advance.
--Shan


The first thing to do is to pay attention to the compiler warnings that this
code generates and fix them.
Nov 14 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Kevin Grigorenko | last post by:
Hello, I couldn't find an obvious answer to this in the FAQ. My basic question, is: Is there any difference in allocating on the heap versus the stack? If heap or stack implementation is not...
17
by: Jonas Rundberg | last post by:
Hi I just started with c++ and I'm a little bit confused where stuff go... Assume we have a class: class test { private: int arr; };
4
by: Chris Mabee | last post by:
Hello all, and Merry Christmas, I'm having a problem understanding an example of an array based implementation of a stack in a textbook of mine. The code in question is written below. The syntax...
20
by: Sushil | last post by:
Hi gurus I was reading FAQ "alloca cannot be written portably, and is difficult to implement on machines without a conventional stack." I understand that the standard does not mandate...
4
by: anonymous | last post by:
Thanks your reply. The article I read is from www.hakin9.org/en/attachments/stackoverflow_en.pdf. And you're right. I don't know it very clearly. And that's why I want to understand it; for it's...
18
by: junky_fellow | last post by:
Hi all, Is there any way by which we mat determine the direction of stack growth (from higher address to lower address Or from lower address to higher address) ? I know this question is...
24
by: John | last post by:
I know this is a very fundamental question. I am still quite confused if the program call stack stack should always grows upwards from the bottom, or the opposite, or doesn't matter?? That means...
148
by: onkar | last post by:
Given the following code & variable i . int main(int argc,char **argv){ int i; printf("%d\n",i); return 0; } here i is allocated from bss or stack ?
8
by: t | last post by:
The stack container adaptor uses deque as its default underlying sequential container. I don't understand why since we only add elements and remove elements from one side of a stack. Why isn't...
87
by: CJ | last post by:
Hello: We know that C programs are often vulnerable to buffer overflows which overwrite the stack. But my question is: Why does C insist on storing local variables on the stack in the first...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.