Clockowl 56 Posting Whiz

Not in any imaginable way if you ask me. Like I said, a pointer to an array of pointers (an array of "arrays") resembles a 2D array. Unless you want that, there's no use for a double pointer.

You can always create the double pointer at any time by &students, so if the need would arise it has a simple solution.

Clockowl 56 Posting Whiz

It is only a pointer, don't worry. It's a pointer that points to pointers.

The struct declaration indeed only reserves one pointer, not an array or anything.

However, I guess you want to have *students in your struct. An "array" of students, right? That makes a bit more sense than what you have now (which resembles a 2D array of students).

Clockowl 56 Posting Whiz

hehe, you should access sc.students then, not students. ;)

Clockowl 56 Posting Whiz

#
students = malloc(sizeof(struct student));
#
strcpy(students[0]->name, "jack");

I might have missed it, but of what type is "students"? It seems to be an int, since the declaration is lacking a type.

Clockowl 56 Posting Whiz

Well for one change your extension from .cpp to .c, since there's no other way for the compiler to know if it's compiling C or C++ code (except maybe a parameter).

Clockowl 56 Posting Whiz

Narrow the problem down a bit and you'll recieve more help.

Clockowl 56 Posting Whiz

Hey guys,

I'm trying to create an MSCache (2 times MD4) cracker. I took the RFC thingy and implemented it in a cracker which is.. fast, but not as fast as I'd like it to be. So I searched a bit and stumbled on MDCrack, which claims it can do about 10 million MD4 hashes per second on my system, while I only get 4 million per second.

Since it's open source, I downloaded the source code of an old version and studied the MD4 algorithm, and it seems to partially reverse it. However, I lack knowledge of just about everything considering hashes and was wondering if someone here could teach me one or two things as the weaknesses of these cryptographic functions, how you would create secure ones and of course how to attack one.

Thanks in advance,
Nick

Clockowl 56 Posting Whiz

Only plain wrong if you're not using a compiler that has defined it. Given that that's not the case most of the time: check your compiler's documentation and see if you can use it, then, bare in mind that this code is not 100% portable and SHOULD input not work or the program simply crash you should first look at fflush(stdin);.

Clockowl 56 Posting Whiz

But don't do that on those strings. Just accept that they are read-only, you are *not* allowed to write to them, end of story.

Clockowl 56 Posting Whiz

iostream isn't a C library? afaik it's stdio.h.

C++ has different rules regarding this issue apparently.

Clockowl 56 Posting Whiz

What OS and compiler are you using?

Clockowl 56 Posting Whiz

You wouldn't want to do that, I think it's undefined behaviour when you try to do that. Why can't you create your array of char arrays like Aia said?

char d[][5]
Clockowl 56 Posting Whiz

>Oh, and there's absolutely no guarantee that the memory will (or won't) move on every single call, whether you're making it bigger, smaller or the same size.
-
I don't know when it "doesn't work", that's exactly what I'm asking. With "doesn't work" I meant that it doesn't copy.

I figured it'd copy from reading this:
"The realloc() function changes the size of the memory object pointed to by ptr to the size specified by size. The contents of the object will remain unchanged up to the lesser of the new and old sizes. If the new size of the memory object would require movement of the object, the space for the previous instantiation of the object is freed. If the new size is larger, the contents of the newly allocated portion of the object are unspecified. If size is 0 and ptr is not a null pointer, the object pointed to is freed. If the space cannot be allocated, the object remains unchanged."

Does that say that the contents of the memory pointed to will be the same after the call to realloc() when the size is smaller or equal? Or is it nothing of that?

Clockowl 56 Posting Whiz

In no implementation it copies if the size is bigger?

My doubt is that it DOES copy when the size is smaller, so it seemed logical it also does that when the size increases, but the it's undefined behavior.

Clockowl 56 Posting Whiz

Hey guys,

I was wondering if realloc() copied the contents of the block of memory to the new block "on most compilers" IF the new size is bigger. I like to learn good ways to program something, but don't want to reinvent realloc just because it doesn't work on <reallyoldcompiler>.

Thanks in advance,

Clockowl 56 Posting Whiz

Regression? Don't you mean iteration? Could be wrong, but according to wiki (I had to look it up) regression is bug-related, not loop related like recursion.

If you mean iteration, then that is faster. But, recursion could give you a lot cleaner code, so it's basically okay to write big functions recursive, large functions iterative.

