« first day (935 days earlier)      last day (2921 days later) » 

00:43
Is there any particular reason why the use of an addr as an index instead of using addr + index is never used? eg. Portability (different pointer sizes), etc...? Code example below.
unsigned int a[5] = {1,2,3,4,5};
unsigned int *i;
for (i = a; i < a+5; ++i) {
    printf("%u\n", *i);
}
And would this be faster if using a non-optimizing compiler (and assuming that the pointer size is the same as the unsigned int in above example)? Asking out of curiosity, not for use in actual code.
01:00
Hey guys so I have a newbie question. AFAIK, char* cannot be modified and must be copied to a new char*, whereas a char[] can be modified by char[5] = 't' and etc...

Can char[] be put on the heap? I only see char* being used with malloc usually
My application of the array would be better on the heap than the stack, but I need to be able to change the chars using the index notation [ ]
01:19
@Byte the thing you are talking about is called "pointer arithmetic", that is, you can think of the p+n expression (where p is a pointer to type t) as jumping n*sizeof(t) amount from the location where p points to in the memory. so in your dummy case basically you can do two things: create a variable, which holds an index value (some sort of unsigned integer) or create a variable which is a pointer pointing at the same location as your original pointer does, both will do the iteration
the big difference is. that using an unsigned integer for indexing can be smaller in memory, as the integer can be smaller than a pointer to a specific type
however
using a second pointer can make it easier for you to both read and write
when I say easier, I mean code readability and writablity
in modern compilers these things will be optimized out ofc, but I know you asked it, wether it would be faster on a non-optimized compiler
well the answer is: it depends -- and also: who knows
you are talking about implementation details here which is not really the concern of the language itself
if you have a target platform and a target compiler and you know your could should never be used on other targets
then you should profile your programs on these targets, otherwise you should stick to the standard (eg. C11 Standard)
helloc @IanC;
hello @PeterVaro, how are you doing? :)
@TeeSee who said that?
@TeeSee you can do that
I'm allright, how are you?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char **argv){
        char NOT_MALLOC[] = "Hello World!";

        char *MALLOC = malloc(strlen(NOT_MALLOC) + 1); //+1 => Null terminator

        strcpy(MALLOC, NOT_MALLOC);

        printf("Not Malloc: %s\n", NOT_MALLOC);
        printf("Malloc: %s\n", MALLOC);

        NOT_MALLOC[0] = 'Z';
        MALLOC[0] = 'Z';

        printf("Not Malloc: %s\n", NOT_MALLOC);
        printf("Malloc: %s\n", MALLOC);

        return 0;
@TeeSee
@PeterVaro I'm great! Just working my head on some big decisions I've to do soon :p
01:29
@IanC thanks I'll go play with that now.

