Program abort due to memory problem

This is a discussion on Program abort due to memory problem within the C++ Programming forums, part of the General Programming Boards category; I have no errors or warnings but my program keep cancelling anyway. The error message is this: The instruction at ...

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    15

    Unhappy Program abort due to memory problem

    I have no errors or warnings but my program keep cancelling anyway. The error message is this:

    The instruction at "0x004053f0" referenced memory at "0x00000000". The memory could not be "read".
    click ok to terminate the program or cancel to debug.


    // this program has a problem with memory


    // Purpose of this program:
    //This program uses the selection sort algorithm to sort
    // an array of names in alphabetical order.
    void showArray(char *[][2], int);
    void selectionSort(char *[][2]);
    #include <iostream.h>
    #include <string.h>
    // Function prototypes
    // this code not used void selectionSort(int [], int);
    // this code not used void showArray(int [], int);
    void main(void)
    {
    char *names[20][2] = {{"Collins, Bill"},
    {"Smith, Bart"},
    {"Allen, Jim"},
    {"Griffin, Jim"},
    {"Stamey, Marty"},
    {"Rose, Geri"},
    {"Taylor Terri"},
    {"Johnson, Jill"},
    {"Allison, Jeff"},
    {"Looney, Joey"},
    {"Wolfe, Bill"},
    {"James, Jean"},
    {"Weaver, Jim"},
    {"Pore, Bob"},
    {"Rutherford, Greg"},
    {"Javens, Renee"},
    {"Harrison, Rose"},
    {"Setzer, Gathy"},
    {"Pike, Gordon"},
    {"Holland, Beth"} };
    cout << "The unsorted names are: \n\n";
    showArray(names, 20);
    selectionSort(names);
    cout << "The sorted names are\n";
    showArray(names, 20);
    }
    //************************************************** ************
    // Definition of function selectionSort. *
    // This function performs an ascending order selection sort on *
    // array. elems is the number of elements in the array. *
    //************************************************** ************
    void selectionSort(char *array[][2])
    {
    char sEarch[15];
    int found = 0;
    cout<<"\nEnter a String to sEarch : ";
    cin >> sEarch;
    cout << sEarch << endl;
    for ( int i = 0 ; (i < 20 && !found); i++)
    {
    for ( int j = 0; (j < 2 && !found); j++)
    {
    cout<<endl << "Comparing " << array[i][j];
    if (strcmp(sEarch,array[i][j])== 0)
    {
    found = 1;
    break;
    }
    }
    }
    if (!found)
    cout <<"\nString is NOT in the array!!!";
    else
    cout <<"\nString is in the array";
    }
    //************************************************** ************
    // Definition of function showArray. *
    // This function displays the contents of array. elems is the *
    // number of elements. *
    //************************************************** ************
    void showArray(char *array[][2], int elems)
    {
    for (int i = 0; i < elems; i++)
    {
    for ( int j = 0 ; j < 2; j++)
    cout << array[i][j]<<" ";
    cout << endl;
    }

    cout<<endl<<endl;
    }

  2. #2
    Registered User
    Join Date
    Aug 2001
    Posts
    129
    What a quick look revealed...

    > char *names[20][2]

    Now you're kind of declaring a three dimensional array. Leave that [2] out and try again...
    kooma - t.h@iki.fi

  3. #3
    Registered User
    Join Date
    Jan 2002
    Posts
    15

    Question Program abort due to memory problem

    Yes, kooma. I removed all the 2's from every areas but the program aborted anyway with this error:
    C:\Documents\Desktop\_#4.cpp(66) : error C2664: 'strcmp' : cannot
    convert parameter 2 from 'char' to 'const char *'

    error is pointing tpo this code:
    if (strcmp(sEarch,array[i][j])== 0)

    Looking for some more help.

  4. #4
    Registered User
    Join Date
    Jan 2002
    Posts
    68
    Looks to me like you want those 2's...bu prob not 2. I only skimmed real quick...but it looks like your making an array of char arrays...I beleive thats char wordArray[a][b], where a is how many names you will have and b is how many leters are allocated for each name(including null character). So it seems 2 isnt enough for what you want.

    I havn't had time to look elsewhere...but a runtime memory error usually means your accesing outside your defined array. If you can't figure this out Ill look a little closer.
    ______________________
    The Gekko

  5. #5
    Registered User
    Join Date
    Jan 2002
    Posts
    68
    Whoops...should give you another freeby since its so obvious...

    if (strcmp(sEarch,array[i][j])== 0)

    you access a name by array[i]...array[i][j] specifies one of its characters...so you want to compare by

    strcmp(sEarch,array[i]==0)

    for example:

    char array[2][5];

    array[0]="Mark"

    cout << array[0] //prints "Mark"
    cout << array[0][1] //prints 'a'
    ______________________
    The Gekko

  6. #6
    Registered User
    Join Date
    Jan 2002
    Posts
    15

    Unhappy More problems with 2 dim array

    Your suggestion doesn't work.
    I tried some different approach. The latest run cancel for a different reason. See the errors below:
    /*
    --------------------Configuration: page500_#4 - Win32 Debug--------------------
    Compiling...
    page500_#4.cpp
    C:\Documents\STUDENT\Desktop\C++_tests\pg_#4.cpp(1 27) : error C2440: '=' : cannot convert from 'char *[17]' to 'char'
    This conversion requires a reinterpret_cast, a C-style cast or function-style cast
    C:\Documents\STUDENT\Desktop\C++_tests\pg_#4.cpp(1 28) : error C2106: '=' : left operand must be l-value
    C:\Documents\STUDENT\Desktop\C++_tests\pg_#4.cpp(1 29) : error C2440: '=' : cannot convert from 'char' to 'char *[17]'
    There are no conversions to array types, although there are conversions to references or pointers to arrays
    C:\Documents\STUDENT\Desktop\C++_tests\pg_#4.cpp(1 39) : error C2143: syntax error : missing ';' before '}'
    C:\Documents\STUDENT\Desktop\C++_tests\pg_#4.cpp(1 39) : error C2143: syntax error : missing ';' before '}'
    C:\Documents\STUDENT\Desktop\C++_tests\pg_#4.cpp(1 39) : error C2143: syntax error : missing ';' before '}'
    Error executing cl.exe.

    pg_#4.obj - 6 error(s), 0 warning(s)
    */

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Mutex and Shared Memory Segment Questions.
    By MadDog in forum Linux Programming
    Replies: 14
    Last Post: 06-20-2010, 05:04 AM
  2. Problems with shared memory shmdt() shmctl()
    By Jcarroll in forum C Programming
    Replies: 1
    Last Post: 03-17-2009, 11:48 PM
  3. Manipulating the Windows Clipboard
    By Johno in forum Windows Programming
    Replies: 2
    Last Post: 10-01-2002, 10:37 AM
  4. memory allocation newbie problem
    By larry in forum C++ Programming
    Replies: 11
    Last Post: 10-08-2001, 09:58 AM

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