Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,973 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,406 people online right now. Registration is fast and FREE... Join Now!




weird error "invalid next size"

 
Reply to this topicStart new topic

weird error "invalid next size"

liem_nguyenho
5 Jan, 2008 - 04:26 AM
Post #1

New D.I.C Head
*

Joined: 23 Aug, 2007
Posts: 14


My Contributions
I'm try this simple linked-list program, this is the code :

student.h:

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

#define MAX_ID 6
#define MAX_NAME 21
#define EQUAL 0

typedef struct date {
   int dd;
   int mm;
   int yy;
} Date;


typedef struct student{
   char studentID[MAX_ID];    /* A 5 character unique ID eg. 'S1234' */
   char lastName[MAX_NAME];   /* Stores Last Name. No spaces!        */
   char firstName[MAX_NAME];  /* Stores rest of the name, inc. spaces*/
   Date birthday;             /* store the birthday of the student */
} StudentType;

typedef struct studentlistnode {
   StudentType student;
   struct studentlistnode *next;
} StudentListNode;

typedef StudentListNode * StudentListNodePtr;

extern StudentListNodePtr NewStudent(char *studentID, char* lastName,
                                     char *firstName, Date birthday);
extern StudentListNodePtr AddStudent(StudentListNodePtr newStudent,
                                     StudentListNodePtr head);

extern StudentListNodePtr DelStudent(char* studentID, StudentListNodePtr head);



student.c:


CODE
#include "student.h"

/* function implementation goes here */
/**/
StudentListNodePtr NewStudent(char *studentID, char* lastName,
                              char *firstName, Date birthday) {

  StudentListNodePtr newN = malloc(sizeof(StudentType));
  
  strncpy(newN->student.studentID, studentID, MAX_ID);
  
  strncpy(newN->student.lastName, lastName, MAX_NAME);
  strncpy(newN->student.firstName, firstName, MAX_NAME);
  newN->student.birthday = birthday;
  
  newN->next = NULL;
  return newN;
}

/*add student*/
StudentListNodePtr AddStudent(StudentListNodePtr newStudent,
                              StudentListNodePtr head) {
  
  StudentListNodePtr currentStudent;
  currentStudent = head;
  
  if(head == NULL){
    /*newStudent->next = head;*/
    head = newStudent;
    }
  else {
    
    while(currentStudent->next!=NULL)
      currentStudent = currentStudent->next;
    
      currentStudent->next = newStudent;
  }
  return head;
}

/*delete student*/
StudentListNodePtr DelStudent(char* studentID, StudentListNodePtr head)
{
  StudentListNodePtr prevStudent, curStudent, temp;
  temp = NULL;
  curStudent = head;
  prevStudent = NULL;

  if(strcmp(head->student.studentID, studentID) == EQUAL) {
    temp = head;
    head = head->next;
    free(temp);                     /*============>error is right here*/
  }
  else {
    while(curStudent != NULL) {
      if(strcmp(curStudent->student.studentID, studentID) == EQUAL) {
    temp = curStudent;
        prevStudent->next = curStudent->next;
    free(temp);                /*==============>error is right here*/
      }
      else {
        prevStudent = curStudent;
        curStudent = curStudent->next;
      }
    }
  }
  return head;
}


when i try to code the driver class to test the function, the addStudent() function is ok, but when i try to delete and free the node, the error "invalid next size(fast)" is displayed. That doesn't happen when i just delete but not free the node.
Can anyone help me ?



*mod edit - please post code using the code tags, like this code.gif

This post has been edited by jjhaag: 5 Jan, 2008 - 04:37 AM
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Weird Error "invalid Next Size"
5 Jan, 2008 - 07:30 AM
Post #2

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,355



Thanked: 51 times
Dream Kudos: 25
My Contributions
Have not had time to compile and run your code, but check the following thread for a debugging technique:

http://forums.devshed.com/c-programming-42...ize-313655.html

User is online!Profile CardPM
+Quote Post

baavgai
RE: Weird Error "invalid Next Size"
5 Jan, 2008 - 08:40 AM
Post #3

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 2,282



Thanked: 136 times
Dream Kudos: 475
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua, Cheese

My Contributions
Try making StudentListNodePtr newN = malloc(sizeof(StudentType)); into StudentListNodePtr newN = malloc(sizeof(StudentListNode));. Your error is basically a memory crash.

Hope this helps.


User is online!Profile CardPM
+Quote Post

liem_nguyenho
RE: Weird Error "invalid Next Size"
6 Jan, 2008 - 08:57 PM
Post #4

New D.I.C Head
*

Joined: 23 Aug, 2007
Posts: 14


My Contributions
QUOTE(baavgai @ 5 Jan, 2008 - 09:40 AM) *

Try making StudentListNodePtr newN = malloc(sizeof(StudentType)); into StudentListNodePtr newN = malloc(sizeof(StudentListNode));. Your error is basically a memory crash.

Hope this helps.



Thanks, my problem is solved. I'm stuck with this for long time.
Forgive me for the late thanks.

User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/8/09 06:39PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month