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

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




Dynamic Memory problem ?

2 Pages V  1 2 >  
Reply to this topicStart new topic

Dynamic Memory problem ?

#include<wmx010>
2 Feb, 2008 - 07:54 PM
Post #1

D.I.C Head
**

Joined: 19 Jan, 2008
Posts: 75



Thanked: 1 times
My Contributions
Program crashes in runtime. Have no idea why.
Here's the code:

Employee.h
CODE

#ifndef EMPLOYEE_H
#define EMPLOYEE_H

class Employee
{
public:
    Employee(const char*, const char*);
    ~Employee();

    const char* GetFirstName() const;
    const char* GetLastName() const;

    static int GetCount(); // static member function, returns # of objects initialized

private:
    char*      _firstName;
    char*      _lastName;
    static int _count; //static data member, number of objects instantiated
};

#endif



Employee.cpp
CODE

#include "Employee.h"
#include <iostream>
#include <cstring>
using namespace std;

//----------------------------------------------
// defines and initializes static data member
//----------------------------------------------
int Employee::_count = 0;

//----------------------------------------------
// define static member function that returns
// number of Employee objects instantiated
//----------------------------------------------
int Employee::GetCount()
{
    return _count;
}

//----------------------------------------------
// constructor dynamically allocates space for
// first and last name and uses strcpy_s to
// copy first and last names into the object
//----------------------------------------------
Employee::Employee(const char* first, const char* last)
{
    _firstName = new char[strlen(first) + 1];
    strcpy_s(_firstName, sizeof(_firstName), first);

    _lastName = new char[strlen(last) + 1];
    strcpy_s(_lastName, sizeof(_lastName),  last);

    // increment static count of employees
    ++_count;

    cout << "Employee constructor for " << _firstName << ' ' << _lastName << " called.\n\n";
}

//-----------------------------------------------------
// Destructor deallocates dynamically allocated memory
//-----------------------------------------------------
Employee::~Employee()
{
    cout << "~Employee() called for " << _firstName << ' ' << _lastName << "\n\n";

    delete [] _firstName;
    delete [] _lastName;

    // decrement static count of employees
    --_count;
}

//-----------------------------------------------------
// return first name of employee
//-----------------------------------------------------
const char* Employee::GetFirstName() const
{
    //---------------------------------------------------------
    // const before return type prevents client from modifying
    // private data; client should copy returned sring before
    // destrctor deletes storage to prevent undefined pointer
    //---------------------------------------------------------
    return _firstName;
}

const char* Employee::GetLastName() const
{
    //---------------------------------------------------------
    // const before return type prevents client from modifying
    // private data; client should copy returned sring before
    // destrctor deletes storage to prevent undefined pointer
    //---------------------------------------------------------
    return _lastName;
}



main.cpp
CODE

#include <iostream>
#include <conio.h>
using namespace std;

#include "Employee.h"

int main ()
{
    cout << "Number of employees before initialization is "
         << Employee::GetCount() << endl; // use class name

    Employee* emp1Ptr = new Employee("Susan", "Baker");
    Employee* emp2Ptr = new Employee("Robert", "Jones");
    cout << "Number of employees after instantiation is " << emp1Ptr->GetCount();

    cout << "\n\nEmployee 1: "
         << emp1Ptr->GetFirstName() << " "
         << emp1Ptr->GetLastName()
         << "\nEmployee 2: "
         << emp2Ptr->GetFirstName() << " "
         << emp2Ptr->GetLastName() << "\n\n";

    delete emp1Ptr; // recapture memory
    emp1Ptr = 0;    // disconnect pointer from free-store space
    delete emp2Ptr; // recapture memory
    emp2Ptr = 0;    // disconnect pointer from free-store space
    cout << "number of employees after deletion is: " << Employee::GetCount() << endl;

    _getch();
    return EXIT_SUCCESS;
}



User is offlineProfile CardPM
+Quote Post

KYA
RE: Dynamic Memory Problem ?
2 Feb, 2008 - 08:06 PM
Post #2

#include <nerd.h>
Group Icon

Joined: 14 Sep, 2007
Posts: 5,910



Thanked: 159 times
Dream Kudos: 1375
My Contributions
Have you tried setting the pointers to null before deleting them?

--KYA
User is online!Profile CardPM
+Quote Post

#include<wmx010>
RE: Dynamic Memory Problem ?
2 Feb, 2008 - 08:17 PM
Post #3

D.I.C Head
**

Joined: 19 Jan, 2008
Posts: 75



Thanked: 1 times
My Contributions
I've tried but it didn't work...
User is offlineProfile CardPM
+Quote Post

KYA
RE: Dynamic Memory Problem ?
2 Feb, 2008 - 08:24 PM
Post #4

#include <nerd.h>
Group Icon

Joined: 14 Sep, 2007
Posts: 5,910