@PeterVaro first time I've clicked on helloc. Nice dialogue on that page :P
@TeeSee take a look at Everything About Pointers -- it will clear things for you
@IanC really? what those decisions might be?
@PeterVaro about getting a place, instead of renting, been looking around and I spotted something in my reach, but would be kind of a big commitment
I'll figure out though, life is all about decisions right? :)
that is a big decision indeed!
and one of the difficult ones -- it is hard to change a property to a new one
also you cannot try it before
you cannot watch unboxing videos about it
and you cannot read reviews about it
it basically is gambling..
I'm a simple kind of person though, the place I'm looking is small, on an old building, but a place I could live in, and cheaper enough that I can afford it with my salary
I kind of took my chances already when I came to this town, I graduated as a chemical engineer and I don't work with that (which probably would give me some better finances) to live by the beach and have a lifestyle that suits me you know?
01:44
where are you from originally?
I'm Brazilian, born in Bahia, but I moved to Sao Paulo. Only after college I decided I wanted to live by the coast again @PeterVaro
@PeterVaro thanks for that resource. I always learn something new with every new resource!
so I made a test for working at the docks, and here I'm! My wage doesn't compare to what a engineer would get, but I'm happy here you know? I get to surf, sometimes do some different stuff, have a healthier life style than what I guess I'd have in other places
@TeeSee sorry I just dropped the code and didn't explain much. When you have an array, you're actually holding a pointer to a place in memory that holds the array items. So accessing them through indexing also work on memory allocated arrays as well, because you also have an address to a place in memory. The difference is mostly where to in memory those pointers point to
@TeeSee The array type (in this case char *) will indicate how the indexing will work: Each item will be of type char, which is 1 byte long. So MALLOC[0] points to byte "X", and MALLOC[1] points to "X+1". It you had an array declared like int *int_array = malloc(sizeof(int)*5); for example and tried to access int_array[0] it would point to "Y", and int_array[1] would point to "Y+4" considering int is 4 bytes long (it can be a different size there)
I might be creating more confusion on the matter, but if you read about pointers and play with them, you'll get the idea. Pointers are complicated when we first learn them, so just take your time :)
well guys, gotta go get some rest before work tomorrow
02:03
@IanC hey thanks sorry I got a phone call
@IanC and no worries because I am always happy to see code over explanations any time, I learn much more from seeing the code :)
@TeeSee cool! But it's always good to see what's going on behind the code, so be sure to read that article @PeterVaro sent you about pointers. I guess it covers most of what you need to know about them :)
good night guys, see you all later!
Goodnight!
user6820627
02:35
"Hello, World" I come from JS and I am learning C now. What compiler are you using? GCC?
@LearnHowToBeTransparent wow what a username
Using gcc
Welcome to C world!
user6820627
03:39
@TeeSee and what graphic lib do you use in C?
What kind of graphics?
@LearnHowToBeTransparent clang!
user6820627
03:55
@littlepootis are you compiling c to js?
oh lol no
user6820627
@littlepootis because i want to compile c to js.
05:45
JS guy asks about lib as first C question. Of course :P
06:34
@PeterVaro Thanks for the great answer. I suppose that testing the speed would be, as you said, the best choice. Though I hypothesize that the second pointer method would be faster than pointer arithmetic (assuming non-optimizing that is). Going to check and let you know
07:03
@PeterVaro It seems it would be (according to JDonner's answer in stackoverflow.com/questions/1234760/… ), but compilers now-a-days make [i] indexing more efficient. C is ever exciting :)
 
1 hour later…
08:06
@Byte I think your question is wrong- it is being used. Some people having harder time grasping it, so they do it via integers. But generally people who are very much comfortable with C pointers- seem to prefer using pointer types
 
3 hours later…
11:36
no real conclusions, no solutions what-so-ever, yet I think it is a lovely article for the morning which can bring the two sides together to agree on some basic things, that is, devided by a common syntax means there are (at least) two types of philosophies out there, and therefore there are (at least) two types of programmers out there.. we cannot agree on everything, deal with it :)
 
2 hours later…
13:48
@IanC strlen() is expecting NULL terminator already ;)
14:33
hey all
14:47
i mean
helloc all;
15:07
@Kamiccolo strlen does expect the NULL terminator indeed, but it doesn't count it as part of the string length. So if you try strlen on "abc" it will return 3, not 4 (which would include '\0')
The strlen() function calculates the length of the string s, excluding the terminating null byte
From the strlen man page
helloc all;
15:41
string literals have terminating nulls, yes
how did you determine that it doesn't?
Yup, it does. Ignore my last post.
1 message moved to Trash
char NOT_MALLOC[12] = "Hello World!"; would not have the terminating null char (if I counted the length correctly).
1 message moved to Trash
@IljaEverilä Yup, You're right. My mistake. Been away from the C for too long :(
Had to use that construct once, even. A synth of mine accepts SysEx patches and they contain fixed-length "strings".
16:12
0
Q: How to handle memory management via mmap properly?