Clockowl 56 Posting Whiz

Define "doing the same thing".

I can drive to the store on my bike and in my car, doing 2 different things. Or I can say that I'm going to the store, doing one thing, in 2 ways.

C compilers will probably never output exactly the same output as when you would hand-code it.

Clockowl 56 Posting Whiz

Here you go:

http://www.xmission.com/~nate/glut.html

If you're on windows that is. Else, you need to download glut.h from somewhere else for Linux.. don't know much about linux, sorry.

Clockowl 56 Posting Whiz

You'd better go and code something yourself I think, or show us what you have so far. Of course, that needs to be more than the code posted above + some misc. main function.

Clockowl 56 Posting Whiz

Narue: It's somewhere on this board, let me look it up. Edit: found it:
http://www.daniweb.com/forums/thread137672.html

Code:

int fileSize = 0;
  int n, i, j;
  printf("Opening file: \"%s\"\n", loc);

  FILE *pFile = fopen(loc, "r");
  if(pFile == NULL){
      dbgError("fopen()");
      exit(1);
  }

  fseek(pFile, 0, SEEK_END);
  fileSize = ftell(pFile);
  rewind(pFile);

  char *data = (char*) calloc(sizeof(char), fileSize + 20);

  fread(data, 1, fileSize, pFile);
  if(ferror(pFile)){
      dbgError("fread()");
      exit(1);
  }
  puts(data);

  fclose(pFile);

  printf("Done reading, parsing file now.\n");
  /*You can return here, the "wrong" data is now written to stdout*/
  return 123;

On the second page of the thread is a small testing program and some results showing that I only had the problem using fread() on "text" opened files, not on binary opened ones.

Salem: I didn't build any security in it. I'd have to make assumptions about available RAM, etc. etc. If the user is "stupid" enough to load a 2GB file when having 512MB RAM, so be it. The likeliness of that happening is small however: it reads HTMLs. But thanks for your concern nonetheless.

Clockowl 56 Posting Whiz

Thanks, nice site by the way.

Clockowl 56 Posting Whiz

Hmz.. I remember using fread() on a none-binary file, passing the fileSize (in the count and size parameters of course) and it read almost everything okay, but it messed up the last few lines: the last few lines were repeating lines from, say, 20 lines above that, somewhat like this:

m
o
n
k
e
y

k

e

y

You never had that?

I'm on Windows XP x64, using the latest version of MinGW GCC.

PS: Basically asking if fread() didn't go bananas when you passed it a size bigger than fileSize, on Windows and Linux.

Clockowl 56 Posting Whiz

How would I reuse functions in C? I've tried simply sticking them in a #include <myfunctions.h> , but that didn't work... Any (complete) suggestions/links on how to do this? I googled some and found this:

The way to write a header file is:

functions.h:
int sum( int a, int b) ;

functions.cpp:
#include "functions.h"
int sum( int a, int b) {
return a+b ;
}

compile:
g++ -c functions.cpp -o functions.o

main.cpp:
#include "functions.h"
main() {
cout << sum(1,2) << endl ;
}

compile and execute main:
g++ main.cpp functions.o -o main
main

How would I, obviously replacing g++ with gcc and *.cpp with *.c, do these steps in Code::Blocks?

Thanks in advance,

Clockowl 56 Posting Whiz

How would one use fread() then with a text file, considering you can't determine the size fread will read?

Clockowl 56 Posting Whiz

Is it possible to read a whole file at once with text files, or can I only use fgets and is that the most data I can fetch at any time? (fread() doesn't work on text files, I've experienced and then been told ;-)).

Thanks in advance,

PS: And suppose I use fseek, ftell, to get the filesize of a file, is that size+1 the correct size for the buffer to read in the complete file using series of fgets calls?

Clockowl 56 Posting Whiz

Does anyone know if it's possible to create a shellscript that knows when it's fopen()ed? So when any process fopens() it, it can take an action?

Thanks in advance,

Clockowl 56 Posting Whiz

You'll need 64-bits integers for that. Depends on your compiler (afaik) if it supports it. Look it up. If your compiler supports a bit of C99, you should be able to do:

unsigned long long int b = 8132123781237;
Clockowl 56 Posting Whiz

Howdy folks,

Does anyone know if GCC is capable of creating a fixed length 1D array of an N-Dimensional array for reasons of speed? I've heard it matters quite a lot if one works constantly with N-Dimensional (with N != 1) compared to 1D arrays in terms of speed, but the changing an N-D array to an 1D array is quite easy code-wise, so I thought maybe the compiler would do it automagicly?

