2 Replies - 2311 Views - Last Post: 11 November 2009 - 04:00 PM Rate Topic: -----

#1 DTG  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 10-March 09

Problem Passing char array to function

Post icon  Posted 11 November 2009 - 02:29 PM

Hi all,
As the title suggests, I am having an issue with passing a char array to a function and then working on the passed array. It has been a while since I have used c++ and most of the relevant knowledge has escaped me.

#include <fstream>
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>

using namespace std;

[b]char* processNibble(char *nibble)[/b]{
	char *modded = new char[6];
	//modded[6] = '\n';
	if (strcmp(nibble, "0000") == 0){
		strcpy(modded, "100000");
		cout << modded; 
		return modded;
	}else if (strcmp(nibble, "0001") == 0){
		strcpy(modded, "010001");
		cout << modded;
		return modded;
	}else if (strcmp(nibble, "0010") == 0){
		strcpy(modded, "110010");
		cout << modded;
		return modded;
	}else if (strcmp(nibble, "0011") == 0){
		strcpy(modded, "100011");
		cout << modded;
		return modded;
	}else if (strcmp(nibble, "0100") == 0){
		strcpy(modded, "100100");
		cout << modded;
		return modded;
	}else if (strcmp(nibble, "0101") == 0){
		strcpy(modded, "010101");
		cout << modded;
		return modded;
	}else if (strcmp(nibble, "0110") == 0){
		strcpy(modded, "110110");
		cout << modded;
		return modded;
	}else if (strcmp(nibble, "0111") == 0){
		strcpy(modded, "100111");
		cout << modded;
		return modded;
	}else if (strcmp(nibble, "1000") == 0){
		strcpy(modded, "101000");
		cout << modded;
		return modded;		
	}else if (strcmp(nibble, "1001") == 0){
		strcpy(modded, "011001");
		cout << modded;
		return modded;
	}else if (strcmp(nibble, "1010") == 0){
		strcpy(modded, "111010");
		cout << modded;
		return modded;
	}else if (strcmp(nibble, "1011") == 0){
		strcpy(modded, "101011");
		cout << modded;
		return modded;
	}else if (strcmp(nibble, "1100") == 0){
		strcpy(modded, "101100");
		cout << modded;
		return modded;
	}else if (strcmp(nibble, "1101") == 0){
		strcpy(modded, "011101");
		cout << modded;
		return modded;
	}else if (strcmp(nibble, "1110") == 0){
		strcpy(modded, "111110");
		cout << modded;
		return modded;
	}else if (strcmp(nibble, "1111") == 0){
		strcpy(modded, "101111");
		cout << modded;
		return modded;
	}
}
[b]
char* processNibble(char*);[/b]

int main(){
	ifstream inFile;
	char c;
	char ch[4];// = new char[4];
	int i = 0;
	//ch[5] = '\n';
	inFile.open("in.txt");
	if (!inFile.good()){
		cerr << "Unable to open file!  Dunno why" << endl;
		return 0;
	}else{
		while (!inFile.eof()){
			inFile >> c;
				if (c == '-'){
					break;
				}else if (c == '0' || c == '1'){
					if (i < 4){
						ch[i] = c;
						i++;
					}else if (i == 4){
						[b]processNibble(ch);[/b]
						i = 0;
						ch[i] = c;
						i++;
					}
			}
		}
	}
}


After the array is passed and I look at its value in a debugger, it looks like only the first character made it to the function. This causes problems later during some compares. I bolded the function, its prototype, and where I am calling it from. This code seems to compile with no problems under VS 2008. Any help in solving this problem would be greatly appreciated!

Is This A Good Question/Topic? 0
  • +

Replies To: Problem Passing char array to function

#2 Ancient Dragon  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 81
  • View blog
  • Posts: 679
  • Joined: 19-July 09

Re: Problem Passing char array to function

Posted 11 November 2009 - 03:20 PM

>> char *modded = new char[6];
>>strcpy(modded, "100000");

You are destroying memory there because modded is not big enough to hold all those characters. Don't forget that strcpy() puts a '\0' null terminator at the end of the string.

main() is causing a huge memory leak because it is ignoring the return value of processNibble(). Do one of two things: (1) change processNibble() so that it is a void return function and so that it does not allocate new memory. or (2) change main() to capture the return value of processNibble() then delete[] the memory.

This post has been edited by Ancient Dragon: 11 November 2009 - 03:26 PM

Was This Post Helpful? 1
  • +
  • -

#3 DTG  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 10-March 09

Re: Problem Passing char array to function

Posted 11 November 2009 - 04:00 PM

View PostAncient Dragon, on 11 Nov, 2009 - 02:20 PM, said:

>> char *modded = new char[6];
>>strcpy(modded, "100000");

You are destroying memory there because modded is not big enough to hold all those characters. Don't forget that strcpy() puts a '\0' null terminator at the end of the string.

main() is causing a huge memory leak because it is ignoring the return value of processNibble(). Do one of two things: (1) change processNibble() so that it is a void return function and so that it does not allocate new memory. or (2) change main() to capture the return value of processNibble() then delete[] the memory.

The reason I am ignoring the return value for now is because I wanted to test passing the array before moving on. By the way, I managed to get things working correctly, but thanks for taking a look at my code
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1