WillyI'm trying to write my own malloc and free implementation for the sake of learning, with just mmap and munmap (since brk and sbrk are obsoletes). I've read a fair amount of documentation on the subject, but every example I see either use sbrk or doesn't explain very well how to handle large zones...

 
1 hour later…
17:23
question
say I have a uint8_t buffer, and I want to take buffer[0] and the next 3 bytes, and store them in float, how would you guys do that?
@Kevin not exactly what you're asking, but very much related port70.net/~nsz/c/c11/n1570.html#6.2.6.1p4
@IljaEverilä I think in that case there is a buffer overflow, not sure though
So basically if given that CHAR_BITS == 8 and sizeof(float) == 4 would memcpy(&the_float, &buffer[0], sizeof(the_float)); not work? Iirc this should be well defined, except that evaluating the resulting float could perhaps result in evaluating a trap representation...
17:38
@IljaEverilä the memcpy would behave as expected and copy the 4 bytes to the_float, but if the intention is to get a number that is a integer and then copy it to a float so it represents the same number it wouldn't work
@IanC you mean the fixed size char array initialisation from the string literal? That is well defined and will result in the array containing just the chars that fit. port70.net/~nsz/c/c11/n1570.html#6.7.9p14
@IljaEverilä Thanks! I thought there was a buffer overflow because printf was not trespassing the string with fixed array size, so I thought it was overwriting the next byte with the '\0'. Maybe I just got lucky that the next byte was a 0
Also it is somewhat obvious that copying the object representation of an integer to a float would not result in a float having the same numerical value...
Still on the subject of the char array, the resulting object is not a "string" of course, as it does not have the terminating \0 and so it is undefined behaviour using it as such :)
say you have (uint8_y[4]){ 0x00, 0x00, 0xAA, 0x00 }, and you want to basically copy those bytes into a float a, similar to doing float a = 0x0000AA00
Copying the bytes is not the same as initializing the float from 0x0000AA00 (an integer).
17:45
-_-
i get that
So, are you trying to first create an integer from those bytes and then use the numerical value as a float, or...?
memcpy(&the_float, &buffer[0], sizeof(the_float)); would work, but is that the best way
It's legal, except for the fact that you might create a trap.
right
which is why i'm questioning whether or not its the right way
i suppose just add in bounds checking?
Not 100% sure, but perhaps you could twiddle with unions :P
17:48
or just assume? ^_^
Having a union of array and float, initializing it as the array, then accessing as the float
not sure how read/maintain/able that would be
eh, good point
the uint8_t array is actually like 200 bytes
You'd still have to move those bytes from your buffer though :)
right
@Kevin on the subject of unions and type-punning, see the footnote here: port70.net/~nsz/c/c11/n1570.html#note95
18:34
@IljaEverilä A union is the same as your first suggestion, memcpy
@Kevin You're asking for the 'correct' method to do something, but you can't specify exactly what is it that you wish to be done 'correctly'. What is the source/origin of this 'thing'? Is the thing an integer? A float? What type of representation?
18:48
@DrorK. that it is. And you'd still have to memcpy the bytes anyway through one member or the other...
@IljaEverilä By his demo above I'm starting to think that his source is actually a serialized integer, which means he first needs to deserialize his integer before converting/assigning it to a float. But unfortunately he's not here and I haev other things to attend
Yeah, from the later replies one would think so
Not to mention given their example it'd be big-endian (looking at the buffer)
So there's that hassle too
Speaking of hassles, receiving a design spec from graphic designers with margins etc. that seem to be pulled from some pseudo random sequence sucks
Why can't they design consistently
19:22
Heh, I know what you mean
Very few programs exist for such "design" with patterns... there's this one of Adobe
InDesign I think
19:33
1
Q: performance counters values return zero using papi attach

ShujaI am trying to read the hardware performance counter using PAPI and I have written the following code: #include <stdio.h> #include <stdlib.h> #include "papi.h" /* This needs to be included every time you use PAPI */ #include <unistd.h> #define NUM_EVENTS 2 #define ERROR_RETURN(retval) { fprintf...