Thanks in advance,

Clockowl 56 Posting Whiz

Hey guys,
Here's my story:

I had a HDD: Data, ~300GB
I created a partition for another OS
I'm done with the OS, remove the partition
Now, it shows up as unallocated, and I can't find a way to merge it back to the drive it belongs to!

A screenshot is attached. For the record: I want to merge the unallocated "partition" on Data with Data so it regains it's ~40GB.

Thanks in advance!

Clockowl 56 Posting Whiz

What's in the registers?

Clockowl 56 Posting Whiz

It's probably the indeed that. DS: Data Segment, so it'd be.. DS + [78E32C]. I can't think of anything else, pretty sure it's that.

Clockowl 56 Posting Whiz

To save some resources you could also try and not redraw when resizing, only when it's done resizing. No clue as of how, but I know it's possible.

Clockowl 56 Posting Whiz

Err, template specialization seems to be the way to go for me, but thanks for the effort. :)

Clockowl 56 Posting Whiz

Is it possible to put compile-time conditional code in template functions?

Kinda like,

template <class T>
void print(T &foo){
  cout << foo;
#if T == float
  cout << " is a float." << endl;
#endif
}

Something like that, I hope I made myself clear.

Thanks in Advance,

Clockowl 56 Posting Whiz

You sir, are a hero.

Clockowl 56 Posting Whiz

Hey guys,

How would one create a vector of pointers to functions? I know from C to create an array of functions, but I'd like to learn how to do it with vectors as well. Here are my efforts so far:

#include <iostream>
#include <valarray>
#include <vector>

#include <ctime>
#include <cstdio>

using namespace std;

time_t startTime, nowTime, thenTime;
static const unsigned int SIZE = 1024 * 1024 * 5;

bool ruleone(int x);
bool ruletwo(int x);

int main (int argc, char *argv[]){
    vector<bool*(int x)> rules;
    rules.push_back(ruleone);
    rules.push_back(ruletwo);

    vector<int> numbers;

    for(int i = 0; i < 35; i++){
        numbers.push_back(i * 3);
    }

    for(int i = 0; i < 35; i++){
        for(int n = 0; n < 2; n++){
            if(rules[n](numbers[i])){
                cout << numbers[i] << " passed rule " << n << endl;
            }
        }
    }

    return 0;
}

bool ruleone(int x){
    if(x > 10) return true;
    return false;
}

bool ruletwo(int x){
    if(x > 50) return true;
    return false;
}

That code won't compile with.. lots of errors:

