Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 ... 452 453 [454] 455 456 ... 796

Author Topic: if self.isCoder(): post() #Programming Thread  (Read 909845 times)

Reelya

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #6795 on: December 21, 2014, 06:49:16 pm »

Yeah, but you'd have to make over 250,000 of those things before the difference even added up to a single MB. I mean sure, I could see the difference if for some reason you needed to make millions of the things, but most computers these days are swimming in so much memory that a 4 byte difference per class instance isn't going to make any real noticeable impact (not absolute best practice of course, but in most cases it's going to fall well below the work/reward threshold IMO).
It can make a difference in processing speed, because of the limits of CPU cache ram. If you can manage to fit all your working data into the cache you can see significant speed improvements, similar to virtual memory paging, even a little paging can lead to drastically slower peformance.

Also, 1/3rd less time to copy a data structure can't hurt (50% less with 64 bit read/writes), and it will make bigger differences with network data traffic and read/write to disk.

optimumtact

  • Bay Watcher
  • I even have sheep
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #6796 on: December 22, 2014, 05:25:47 pm »

I found a really nice animation and write-up of a dungeon level generator and I thought people here might be interested in reading it

http://journal.stuffwithstuff.com/2014/12/21/rooms-and-mazes/
Logged
alternately, I could just take some LSD or something...

ed boy

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #6797 on: December 29, 2014, 07:56:48 pm »

I'm afraid I'm having some trouble understanding basic concepts of C, and would really like some help. My main issue is with passing an array of strings to a function. Here is an example program:

Code: [Select]
#include<stdio.h>
#include<string.h>

void examplefunction(char *poemstring)
{
    printf(&poemstring[0]);
    printf(&poemstring[1]);
    printf(&poemstring[2]);
    printf(&poemstring[3]);
}

int main()
{
    char poemstring[4][8];
    strcpy(poemstring[0],"oh\n");
    strcpy(poemstring[1],"noetry\n");
    strcpy(poemstring[2],"bad\n");
    strcpy(poemstring[3],"poetry\n");
    examplefunction(poemstring);
    getch();
    return 0;
}

which, when runs, produces the output:

Code: [Select]
oh
h


As I understand it, when I declare poemstring, I declare is as a array of size four of arrays of size eight that hold characters. I then use strcpy to copy the four lines of the poem to the elements of poemstring. This leaves poemstring as an array of size four, with each element an array of characters holding a line of the poem. I then pass this array of strings to the function example function.

As I understand it, although I passed poemstring, the compiler decides that it's only going to pass a pointer to poemstring, which is why I tell examplefunction to expect a pointer to be passed to it by using the asterisk. When I use the printf functions, however, I have to use the ampersand to tell it to look at where the pointer is pointing instead of looking at the pointer itself, and then I tell it to print each of the individual lines by using the square brackets.

I can't see how this is going wrong. Have I made a silly error in my code, am I misunderstanding something about C completely, or am I wrong in some other way?
Logged

Uristides

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #6798 on: December 29, 2014, 08:26:20 pm »

IIRC arrays of arrays are just pointers of pointers, so the "true type" of poemstring is really char ** and not char *. Like, an array is a pointer to the first(0th, technically) element of the array(whose elements, in this case are chars), likewise a bidimensional array is a pointer to the first element of an array whose elements are pointers to the first element of the arrays each of them represent(the chars in each line of your poem here).
Just changing the declaration of examplefunction to take a char ** as argument made it work. Do note that even though the program works the compiler still spills out a warning about type incompatibility, and I'm really to rusty to remember why's that or what's the correct, good C practice in this case.

With that I think you can see where your program went wrong and why it behaved the way it did. Try printf(&poemstring[n]) for n>7, for instance, in your original program.
« Last Edit: December 29, 2014, 08:39:14 pm by Uristides »
Logged

ed boy

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #6799 on: December 29, 2014, 08:39:28 pm »

Just changing the declaration of examplefunction to take a char ** as argument made it work. Do note that even though the program works the compiler still spills out a warning about type incompatibility, and I'm really to rusty to remember why's that or what's the correct, good C practice in this case.

It doesn't work in my end. When I make that change, I get a bunch of warnings about passing argument 1 of printf from incompatible pointer type, and the printed output is:
Code: [Select]
oh
`↨@noetry
ry
Logged

Uristides

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #6800 on: December 29, 2014, 08:52:27 pm »

That would be because I also changed your program on my end to use the convetional syntax with the first argument being a formatting string and the second the stuff to fill the string with:
Code: [Select]
printf("%s", &poemstring[n]);
Sorry, that went over my head on the first post.
Logged

ed boy

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #6801 on: December 29, 2014, 09:05:50 pm »

Can you post the whole thing that you have on your end please? I changed mine as you said, and it still doesn't appear to be working.
What I have on my end:
Code: [Select]
#include<stdio.h>
#include<string.h>

void examplefunction(char **poemstring)
{
    printf("%s",&poemstring[0]);
    printf("%s",&poemstring[1]);
    printf("%s",&poemstring[2]);
    printf("%s",&poemstring[3]);
}

int main()
{
    char poemstring[4][8];
    strcpy(poemstring[0],"oh\n");
    strcpy(poemstring[1],"noetry\n");
    strcpy(poemstring[2],"bad\n");
    strcpy(poemstring[3],"poetry\n");
    examplefunction(poemstring);
    getch();
    return 0;
}

The compiler gives me two warning when compiling:
Code: [Select]
warning: passing argument 1 of 'examplefunction' from incompatible pointer type
note: expected 'char **' but argument is of type 'char (*)[8]'
[\code]
which indicates that the double asterisk when declaring examplefunction might be a bad idea.
Logged

Uristides

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #6802 on: December 29, 2014, 09:21:32 pm »

That's it. I just removed the getch() and played a bit with malloc(commented out, that's more like the way I'm used to deal with that kind of stuff).
Code: [Select]
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

void examplefunction(char ** poemstring)
{
  printf("%s",&poemstring[0]);
  printf("%s",&poemstring[1]);
  printf("%s",&poemstring[2]);
  printf("%s",&poemstring[3]);
}

int main()
{
  //char ** poemstring = (char **)malloc(sizeof(char *)*4);
  //int i = 0;
  //for(i ==0; i< 4; i++){
  //  poemstring[i] = (char *) malloc(sizeof(char)*8);
  //}
  char poemstring[4][8];
  strcpy(poemstring[0],"oh\n");
  strcpy(poemstring[1],"noetry\n");
  strcpy(poemstring[2],"bad\n");
  strcpy(poemstring[3],"poetry\n");
  examplefunction(poemstring);
  return 0;
}
Oh, and before someone calls me out for that, yeah, the dynamic allocation here is really lazy and wasteful.

I'm thinking that strictly the argument should be char poemstring[4][8], that gives no warnings, but looks ugly.(if you do that or my dynamic mess remove the ampersands from printf)
« Last Edit: December 29, 2014, 09:28:38 pm by Uristides »
Logged

Rose

  • Bay Watcher
  • Resident Elf
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #6803 on: December 29, 2014, 10:03:57 pm »

Or just use c++ and do std::vector<std:string> and have everything simple.
Logged

Reelya

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #6804 on: December 29, 2014, 11:56:18 pm »

The problem with passing a char[4][8] as a char ** is that a two-dimensional static-sized array is not implemented as a (char *)[], with pointers for each actual string. it's actually implemented as a single char[32] and the compiler converts all your indexes to that. So the "under the hood" implementation is actually different to manually making a (char *)[4]  then assigning each (char *) to a memory location containing a string. This version shows how it works (tested with VC++ 2012).

I did have to cast the parameter to a (char *) to get around the need to specify the size in the parameter list. That is needed because the char[4][8] in itself doesn't contain any size information, it's just a pointer to the first character, which is proven by the cast working.

Code: [Select]
// because strcpy is deprecated due to exploitable hacks, strcpy_s is the modern bugtested version, but just turning off safety for now
#define _CRT_SECURE_NO_WARNINGS 1

#include<stdio.h>
#include<string.h>

void examplefunction(char *poemstring)
{
    printf("%s",poemstring);
    printf("%s",poemstring+8);
    printf("%s",poemstring+16);
    printf("%s",poemstring+24);
}

int main()
{
    char poemstring[4][8];
    strcpy(poemstring[0],"oh\n");
    strcpy(poemstring[1],"noetry\n");
    strcpy(poemstring[2],"bad\n");
    strcpy(poemstring[3],"poetry\n");
    examplefunction((char *)poemstring);

//    getch();
    while(1==1);
    return 0;
}
So you can pass it the same as a char * but it doesn't have size and dimension information attached because it's just implemented as 32 bytes of memory with a pointed to it. This also explains why you can't pass one of these with a ** the same as the one you make manually as a char **

===

Here's a working version which uses templates to hid the need to cast the reference to a pointer, and avoid the function needing to be told how big the array is. Note that a char[][] implemented as a member of a class is actually implemented differently to the one created in a function body (it's a proper char ** here):

Quote
#define _CRT_SECURE_NO_WARNINGS 1

#include<stdio.h>
#include<string.h>

template <int r, int c> class charArray
{
public:
    char data[r][c];
    char *operator [](int index)
    {
        return data[index];
    }
};

template <int r, int c> void examplefunction(charArray<r,c> poemstring)
{
    for(int index=0; index<r;index++)
        printf("%s",poemstring[index]);
}

int main()
{
    charArray<4,8> poemstring;
    strcpy(poemstring[0],"oh\n");
    strcpy(poemstring[1],"noetry\n");
    strcpy(poemstring[2],"bad\n");
    strcpy(poemstring[3],"poetry\n");
    examplefunction(poemstring);

//    getch();
    while(1==1);
    return 0;
}
« Last Edit: December 30, 2014, 12:32:27 am by Reelya »
Logged

Orange Wizard

  • Bay Watcher
  • mou ii yo
    • View Profile
    • S M U G
Re: if self.isCoder(): post() #Programming Thread
« Reply #6805 on: December 30, 2014, 01:12:04 am »

This is why I prefer Python.
Logged
Please don't shitpost, it lowers the quality of discourse
Hard science is like a sword, and soft science is like fear. You can use both to equally powerful results, but even if your opponent disbelieve your stabs, they will still die.

Telgin

  • Bay Watcher
  • Professional Programmer
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #6806 on: December 30, 2014, 01:22:24 am »

Yeah, C is really only useful these days for development of things like low level OS kernel components and drivers, or embedded systems that either only have a C compiler or have very tight timing requirements.  C++ is better in a lot of ways, but it's still very fiddly and for a few reasons it's still not easily used in those very low level things that C is used for.

Modern languages like Python are usually going to be much more productive to use if you don't have some serious performance bounds on your application.  I do hope to get a feel for that soon though, since my little astronomical simulation is way too slow in Python right now.  It probably won't be an order of magnitude better just for moving it to C++, but it'll be interesting to know how much improvement potential there is.
Logged
Through pain, I find wisdom.

Reelya

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #6807 on: December 30, 2014, 01:35:21 am »

Maybe I could work on porting that with you. I need to learn more about Python and am fluent in c++. I bet we can get some heavy duty speedup going.

alway

  • Bay Watcher
  • 🏳️‍⚧️
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #6808 on: December 30, 2014, 01:57:58 am »

Likely pretty close to an order of magnitude. Most sources I'm seeing say around 5x faster in C++ than python. Which is why performance limited games (basically any big game) is written in C++.


Oh, and some fun trivia about C++ dynamically allocated arrays: they actually do track their size. It's just a number you should never ever touch, since it is implementation specific, could be stored anywhere, and is really owned by the heap, rather than your program. https://stackoverflow.com/questions/197675/how-does-delete-know-the-size-of-the-operand-array
So uh, forget you heard that when it comes to implementing anything, but it's how the delete[] magically knows how much memory to release without it being specified. In visual studio, you can take a look at the memory just before the first entry in the allocated array, and will typically find the array size in bytes there.
Spoiler (click to show/hide)
Logged

EnigmaticHat

  • Bay Watcher
  • I vibrate, I die, I vibrate again
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #6809 on: December 30, 2014, 02:36:36 am »

I have two partially finished games in python that have a LOT of iterating over large arrays, and let me tell you I'm really feeling the lack of that 5x (I've actually heard 10x) performance boost.

That's why I'm learning C++.  Or I would be if I wasn't posting on this forum.
Logged
"T-take this non-euclidean geometry, h-humanity-baka. I m-made it, but not because I l-li-l-like you or anything! I just felt s-sorry for you, b-baka."
You misspelled seance.  Are possessing Draignean?  Are you actually a ghost in the shell? You have to tell us if you are, that's the rule
Pages: 1 ... 452 453 [454] 455 456 ... 796