Stack implementation question

A

anonymous

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
 
J

Jens.Toerring

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
 
J

Jarmo

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top