C:\MinGW\bin\..\lib\gcc\mingw32\3.4.5\..\..\..\..\include\c++\3.4.5\bits\allocator.h|81|instantiated from `std::allocator<bool*()(int)>'|
C:\MinGW\bin\..\lib\gcc\mingw32\3.4.5\..\..\..\..\include\c++\3.4.5\bits\stl_vector.h|79|instantiated from `std::_Vector_base<bool*()(int), std::allocator<bool*()(int)> >::_Vector_impl'|
C:\MinGW\bin\..\lib\gcc\mingw32\3.4.5\..\..\..\..\include\c++\3.4.5\bits\stl_vector.h|110|instantiated from `std::_Vector_base<bool*()(int), std::allocator<bool*()(int)> >'|
C:\MinGW\bin\..\lib\gcc\mingw32\3.4.5\..\..\..\..\include\c++\3.4.5\bits\stl_vector.h|142|instantiated from `std::vector<bool*()(int), std::allocator<bool*()(int)> >'|
C:\Code\TEMP\main.cpp|17|instantiated from here|
C:\MinGW\bin\..\lib\gcc\mingw32\3.4.5\..\..\..\..\include\c++\3.4.5\ext\new_allocator.h|75|error: `const _Tp* __gnu_cxx::new_allocator<_Tp>::address(const _Tp&) const [with _Tp = bool*()(int)]' and `_Tp* __gnu_cxx::new_allocator<_Tp>::address(_Tp&) const [with _Tp = bool*()(int)]' cannot be overloaded|
C:\Code\TEMP\main.cpp||In function `int main(int, char**)':|
C:\Code\TEMP\main.cpp|18|error: no matching function for call to `std::vector<bool*()(int), std::allocator<bool*()(int)> >::push_back(bool (&)(int))'|
C:\MinGW\bin\..\lib\gcc\mingw32\3.4.5\..\..\..\..\include\c++\3.4.5\bits\stl_vector.h|557|note: candidates are: void std::vector<_Tp, _Alloc>::push_back(const _Tp&) [with _Tp = bool*()(int), _Alloc = std::allocator<bool*()(int)>]|
C:\Code\TEMP\main.cpp|19|error: no matching function for call to `std::vector<bool*()(int), std::allocator<bool*()(int)> >::push_back(bool …
Clockowl 56 Posting Whiz

Well, that didn't happen to my experience with the latest version of GCC in my program. Is it safe to state that this optimization can be rarely made?

Clockowl 56 Posting Whiz

Doesn't the program state that C-arrays multiplication took about as long as vector and valarray multiplication? Doesn't that indicate there's some other bottleneck? I could be wrong here, quite often missing information, heh.

Clockowl 56 Posting Whiz

Well that makes sense, no? I'm a better programmer leads to discussion and hopefully measurements. It's not so much "I'm a better coder" as "I know the way to do this.".

But back to the topic, how come vectors are equally fast as C-arrays in this example? That makes no sense at all. I thought ArkM showed they were at least factor 5 slower?

Clockowl 56 Posting Whiz

It's called a

bool

in C++ ;)

You can create types with typedef.

typedef <newtype> <oldtype>

IIRC

Clockowl 56 Posting Whiz

You can use atoi() for converting "ASCII" to "integer". :)

Clockowl 56 Posting Whiz

You can have a monster class with a function isHit(const unsigned int x, const unsigned int y); that returns whether the monster is hit. This makes the hit function belonging to the player really short, since it only needs to feed 3 coordinates, the monster does the rest. Like this, you can hide big pieces of code, make sure they work and never look at them again until you want to change them.

Best of luck with it, I might also take a look at it myself if you like? I'm kinda out of projects now, and this seems a nice project with a dedicated developer. Besides, I have some OpenGL skills if you'd like it to be more visual...

Let me know,

Clockowl 56 Posting Whiz

You're coding in C++. Forget about global variables, try and make a class that has all those variables. If you can't fit it in a class or function you're quite possibly doing something wrong. Globals are considered dangerous when you tend to use them as much as you do. It also cleans up your code a lot, 2000 lines is a lot, especially when I think you could make it a lot more readable by splitting all the code up in classes. Having the functions that need the member variables of a class in that same class or friends of that class also eliminates the need to pass every variable and allows you to simply make private "safe" data members.

2, 3, 4 should be solved by learning and using classes. 1 is very advanced and will make your eyes bleed and your program deadlock. ;-) Assigning each monster its own thread would do you little good since you'd have to lock every variable/array it'll access and unlock it... It will probably slow down the program quite a lot.

Well, really try to encapsulate most of the functions and variables into classes. That should be your focus. I like the idea and your work on the RPG though! Very cool. :D


The monster hit algorithm could be something like this right:?

For each monster
   for each hit coordinate
       check if the monster.location == hitcoordinate[n]
       if so do damagerollorsomething
Clockowl 56 Posting Whiz

I said

int main

not

int _tmain

EDIT: That might sound a bit harsh. No hard feelings, it's just what I said.

Clockowl 56 Posting Whiz

Erm..you're using MSVC++ right? What I posted before should work... You might want to get GCC for Windows tho. It doesn't rape the standard as much.

Oh, and post your code again please. Like it is now. May seem tedious, but it gives valuable information.

Clockowl 56 Posting Whiz

Just make it

int main(int argc, char *argv[]){

It also works on MSVC++.

@ iamthwee: Examples on MSDN use that, I think.

Clockowl 56 Posting Whiz

Coming from C, just make n = 100. Access times is about 8 times as fast and you'll lose little memory in return.. Unless you're making a quazillion of those structures of course.

Clockowl 56 Posting Whiz

That structure, &cad, has a member: sin_addr. Use inet_ntoa() to convert it to a string and print it.

Clockowl 56 Posting Whiz

That depends on your OS in C++. C++ itself doesn't have functions for that. Maybe some library does have crossplatform functions for it. If you're on Windows, go to MSDN and lookup the threading functions there.

Clockowl 56 Posting Whiz

It's the only way eh? Well I unrolled the loop as it's only 4 iterations, but thanks a lot for your input. :)