trying to do exactly this: memcpy(&the_float, &buffer[0], sizeof(the_float)); but make sure its safe
sorry about the pings
@Kevin The function memcpy is obviously well-defined. The questions are: 1. what is your source, 2.what is your destination, 3. what do you intend to do with your destination
It's safe if the buffer is large enough and the object representation doesn't contain a trap
Actually, what Dror K. said.
If you don't understand the questions feel free to ask about them. I'll bbs
destination is a float, source is a unsigned char array
intend to copy 4 bytes from the array to a single float
19:39
Where did those bytes in the buffer array originate from?
Who is responsible for the source? How it's being 'created'?
19:56
originate from an external source, of which i'm also in control of
its a static buffer, created by the module its in
static uint8_t buffer[1024]
for instance
Let's try to simply it. Could you write an example code which generates such an array?
shouldn't 00 00 AA 00 be copied to data?
20:13
@Kevin you commented out the call
sorry, refresh
was playing around with it
@Kevin What's relevant is- how the source is being generated. This example doesn't generate a value- what is the value? Where it's coming from? How it's being generated?
its an example, so its being generated here, isn't it? : uint8_t buff[4] = {0x00, 0x00, 0xAA, 0x00};
you were using %f but the value was a denormal float, with a 0 exponent and nonzero mantissa, putting the number < 1e-34
@Kevin It's simply initializing the array with 4 values- where these values are coming from? What are their representation?
20:19
nothing, it could be anything, i just want to take those four bytes, and copy those bytes into a float
@doug65536 float a = 0x0000AA00; printf("%f", a); doesn't print < 1e-34
obviously
it prints 43,520
.00 or whatever
@Kevin Which means that what you ask for, and what you expect, are two different things
so if i copy four bytes from an array, into a float, why isn't it the same?
you realize that float and 4 bytes are two totally different representations, right?
because floating point values are not integers
20:22
yes.
@Kevin "Copying bytes" means copying the object representation of something. This is possible only when you know what the representation is, and that it actually makes sense to the target type
The IEEE Standard for Floating-Point Arithmetic (IEEE 754) is a technical standard for floating-point computation established in 1985 by the Institute of Electrical and Electronics Engineers (IEEE). The standard addressed many problems found in the diverse floating point implementations that made them difficult to use reliably and portably. Many hardware floating point units now use the IEEE 754 standard. The standard defines: arithmetic formats: sets of binary and decimal floating-point data, which consist of finite numbers (including signed zeros and subnormal numbers), infinities, and special...
yep, i've read that
@Kevin If you believe that your "source" is a serialized integer, then you need to de-serialize your source, and then assign the value to whatever type you wish
the source is de-serialized, isn't it. because a buffer is typically just a byte array
and you have to seralize it into a type?
20:25
Unfortunately it's something that you should be able to tell us, we don't know what your source is
lol
the source is an array of bytes
it can have whatever
Then the answer is no
and indicies 0 through 3 contains four bytes, which needs to be "copied" or whatever into a float.
its a packet
It's impossible to convert an array of bytes, which may or may not contain whatever, to a float, or to any type
The "whatever" needs to be known
the array of bytes, contains bytes
20:27
What do these bytes represent?
represents a temperature in Fahrenheit
bytes could be anything. pixel colors, audio signals, floats, doubles, shorts, chars, ints, unsigned ints, bitfields, etc
That's a bit high-level. Do the bytes represent a serialization of an integer, a float or what? In other words at some point in the past did some other piece of code do the opposite, i.e. memcpy the object representation of a float in to a buffer?
even if you knew something was text, it might be UTF-8, UTF-16, some codepage, plain ascii, etc
20:30
C# program:
float a = 43520.0f;
byte[4] buff = BitConverter.GetBytes(a);
serialPort.Write(buff, 0, 4);
Now we're getting somewhere :)
Haha!
We broke him!
Now we haev the source!
so thats going to send 4 bytes to my device which is the device connected to the serial port
Now the remaining question is: what endianness does the machine sending the float representation have? What about the receiver?
GetBytes is machine dependent.
yep, so just assume they are the same. lol
20:33
assume, lol
because literally the source is made up
So we didn't break him :)
haha
And I hope your made up source value is a valid object representation of a float.
Or you'll be having a bad time, mmkay
the source would in fact work
20:34
we don't need those bulletproof vests. just assume we won't get shot
I say, we should brute force him, let's do what the Orcas do... let's take shifts
a could be any valid ieee float value.
And that'd work, if you're really handling the received buffer from your C# thingie and the endianness is the same. But you claim that you're (testing?) using made up buffers, so they may or may not contain a valid float.
@Kevin I suggest you should look up "representations" and examples of such, maybe that way you'll understand the difference between a representation and a value
okay, so there is another piece of hardware in between the PC and my device, on the serial port line, which makes sure all bytes going from the PC to my device are little endianness
20:37
That's interesting. How does it do that?
proprietary hardware that i have no control over
connected directly to the processor of the PC :D
i must honestly be retarded
sorry
You're so close to grasping the concept, don't give up just yet.
so you know what GetBytes(float) does
Let's see, say I create a totally new type, ilja_t. I then declare that sizeof(ilja_t) is for example 4 bytes and that the only valid bit patterns that represent an ilja_t are { 0x00, 0x00, 0x00, 0x01 } and { 0x00, 0x00, 0xA0, 0x00 }.
Now someone using ilja_t and testing with it tries to do the memcpy thingie, but uses a bit pattern of { 0x10, 0x10, 0x10, 0x10 }. That'd clearly violate the constraints of that type.
right
20:42
It's basically the same with floats. Not all bit patterns are representations of a float.
okay.
And that's why the source matters etc.
okay
then is it UB if you give it an invalid bit pattern?
So if you're handling the received buffer sent by your C# program, and the they agree on endianness, you should be fine.
It is UB if you evaluate the float that has been assigned the invalid representation.
i know i should be fine, thats the point i'm trying to make, is i know the data coming over is valid floats
20:45
Aye, you should be fine :D
i want to cry
i see it
21:35
thanks all :(
 
1 hour later…
22:59
@IljaEverilä what bit pattern is not a float?
every possible bit pattern is a float
all the ones with maximum exponent and nonzero mantissa are NaN, bit they are all "valid"
all the values with zero exponent and nonzero mantissa are denormal. all valid
negative zero is valid
max value exponent with zero mantissa is infinity
so what's left? that covers every possible bitpattern
@Kevin there is no such thing as an invalid float. all possible values are valid floats

« first day (935 days earlier)      last day (2921 days later) »