Program Crashes when i modify one of the parameters

This is a discussion on Program Crashes when i modify one of the parameters within the C++ Programming forums, part of the General Programming Boards category; I have a function in a class but whenever i try to modify one of the parameters in the function ...

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    112

    Program Crashes when i modify one of the parameters

    I have a function in a class but whenever i try to modify one of the parameters in the function the program crashes.

    This is my .h file:
    Code:
    class InfFiles
    {
    public:
    	char* GetValue(char option[50]);
    	void  SetValue(char option[50], char Value[50]);
    	char  fileName[100];
    };
    This is my code for the GetValue() function:
    Code:
    #include <windows.h>
    #include <fstream.h>
    #include "class - inf.h"
    
    char* InfFiles::GetValue(char option[50])
    {
    	ifstream file;
    	file.open(fileName, ios::in);
    
    	char searchResult[100];
    	int  size = strlen(option);
    	bool optionFound = false;
    	while(!file.eof())
    	{
    		file.getline(searchResult, 100);
    		searchResult[size] = NULL;
    		if(strcmp(option, searchResult)==0)
    		{
    			optionFound = true;
    			return searchResult;
    		}
    	}
    	return NULL;
    }
    If i put "strcat(option, " = ");" or "option[0] = NULL" into the GetValue function the program will crash. Also if the GetValue function returns "option" i get a returned value but if it returns "searchResult" it just returns NULL.

    Please help me
    thanks
    Last edited by pinkcheese; 01-27-2003 at 09:19 PM.

  2. #2
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    >>char searchResult[100];
    You try to return a pointer to a local variable, this won't work because searchResult is destroyed when the function returns, there's nothing to point to but garbage.

    >>searchResult[size] = NULL;
    You shouldn't confuse NULL with '\0', they may be equivalent but using NULL might kill things too, better to be safe :-)

    >>If i put "strcat(option, " = ");" or "option[0] = NULL" into the GetValue function the program will crash.
    My best guess is that option is a string literal and is in read only memory. When you try to modify it, you get an access violation.
    *Cela*

  3. #3
    Registered User
    Join Date
    Nov 2002
    Posts
    491
    A stylistic note, member variables are usually private in a class and member functions public. Why this is so has been written about many times, so a quick google search (or looking around on this site) should reveal some answers. I'm not saying your code is wrong in anyway, just that you should get into the habit of it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. My program crashes :|
    By Testify in forum C++ Programming
    Replies: 12
    Last Post: 06-27-2007, 11:48 AM
  2. Program crashes and memory leaks
    By ulillillia in forum Tech Board
    Replies: 1
    Last Post: 05-15-2007, 10:54 PM
  3. program crashes on closure.
    By ssjnamek in forum C++ Programming
    Replies: 7
    Last Post: 09-26-2005, 04:55 PM
  4. two strcpy-ies crashes my program
    By Jakob in forum C++ Programming
    Replies: 3
    Last Post: 03-28-2005, 06:17 AM
  5. My program crashes with this code
    By blackwyvern in forum C++ Programming
    Replies: 3
    Last Post: 01-27-2002, 11:28 PM

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