[/color][/b]
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define FMAX 6
typedef struct cell *CellPtr;
typedef struct cell {
int contents; /* contents of the cell */
CellPtr next; /* next cell in the list */
} Cell;
CellPtr insert (int input, CellPtr list);
void print_list(CellPtr list);
int main()
{
char function[FMAX];
CellPtr list=NULL;
int number;
while(1)
{
printf("< ");
scanf("%s",function);
if(strcmp(function,"insert")==0)
{
scanf("%d", &number);
list=insert(number,list);
print_list(list);
}
if(strcmp(function,"print")==0)
{
print_list(list);
}
}
return 0;
}
void print_list(CellPtr list)
{
if (list==NULL)
{
printf("The list is empty!\n");
}
else
{
while(list!=NULL)
{
printf ("%d ->",list->contents);
list=list->next;
}
printf("\n");
}
}
CellPtr insert(int input, CellPtr list)
{
CellPtr new_list, current_node;
if ((new_list=(CellPtr)malloc(sizeof(CellPtr)))== NULL)
{
printf(" Memory allocation failed !\n");
exit (1);
}
new_list->contents=input;
if(list==NULL)
{
new_list->next= list;
return new_list;
}
else
{
current_node=list;
while(current_node->next!=NULL)
{
current_node=current_node->next;
}
new_list->next=current_node->next;
current_node->next=new_list;
return list;
}
}
[b][color="#FF0000"]
problem with dynamic allocation
Page 1 of 16 Replies - 178 Views - Last Post: 19 January 2013 - 02:10 PM
#1
problem with dynamic allocation
Posted 19 January 2013 - 12:30 PM
im trying to insert values in the linked list... the first insert works but the the second fail and the program stops working... i guess the problem is with the allocation.. i couldnt solve it:( sorry the code is lil bit long
Replies To: problem with dynamic allocation
#2
Re: problem with dynamic allocation
Posted 19 January 2013 - 01:02 PM
This looks wrong. Taking the size of a pointer will give the same result every time (per machine)--probably 4 bytes. You probably want the size of the Cell struct rather than a pointer.
-- Edit --
It seems to work OK with the above replaced with this:
if ((new_list=(CellPtr)malloc(sizeof(CellPtr)))== NULL)
{
printf(" Memory allocation failed !\n");
exit (1);
}
-- Edit --
It seems to work OK with the above replaced with this:
new_list = (CellPtr)malloc(sizeof(Cell));
if ( !new_list )
{
printf(" Memory allocation failed !\n");
exit (1);
}
This post has been edited by NathanMullenax: 19 January 2013 - 01:07 PM
#3
Re: problem with dynamic allocation
Posted 19 January 2013 - 01:16 PM
you are right i didnt see that... but still the same problem !! the program fails when i insert the second value
#4
Re: problem with dynamic allocation
Posted 19 January 2013 - 01:30 PM
Maria23, on 19 January 2013 - 01:16 PM, said:
you are right i didnt see that... but still the same problem !! the program fails when i insert the second value
How is it failing? Are you seg-faulting? I tested it with the following inputs:
Test 1:
-------
insert 1
1->
insert 2
1->2->
insert 3
1->2->3->
Test 2:
-------
yes 'insert 3' | ./a.out
The first test worked as expected, and the second test ran until I ran out of memory (as expected). What are your test cases?
#5
Re: problem with dynamic allocation
Posted 19 January 2013 - 01:43 PM
i tried the same as u... it failed on the second number (2) !
i copied and edited the code
it doesnt say memory allocation faild
the program just fail (it comes a small window with green thing.. then it says ur program has stopped for some reason)
i copied and edited the code
it doesnt say memory allocation faild
the program just fail (it comes a small window with green thing.. then it says ur program has stopped for some reason)
#6
Re: problem with dynamic allocation
Posted 19 January 2013 - 02:04 PM
Ah. I just noticed another thing: you allocate six characters for the 'function' array, but insert has seven when you count the zero that terminates the string, so you're likely getting a buffer overrun when you call scanf. For some reason it wasn't crashing on my system. Does the error still happen when you change FMAX to, say, 10?
#7
Re: problem with dynamic allocation
Posted 19 January 2013 - 02:10 PM
wow all that because that "6" thing !! i forgot to count the \0
thnx alot it works fine now
thnx alot it works fine now
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote




|