• What do you need help with? Version 1
    5,001 replies, posted
[QUOTE=Darwin226;24037584]A question guys. Arrays in C# aren't declared along with their size unlike in C++ where you can do int arr[5]; so I'm wondering what is the difference when used in a struct. I'm assuming if I had a struct in C++ containing only int arr[5]; and made an instance of that struct it would reserve 5*sizeof(int) in memory. How does it work in C# and am I missing something crucial here?[/QUOTE] You do declare their size: [code]int[] numbers = new int[10];[/code]
Apparently there's the "fixed" keyword that allows me to do "fixed int arr[5];" I can only be used in unsafe context, why is that? [editline]06:11PM[/editline] [QUOTE=arienh4;24037680]You do declare their size: [code]int[] numbers = new int[10];[/code][/QUOTE] You can't initialize a field in a struct.
[QUOTE=Darwin226;24037686]Apparently there's the "fixed" keyword that allows me to do "fixed int arr[5];" I can only be used in unsafe context, why is that? [editline]06:11PM[/editline] You can't initialize a field in a struct.[/QUOTE] This doesn't matter in a language like C#, because memory is managed for you. Just define the array and initialize it in the constructor.
Well there are obviously disadvantages to that, so if I can make it run more efficient, why not?
[QUOTE=Darwin226;24038587]Well there are obviously disadvantages to that, so if I can make it run more efficient, why not?[/QUOTE] What exactly are you trying to do? It just seems like you're trying to "optimize" something that doesn't matter at all.
Well, not really trying to do anything. I was just curious how C# does something as opposed to the way C++ does it (yes, yes... the language doesn't actually do anything. You know what I mean) What do you mean it doesn't matter? Would accessing data from an array that it predefined be faster? For example if I had a struct like this in C++ [cpp]struct{ int a; int arr[5]; }[/cpp] I'm assuming that, when I made an instance of that struct, it would reserve a 6*sizeof(int) in the memory, probably in a sequence (right?). Since I can't do that in C# (without using unsafe context and fixed), the memory can't be allocated as long as I don't initialize that array. If it really doesn't matter, fine. If it does, why not use a more efficient method?
[QUOTE=Darwin226;24038905]For example if I had a struct like this in C++ [cpp]struct{ int a; int arr[5]; }[/cpp] I'm assuming that, when I made an instance of that struct, it would reserve a 6*sizeof(int) in the memory, probably in a sequence (right?).[/QUOTE] The problem here is that the struct is an anonymous type, so you can't do shit with it :smile: Also, remember the semicolon after structs and classes and whatnot.
Well, I haven't used C++ in a while so my syntax is rusty. The point is, it's a struct with one int and an array with 5 elements.
In java, can someone explain to me code that will: (a) print a line of text, and then pause for a couple seconds, and then print the next line. and (b) will NOT close the "Console" application when the program is finished printing everything =P also, I haven't been online in a month or two; what happened to the auto-lock after 51 pages? Did garry change it again?
(a) System.out.println("text"); Thread.sleep(3000); //sleep for 3 seconds System.out.println("next line"); (b) Well, those programs are meant to be run from command line. You could wait for input before exiting, so the you have to press enter before it exits. Someone said that the limit has been upped to 5000.
Upped limit is shit, I wanna see highlights:saddowns:
I'm stuck with a weird error with lua. Basically I have a lua table, ENT, with its metatable's index set to an instance of a C# class (userdata). In the lua table is this function: [lua]function ENT:TestFunc( name ) return "Hi there " .. name .. "!" end[/lua] In the C# class is this function: [cpp]public string TestFunc2( String name ) { return "Hello there " + name + "!"; }[/cpp] But when trying them both out I get this: [lua]ent = items.Consumable.Create( "corn" ) -- this just gives a copy of the ENT table print( ent:TestFunc( "James" )) --Hi there James! > print( ent:TestFunc2( "James" )) --instance method 'TestFunc2' requires a non null target object[/lua] ent.TestFunc2 definitely exists. Also public variables in the C# class can be printed fine with ent.VarName. What could be the problem?
[QUOTE=ZeekyHBomb;24040876](a) System.out.println("text"); Thread.sleep(3000); //sleep for 3 seconds System.out.println("next line"); (b) Well, those programs are meant to be run from command line. You could wait for input before exiting, so the you have to press enter before it exits. Someone said that the limit has been upped to 5000.[/QUOTE] Ah ok thank you Will do =P
If my previous question can't be solved, how would I set the metatable of userdata using LuaInterface in C#?
-snip-
@Darwin You don't worry about that in C#. The array is allocated on the heap when you create it (new T[n];) The struct only holds a reference to that array.
[QUOTE=ROBO_DONUT;24036551]Windows Bitmap? Are you writing the loader yourself or is memblock[] the output of some library you're using? I think you have too much padding. You need to pad to the nearest multiple of four. For a ten-pixel width image this would be 10*3=30 bytes, and the closest multiple is 32, so you need 2 bytes of padding, not 4. I have a [url=http://github.com/ml32/imghash/blob/master/src/ihash-img.c]working bitmap loader[/url] in an old library I was writing, if some example code would help. The relevant portion is ihash_img_load_bmp().[/QUOTE] Thank you, that was the problem, I'd misread the page on BMP formatting. And memblock[] is just the binary contents of the bmp file.
[QUOTE=jA_cOp;24015409]SFML doesn't make any assumptions about which OpenGL implementation you use, it only sets up a rendering context. It's as efficient as any other use of OpenGL (but again, it doesn't provide extensions).[/QUOTE] SFML2 though, does include glew.
I've always preferred GLEE because I found it easier to build with. What exactly are the differences?
[QUOTE=Darwin226;24038905]Well, not really trying to do anything. I was just curious how C# does something as opposed to the way C++ does it (yes, yes... the language doesn't actually do anything. You know what I mean)[/QUOTE] In C/C++, the size of an array variable is part of its type because the variable represents the actual contents of the array. An int[5] is the size of five integers, and if you put one in a struct, the struct will be that big. In C#, arrays are objects, so an "array variable" is actually a [i]reference[/i] to an array. If you put an int[] in a struct, the struct will be the size of one reference. Later you can assign something like "new int[5]" or "new int[10]" to it, and the struct doesn't care how many integers there are because it's not holding the integers, it's just holding a reference to an array object, and references are always the same size.
Hmmm, interesting. I have this struct that holds all the data about a single vertex that my .obj model loader uses and OpenGL is able to read from it since it only contains floats, no arrays. So if I for example decided to use an array of floats to hold the properties of the vertex in the struct and have the OpenGL read from the array of those structs (vertices in a mesh) it wouldn't work right? Good to know, good to know.
OpenGL supports "arrays of structs" through things like the [i]stride[/i] parameter of [url=http://www.opengl.org/sdk/docs/man/xhtml/glVertexAttribPointer.xml]glVertexAttribPointer()[/url], which let you tell it the size of your structure so that it can traverse the array, reading just the values it needs from each struct and skipping over the others. If you're talking about a structure like [code] struct VertexInfo { float coords[3]; int otherdata; }; [/code] you'd pass 3 as the [i]size[/i] parameter to glVertexAttribPointer, GL_FLOAT as the [i]type[/i], and sizeof(VertexInfo) as the [i]stride[/i].
But that wouldn't work in C# right?
OpenGL is a C API, so it can't be used directly in C#; presumably you're using some .NET wrapper for it. If it's a "thin" wrapper that directly exposes OpenGL's use of pointers, you have to write unsafe code to use it, so you can use that "fixed" keyword to put actual arrays into structures. If it's a "thick" wrapper that hides the real OpenGL API and presents a different, more .NET-ish type-safe API, then none of this is applicable anyway.
How would I go about converting the bytes gotten from a file(this is for my bmp file reader) into single integers? They are read seperately into different integers, and from what I can see go 1-256, so 1 and 1 make 257, 2 and 1 make 258, etc. This is hexadecimal is it not? How would I go about converting all the bytes into one integer?
[QUOTE=FerrisWheel;24051457]How would I go about converting the bytes gotten from a file(this is for my bmp file reader) into single integers? They are read seperately into different integers, and from what I can see go 1-256, so 1 and 1 make 257, 2 and 1 make 258, etc. This is hexadecimal is it not? How would I go about converting all the bytes into one integer?[/QUOTE] Uh. You didn't explain the question very clearly, so I'm going to have to take a guess at what you mean. 1. If you're reading data from a file, you can directly read integers using fread: [code] int number; fread(&number, sizeof(int), 1, file); [/code] 2. If you have a large array of bytes, and you know that four sequential bytes at some location represent an integer, you can cast the pointer and dereference it. [code] char bytes[0x1000]; /* ...data is loaded into bytes[]... */ int number = *(int*)(bytes+offset); [/code] 3. If you have a bunch of totally separate bytes, which you want to pack into a single integer, you can use left-shift and bitwise-or: [code] char a, b, c, d; /* ...data is loaded into a, b, c, and d... */ int number = (int)a << 0x18 | (int)b << 0x10 | (int)c << 0x08 | (int)d; [/code] Just remember that you must know what endianness (byte order -- big-endian or little-endian) the data is in and reorder it if necessary.
No, that's not hexadecimal. Hex is just a way of writing numbers, not a characteristic of the numbers themselves. Anyway, if you want to read a 16- or 32-bit value, it's simpler to read the whole thing at once than to read the bytes individually and combine them afterward. What functions are you using to read data from the file?
[QUOTE=ROBO_DONUT;24051957] 2. If you have a large array of bytes, and you know that four sequential bytes at some location represent an integer, you can cast the pointer and dereference it. [code] char bytes[0x1000]; /* ...data is loaded into bytes[]... */ int number = *(int*)(bytes+offset); [/code] [/QUOTE] This looks like what I need, but I'm unsure how to use it, bytes is the array of bytes, but what is offset, the place where the bytes start? And how does this combine 2,3,4,etc bytes into the integer?
[QUOTE=FerrisWheel;24052265]This looks like what I need, but I'm unsure how to use it, bytes is the array of bytes, but what is offset, the place where the bytes start? And how does this combine 2,3,4,etc bytes into the integer?[/QUOTE] In program memory, everything is just a series of binary digits. If you type something as a "char", the computer takes just one byte when it accesses that variable. If you type something as an "int", it loads up four sequential bytes. So if you have the string of bytes: [code]char bytes[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xC0, 0xFF, 0xEE }[/code] It appears in memory as [code]DEADBEEFC0FFEE[/code] Now remember that a C/C++ array is really just a pointer to some memory on the stack. When you cast the pointer from (char*) to (int*), you're just telling the processor to read four bytes instead of one, and the result is of the type int. You can also perform arithmetic on pointers before dereferencing (this is what the array shorthand "array[index]" does). Thus, [code]int number = *(int*)(bytes+3); /* or */ int number = *(int*)(&bytes[3]);[/code] loads 0xEFC0FF33 into number. [QUOTE=FerrisWheel;24052265]bytes is the array of bytes, but what is offset, the place where the bytes start?[/QUOTE] "bytes" is the array. Arrays in C/C++ are pointers do blocks of contiguous data. "offset" is like the index into this array. It's how many bytes to skip before loading the data. It is important to understand when writing C/C++ that array operations are nothing but syntactic sugar -- shorthand for pointer operations. Also that "types" are really only there to aid the programmer and mean nothing to the computer itself.
[QUOTE=ROBO_DONUT;24052798]*helpful info*[/QUOTE] Thanks alot, that works quite nicely. Another question, my bitmap loads and displays fine, but I'm using a multidimensional array to store the pixels. [cpp]struct colour { char r; char g; char b; } colour pixels[width][height];[/cpp] I can tell this is inefficient, as the program crashes with a width/height over about 900 in size, meaning my images are limited in size. How would I be able to store larger images? Or just a more efficient way of storing the the pixels in memory?
Sorry, you need to Log In to post a reply to this thread.