When your question is answered use !solved to mark the question as resolved.
Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.
191 messages · Page 1 of 1 (latest)
When your question is answered use !solved to mark the question as resolved.
Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.
struct Entry {
char *word;
unsigned count;
};
struct Bucket {
unsigned numEntries;
Entry *entries[BUCKET_LENGTH];
};
the line
bucket->entries=NewEntries;
invali
invalid
expression must be a modifiable lvalue
why?
entries is an array which you cannot overwrite with simple assignment.
It's the equivalent of
int ia[42];
ia = new int(100);
can't change the array like that.
If you meant an array position then you have to add the position.
Like bucket->entries[position] = NewEntries;.
It seems like what you really want is to change the type of bucket->entries to std::vector<Entry>.
Alternatively if you want to change the array that is bucket->entries you have to use Entry* instead of Entry[].
so i think the solution is delete old array and make new one
Yeah, except just like with ia up there you can't do that when it's an array.
Gotta change it to a pointer that points to some number of Entrys.
The way I see it, entries is a bucket of entries. It's not just entries. So bucket->entries=NewEntries is incorrect, because you're assigning entries to a bucket of entries. The types don't match and the logic doesn't work out. bucket->entries[position] = NewEntries; is correct because you're putting entries in the bucket of entries.
The bucket never changes, only the entries that are in the bucket.
i initialized newEntries as an array
newEntry is the entry
sorry for bad naming
Entry *newEntry= new Entry;
newEntry->count=1;
newEntry->word=NewWord;
Entry *NewEntries= new Entry[bucket->numEntries+1];
I think that is basically right, except "newEntry is the entries", plural.
did i initialize it wrong
I think new Entry[bucket->numEntries+1]; is not correct. The number of entries should not depends on the bucket size.
wdym by that
Anyway, the solution is to read again what the purpose of this is. I feel like the point is to make a bucket of entries, as in the bucket holds lists of entries. NewEntry is a list of entries and gets put in the bucket. The bucket is not a list of entries, it's a list of lists of entries. But I just pieced that together from the variable names, you probably have a text description that explains this properly.
yeah
bucket has an array of entries
dynamic
and the num of entries is length of the array basically
bool bucketInsert(Bucket *bucket, const char *word, unsigned int count) {
bool hasNewEntry = false;
Entry *entry = bucketFind(bucket, word);
// TODO BEGIN
if(bucket->numEntries>=BUCKET_LENGTH){
return hasNewEntry;
}
if(entry!=nullptr){
entry->count=count;
return hasNewEntry;
}
// get past this point
int size=strlen(word);
char *NewWord=new char[size];
strcpy(NewWord,word);
Entry *newEntry= new Entry;
newEntry->count=1;
newEntry->word=NewWord;
Entry *NewEntries= new Entry[bucket->numEntries+1];
if(bucket->numEntries>0){
for(int i=0;i<bucket->numEntries;i++){
NewEntries[i]=bucket->entries[i];
}
delete[] bucket->entries;
}
NewEntries[bucket->numEntries]=newEntry;
bucket->entries=NewEntries;
bucket->numEntries++;
hasNewEntry=true;
// TODO END
return hasNewEntry;
}
my code is basically if it gets past that point that means it needs to create a new entry
logic is correct
i just had a memory leak
Bucket has an array of entry pointers, not entries. And I think the point of that is that bucket has a list of variable-many entries.
I don't think that is correct. I think the length of the array is only the maximum number of entry lists. It can hold infinite entries.
You can't initialize it to something higher the way you imagine. It's an array with a fixed size and its size cannot change.
huh
But the entry*s inside can point to more than 1 entry.
I think the key is that bucket->entries is not a list of entries but a list of lists of entries. Then it should start making sense.
Entry* is a list of entries, you dynamically allocate the number of entries you want with new.
yes
Entry*things[123]; is a list of 123 entry lists.
The length of things is fixed, you always have exactly 123 entry lists.
Some of them may be nullptr or something.
struct Entry {
char *word;
unsigned count;
};
ohh
so entry is a list?
is that what you mean
that entry
No, Entry is not a list, only a single entry.
Entry * is not just a pointer to an entry though, Entry * points to something like new Entry[count] and therefore points to count entries, so it's basically a list of entries.
Entry entry; //single entry
Entry *entries; //list of entries
Entry *bucket[213]; //list of lists of entries
unsigned numEntries;
Entry *entries[BUCKET_LENGTH];
};
bucket has its own initializatio
bucket is bucket
I think the naming is just dumb.
numEntries is incorrect, it should be numEntryLists.
And it should be Entry *bucket[BUCKET_LENGTH];.
The bucket has the length bucket length.
It's understandable if you confuse the bucket with lists since apparently your teacher also confused them.
bucket length is like
wait
its just 128
Yeah, it has to be a number known at compile time because C++ says so.
wait
And it can't change.
Right
what am i doing
It's a fixed array of dynamic arrays 😄
the entry is the dynamic array right?
Maybe. I lost track of which entry you mean.
when i say entry i just mean the Entry struct
the word inside it is dynamic
Sure, but it's still only 1 entry.
what is the dynamic array then
Entry *
That's not exactly an Entry *, that's an array of Entry *s.
In other words, a list of lists of entries.
but its fixed right
Yes, there is a fixed number of entry lists.
so no dynamic?
The entry lists are dynamic.
A fixed number of lists, but each list has a dynamic number of entries.
Yes
You could theoretically put all entries in the same list, but there is probably a purpose to keeping them separate.
Maybe all the entries where the word starts with a goes into one list in the bucket and so on.
its not a list though
i cant access it
no list of list
unless vsc is acting up
its just a fixed array of a single entry
in each index
bucket->entries[0]; should be the first entry list.
That's an issue of C/C++ being dumb. Entry* can point to 1 Entry or to none, or to many. You can't tell. Your IDE can't either. Your debugger could in theory sometimes, but in practice they cannot.
ok
What's the error message here?
Ignore the squigglies, try to compile and look at the compiler's error message.
The squigglies are shortened and sometimes wrong.
no
i havnet put ; yet
if you type bucket.[something] it will autoconvert it
into the arrow
Well, bucket->entries[1].if is invalid syntax, so it complains.
Yeah, makes sense, because bucket is a Bucket *, right?
So bucket. doesn't work, pointers don't have .something.
it converts it to ->
So your IDE thinks you must have meant bucket-> which makes sense.
The -> is correct, I don't see the problem.
there is no problem
im just saying its not a list of entries
inside of bucket->entry[0]
Well, true, bucket is a pointer to 1 Bucket which contains an array of Entry *s which each point to a variable number of Entrys.
bucket->entry[0] should be an Entry*.
Chances are that bucket->entries[0] is invalid because you didn't put a list in yet.
That's the purpose of unsigned numEntries;. Technically bucket->entries is a fixed size array, but practically only some lists are logically valid and numEntries probably says that the first numEntries lists are valid.
And chances are numEntries starts out with 0.
So before you can access a list you have to create a list, assign it and adjust numEntries.
wdym by this
Well, you have
struct Bucket {
unsigned numEntries;
Entry *entries[BUCKET_LENGTH];
};
Clearly numEntries tells us how many entries are in Bucket.
But also, the number of entries are fixed, it's BUCKET_LENGTH. Doesn't make any sense.
So it must be for something else.
I assume it's for saying that numEntries values in entries are valid and the rest are garbage.
To keep track of what's in entries.
Or maybe it expresses the length of the lists. Who knows.
It’s the amount of valid entries
My code doesn’t access it though
Is the thing
Unless I’m dumb
So far you haven't accessed it, no. But I assume your task requires you to do so eventually.