Comparing characters in C++

EvilManagedCare

Senior member
Nov 6, 2004
324
0
0
Hi,

Yes, this is in regard to a class project, however, this portion is trivial, and not the purpose of the project. But, because I have been doing the rest of it for hours, I am fried and can't seem to make this part of it work. The project itself is a system simulation with discrete events, and I am trying to simulate the memory portion with a character array.

I have an array of characters, that is memory[] is a 256 element array that I'm using to simulate memory of 256 units. I'm not interested in string manipulation per se. Each element represents a memory cell, with character '_' denoting emtpy memory.

Anyway, I am trying to check if an element contains the character. I have been iterating through the array, and tried the comparison if (memory == '_') do this, etc. This never evaluates to be true, which is odd because the array is initialized to be filled with all '_' character. And it is as I've printed the array and seen this. So my question, how can I compare to characters? Please be merciful, I have been putting the rest of this thing together and am positively at a loss. What am I missing?

Thanks in advance.
 

nickbits

Diamond Member
Mar 10, 2008
4,122
1
81
Need to see code or at least your declaration of memory.

But my guess is your comparison is comparing a pointer to a character, not a character to character. i.e. you need to dereference the pointer (*) or specify an array index.
 

Markbnj

Elite Member <br>Moderator Emeritus
Moderator
Sep 16, 2005
15,682
14
81
www.markbetz.net
I think nickbits probably nailed it. You only give hints, but the hints you give are wrong: memory is shown as an array, but later you try to compare the name of the array to '_', which can't be right if memory is indeed an array. So yeah, post an example.
 

EvilManagedCare

Senior member
Nov 6, 2004
324
0
0
All right, here's the code. memory is declared publicly as char memory[256]. It is passed as a parameter to the loadMemory function. Job is a struct defined elsewhere. Here it is:

void loadMemory(job j, char m[])
{
/*
First, find holes.
Add holes to holeList
After all holes found, load job according to management scheme.

Trying to redo without pointers because I can't get that approach to work.
*/
int i = 0;
// char * pHoleStart = m;
// char * pEmpty;
int hs; // Size of memory hole.
int base;

list<hole> holeList;

for (i = 0; i < MEMORY_SIZE; i+= hs)
{
// char c;
hs = 0;
if (m[ i ] == '_') // This is where I'm expecting true for the first invocation of this
{ // TEST
cout << "Found a hole!\n";
hs++;
base = i;
int nextc = base + 1;
while (m[nextc] == '_')
{
hs++;
nextc++;
}
hole tempHole = {base, hs};
holeList.push_back(tempHole);
}
}

This has undergone some changes throughout the day. It may very well be unintelligible because I slipped into a psychogenic fugue after a long day. What it's _supposed_ to do is find the first occurance of '_', then count subsequent occurrances from that position. The number of these ends up becoming a memory hole of size hs. The first occurance and hs are put into a struct called hole, to represent a memory hole in which a new process can be allocated. You can see I have commented out some pointers as I tried it with those first to no avail.

Thanks again for the help.
 

nickbits

Diamond Member
Mar 10, 2008
4,122
1
81
Your problem is that m == '_' should be m == '_' (m means &m[0]) which isn't what you want.

But my general impression is that your whole algorithm is flawed. Hopefully you can fix that up after you fix the m error. My main concern is why you are incrementing the loop by hs. I think your intent is to skip the hole during the next interation of the loop but I don't think that is what it is doing.
 

DaveSimmons

Elite Member
Aug 12, 2001
40,730
670
126
^ forum ate tag that should be:

m==' _' should be m[ 0 ] == '_' <== without the [ 0 ] you're comparing the memory address of the buffer that m points to, not the first element of the buffer contents. *m would work too.
 

Cogman

Lifer
Sep 19, 2000
10,286
147
106
Originally posted by: nickbits
Your problem is that m == '_' should be m == '_' (m means &m[0]) which isn't what you want.

But my general impression is that your whole algorithm is flawed. Hopefully you can fix that up after you fix the m error. My main concern is why you are incrementing the loop by hs. I think your intent is to skip the hole during the next interation of the loop but I don't think that is what it is doing.


:) not his problem. quote his code you will notice that he has it right, what you discovered is that the [ i ] is an italics command :)

Your problem lies in the variable hs. hs is not declared so it is loaded with garbage. The problem is that that is how you are looping through the memory is by the size of hs. So the if statement is never called because the for loop never iterates.
 

EvilManagedCare

Senior member
Nov 6, 2004
324
0
0
Sorry about the typo, I fixed the m to read m[ i ] == '_' which is what it reads in my actual code. I didn't realize that would invoke the italics command. Oops.

Thanks, cog, I'll try initializing hs. It was driving me crazy trying to figure out why the loop wouldn't iterate. I do not deny the algorithm could be flawed as well :)
 

DaveSimmons

Elite Member
Aug 12, 2001
40,730
670
126
^ good catch Cogman. The code looks plausible. This is where single-stepping comes in handy. I wonder if MEMORY_SIZE is properly defined as 256?
 

EvilManagedCare

Senior member
Nov 6, 2004
324
0
0
Originally posted by: DaveSimmons
^ good catch Cogman. The code looks plausible. This is where single-stepping comes in handy. I wonder if MEMORY_SIZE is properly defined as 256?

I'm hoping it was defined right. I defined it at the beginning as a constant. Outside any functions it was const int MEMORY_SIZE = 256; But the way my day has gone with this, who knows? After what Cogman said about the hs being defined with garbage, I see base is suffering the same fate. I will change both of those.

Thanks very much for the help so far, everyone. It's nice that at this late hour on a Sunday people are responding, and with no snarky comments about my C++ skills. This has been a trial by fire to say the least returning to school :)
 

zebano

Diamond Member
Jun 15, 2005
4,042
0
0
# deleted since it was too snarky.

2. You have 4 opening brackets '{' and only three closing brackets '}' I assume this is a copy and paste error but it makes my indentation wrong when I paste it in an editor.

3. The code as edited at 4/5 at 10:27 PM won't work. You went from hs being uninitialized (good catch cogman) to being 0 which means that i will never increment unless m[0] != '_'
for (i = 0; i < MEMORY_SIZE; i+= hs)
{
// char c;
hs = 0;
if (m[ i ] == '_')
.... // you then edit hs again if it's in a hole and don't set it back to one before the next time through the loop.
//This is an infinite loop I assume you mean to use i++ in your for loop declaration.

4. Your comments mention that you kick off a job after all holes are found... this isn't done but since it's not pertinent to the problem code, you probably left it out intentionally.

5. You should learn to use a debugger and single step through this code. It would be very instructive. Most debuggers have a 'watch' area where you can watch the precise value of variables/registers as you go through the code. A cod file could also be instructive.


Edit:

I looked at it again and I understand why you used i+=hs; since you don't want to recheck any portion of the hole, but your hs=0 should be hs=1 since if your evaluation is false you want to go onto the next char.