Thanked: 159 times
Dream Kudos: 1375
My Contributions
QUOTE(#include<wmx010> @ 2 Feb, 2008 - 10:17 PM) *

I've tried but it didn't work...



When I ran it it crashed just after outputting the line "Number of employees before intialization is 0".

Is that where it happens for you too?

--KYA
User is online!Profile CardPM
+Quote Post

#include<wmx010>
RE: Dynamic Memory Problem ?
2 Feb, 2008 - 08:37 PM
Post #5

D.I.C Head
**

Joined: 19 Jan, 2008
Posts: 75



Thanked: 1 times
My Contributions
Yep... same spot ...
User is offlineProfile CardPM
+Quote Post

KYA
RE: Dynamic Memory Problem ?
2 Feb, 2008 - 08:40 PM
Post #6

#include <nerd.h>
Group Icon

Joined: 14 Sep, 2007
Posts: 5,910



Thanked: 159 times
Dream Kudos: 1375
My Contributions
QUOTE(#include<wmx010> @ 2 Feb, 2008 - 10:37 PM) *

Yep... same spot ...


I think I've narrowed it down to the strcpy call here:

CODE

Employee::Employee(const char* first, const char* last)
{
    _firstName = new char[strlen(last) + 1];
    strcpy_s(_firstName, sizeof(_firstName), first); // HERE

    _lastName = new char[strlen(last) + 1];
    strcpy_s(_lastName, sizeof(_lastName),  last);  //HERE

    // increment static count of employees
    ++_count;

    cout << "Employee constructor for " << _firstName << ' ' << _lastName << " called.\n\n";
}


Although I cannot say why it is a problem. sad.gif I'll keep on looking.

edit: The strcpy_s function only creates a shallow copy so the memory address of _lastName and last become the same rather then a new string created. That could be the problem--a memory leak. Also the length can override adjacent memory addresses but you seem to have taken care of that by clipping down _lastName to the size of last before calling strcpy...

--KYA

This post has been edited by KYA: 2 Feb, 2008 - 08:47 PM
User is online!Profile CardPM
+Quote Post

#include<wmx010>
RE: Dynamic Memory Problem ?
2 Feb, 2008 - 08:52 PM
Post #7

D.I.C Head
**

Joined: 19 Jan, 2008
Posts: 75



Thanked: 1 times
My Contributions
I've used strcpy(_firstName, first); and it worked fine.
But visual studio 2008 generates a warning saying strcpy has been deprecated & i should use
strcpy_s which takes 3 arguments instead.

Should i avoid the warnings ?

This post has been edited by #include<wmx010>: 2 Feb, 2008 - 08:53 PM
User is offlineProfile CardPM
+Quote Post

KYA
RE: Dynamic Memory Problem ?
2 Feb, 2008 - 08:57 PM
Post #8

#include <nerd.h>
Group Icon

Joined: 14 Sep, 2007
Posts: 5,910



Thanked: 159 times
Dream Kudos: 1375
My Contributions
QUOTE(#include<wmx010> @ 2 Feb, 2008 - 10:52 PM) *

I've used strcpy(_firstName, first); and it worked fine.
But visual studio 2008 generates a warning saying strcpy has been deprecated & i should use
strcpy_s which takes 3 arguments instead.

Should i avoid the warnings ?



If this progam is needed immediately maybe, but strcpy_s is safer, it keeps the buffer from overflowing. I'm racking my brain-- I can't think of a reason why it would crash during run time. I think it has something to do with the sizes... I'll give it some more thought at work.

--KYA
User is online!Profile CardPM
+Quote Post

WXY
RE: Dynamic Memory Problem ?
2 Feb, 2008 - 11:32 PM
Post #9

D.I.C Head
Group Icon

Joined: 2 Jan, 2008
Posts: 85


Dream Kudos: 50
My Contributions
CODE
_firstName = new char[strle....
.... sizeof(_firstName)

There's your problem.

sizeof cannot be used to determine size of memory buffers allocated dynamically. If you must know the size of dynamically allocated buffers use _msize
User is offlineProfile CardPM
+Quote Post

#include<wmx010>
RE: Dynamic Memory Problem ?
3 Feb, 2008 - 01:06 AM
Post #10

D.I.C Head
**

Joined: 19 Jan, 2008
Posts: 75



Thanked: 1 times
My Contributions
QUOTE(WXY @ 3 Feb, 2008 - 12:32 AM) *


There's your problem.

sizeof cannot be used to determine size of memory buffers allocated dynamically. If you must know the size of dynamically allocated buffers use _msize


I'll try this. Thank you both for the help...

This post has been edited by #include<wmx010>: 3 Feb, 2008 - 01:10 AM
User is offlineProfile CardPM
+Quote Post

KYA
RE: Dynamic Memory Problem ?
3 Feb, 2008 - 01:07 AM
Post #11

#include <nerd.h>
Group Icon

Joined: 14 Sep, 2007
Posts: 5,910



Thanked: 159 times
Dream Kudos: 1375
My Contributions
QUOTE(WXY @ 3 Feb, 2008 - 01:32 AM) *

CODE
_firstName = new char[strle....
.... sizeof(_firstName)

There's your problem.

sizeof cannot be used to determine size of memory buffers allocated dynamically. If you must know the size of dynamically allocated buffers use _msize



Interesting. Using _msize just moves the crash a bit farther down and only Susan gets instanitized before it falls.

--KYA
User is online!Profile CardPM
+Quote Post

#include<wmx010>
RE: Dynamic Memory Problem ?
3 Feb, 2008 - 01:09 AM
Post #12

D.I.C Head
**

Joined: 19 Jan, 2008
Posts: 75



Thanked: 1 times
My Contributions
QUOTE(KYA @ 3 Feb, 2008 - 02:07 AM) *

Interesting. Using _msize just moves the crash a bit farther down and only Susan gets instanitized before it falls.
--KYA


So. should i use it or not ?? Confused ???
User is offlineProfile CardPM
+Quote Post

2 Pages V  1 2 >
Reply to this topicStart new topic
Time is now: 1/8/09 11:12PM

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