Error with Dynamic Arrays

This is a discussion on Error with Dynamic Arrays within the C++ Programming forums, part of the General Programming Boards category; I am writing a program that asks the user for their first, middle, and last name, then concatenates the 3 ...

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    6

    Error with Dynamic Arrays

    I am writing a program that asks the user for their first, middle, and last name, then concatenates the 3 fixed size arrays into one array that is dynamically created based on the length of the full name.

    When executing the program, I get this error:

    Unhandled exception at 0x1026f8e0 (msvcr90d.dll) in Lab5.exe: 0xC0000005: Access violation reading location 0x00000000.

    It occurs in the DisplayName function. I noticed when I return to Main after calling my Allocate function that the ptrName shows <bad pointer> in the debug menu.

    Here is the code for my 3 files:

    Lab5header.h

    Code:
    // Function Prototype
    void Allocate(char [], char [], char [], char *);
    void DisplayName(char *);
    Main.cpp

    Code:
    // Pre-defined headers
    #include <iostream>
    #include <cstring>
    
    // User-defined headers
    #include "Lab5Header.h"
    
    using namespace std;
    
    const int ARRAY_SIZE = 12;
    
    int main()
    {
    	// Declare fixed-sized array
    	char firstName[ARRAY_SIZE] = "";
    	char middleName[ARRAY_SIZE] = "";
    	char lastName[ARRAY_SIZE] = "";
    
    	char *ptrName = NULL;
    
    	// Variable to check whether user wants to continue program or not.
    	char answer = 'y';
    
    	do
    	{
    		cout << "WARNING: First, middle, and last name can only be up to 12 characters long.";
    		cout << endl << endl;
    
    		// Get users last, first, then middle name.
    		cout << "Please give me your last name. > ";
    		cin >> lastName;
    
    		cout << endl << "Please give me your first name. > ";
    		cin >> firstName;
    
    		cout << endl << "Please give me your middle name. > ";
    		cin >> middleName;
    
    		// Find the total length of the users full name.  Then, dynamically allocate a new
    		// array for the users full name.
    		Allocate(lastName, firstName, middleName, ptrName);
    
    		// Call function to display the full name in its correct order
    		DisplayName(ptrName);
    	}
    	while (answer == 'y' || answer == 'Y');
    
    	// Delete the array we created
    	delete [] ptrName;
    	ptrName = NULL;
    
    	system("pause");
    	return 0;
    }
    Implementation.cpp

    Code:
    // Pre-defined headers
    #include <iostream>
    #include <cstring>
    
    // User-defined headers
    #include "Lab5Header.h"
    
    using namespace std;
    
    
    void Allocate(char lastName[], char firstName[], char middleName[], char *ptrName)
    {
    	// Determine the length of the array	
    	int arrayLength = strlen(firstName) + strlen(middleName) + strlen(lastName) + 3;
    
    	// Create new Array
    	ptrName = new char [arrayLength];
    
    	// Copy and append First, Middle, and Last names into the new array
    	strcpy(ptrName, firstName); 
    	strcat(ptrName, " ");
    	strcat(ptrName, middleName);
    	strcat(ptrName, " ");
    	strcat(ptrName, lastName);
    
    	cout << endl << ptrName << endl;
    
    	return;
    }
    
    void DisplayName(char *ptrName)
    {
    	cout << endl << endl << ptrName;
    	return;
    }
    I know that I need to pass the pointer by reference but all my attempts at this end up giving me more errors.

  2. #2
    Registered User
    Join Date
    Aug 2008
    Posts
    188
    ptrName gets initialized inside Allocate, but this information doesn't go back to the calling function, so when you call DisplayName, ptrName is still NULL. you need to return the pointer value.
    Last edited by bling; 11-26-2008 at 02:30 PM.

  3. #3
    Registered User
    Join Date
    Nov 2008
    Posts
    6
    That is the problem bling. In Allocate, the last cout line where I print ptrName... it print what I expect it to. Back in main, when I call DisplayName, ptrName is no longer what I am expecting it to be.

    In my previous usage with pointers I would create the pointer then assign it the address of the variable to it. But since I have to create this new array dynamically, I don't think I can do the same that I've done in the past.

    I guess what it's coming down to is how to I pass ptrName that isn't currently pointing to anything?

  4. #4
    Registered User
    Join Date
    Aug 2008
    Posts
    188
    when you're passing pointers, it's really just passing a number.

    imagine Allocate having the following signature:
    void Allocate(char last[], char first[], char mid[], int ptrName);

    that's what's really happening...

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    You can either return the pointer as bling suggested or pass the pointer in by reference.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,829
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    That link asks for a password (dont know why).
    In any case, if you wanted to work with pointers you would one of these:
    Code:
    void Allocate(char lastName[], char firstName[], char middleName[], char** ptrName){...}
    char* Allocate(char lastName[], char firstName[], char middleName[], char *ptrName){...}
    void Allocate(char lastName[], char firstName[], char middleName[], char*& ptrName){...}
    The best way for me is the third, because you won't have to change anything from your code.
    Or alternative, you use the second, but better it would be like this (emitting one unecessary parameter):
    Code:
    char* Allocate(char lastName[], char firstName[], char middleName[])
    {
        ...
        char* ptrName = new char[ArrayLength]
        ...
        return ptrName;
    }
    
    //in main
    char* ptrName = Allocate(lastName, firstName, middleName);

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,829
    The 3rd is the best C++-way.
    The first is the C way.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating and freeing dynamic arrays
    By circuitbreaker in forum C++ Programming
    Replies: 8
    Last Post: 02-18-2008, 10:18 AM
  2. Replies: 16
    Last Post: 01-01-2008, 03:07 PM
  3. Dynamic (Numeric) Arrays
    By DavidB in forum C++ Programming
    Replies: 5
    Last Post: 05-03-2006, 07:34 PM
  4. dynamic arrays and structures
    By godofbabel in forum C++ Programming
    Replies: 1
    Last Post: 10-13-2002, 03:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21