Quantcast
Channel: User Lundin - Stack Overflow
Browsing latest articles
Browse All 322 View Live

Comment by Lundin on Digit after null terminator in C?

@IanAbbott Ah yeah that seems to be the case, I must be mixing it up with hex escape sequences.

View Article



Comment by Lundin on I am trying to understand why is there a "const char...

@Atti Casts typically don't cause any run-time overhead. But *(char*)src += count; is far less readable code than what you have.

View Article

Answer by Lundin for how to arrest sudden spike value occurs occasionally in...

Note that a digital average filter works exactly like a RC filter in hardware: it doesn't eliminate anything, just round it down. If the spikes are part of the expected input, you can use an average...

View Article

Answer by Lundin for How to create an underflow in this situation?

uint8_t a;uint8_t b;...a=b*39216/100000In this expression, b is integer promoted (Implicit type promotion rules) to type int. Since it started as an unsigned type, all values it can hold will fit...

View Article

Answer by Lundin for Arrays and pointers in C using Malloc

But I'm confused about how pointer arrays workThere's no such thing in your code. There are arrays and there are pointers. In many situations, an array name whenever used in an expression "decays" into...

View Article


Answer by Lundin for Pointer Arrays and Addresses

printf("Address stored in Pointer: %p\n", array_ptr);This is the address of your array, not where the first item points. Should be *array_ptr or array_ptr[0].

View Article

Comment by Lundin on Design of a method and data structures to process...

@mgfernan Yes and that is made possible by switching to a union. You still have to ensure that the incoming raw_data is aligned appropriately though.

View Article

Answer by Lundin for Does the C compiler have to short-circuit && and ||...

This feels like it is equivalent and removes the branches.It is not. You execute the right operands of the && regardless of what the previous results were. None of the mainstream x86 compilers...

View Article


Answer by Lundin for Unexpected value when dereferencing pointer in C

char may be either signed or unsigned depending on compiler. Is char signed or unsigned by default? In this case it appears to be signed.On mainstream computers, signed char can only hold values -128...

View Article


Answer by Lundin for Constant memory gets changed, it should give error, but...

I have executed this code and I think this should give errorIt should give a diagnostic message. The C standard doesn't speak of warnings and errors as such. What must a C compiler do when it finds an...

View Article

Answer by Lundin for Does gcc compiled executable LSB imply "Least...

With GCC, big endian machines lay out the bits big end first and little endian machines lay out the bits little end first.Not defined by the C standard and probably not even defined by the gcc...

View Article

Answer by Lundin for C Macro Expansion Again

I don't see how headerSymbolImages makes any sense as written, if the initializers are structs declared elsewhere. If it was valid you'd end up copying structs by value and end up with duplicate data....

View Article

Answer by Lundin for Addresses and arrays in C

When should I use one or the other, or are they equivalent ?"element address using pointers arr+i" This will utilize pointer arithmetic, meaning that the compiler automatically looks up the element...

View Article


Answer by Lundin for Are bitmasks mandatory for unsigned conversions?

Regarding the trusted sources/C standard part:How do we know that a conversion takes place?You didn't force an explicit conversion with a cast in your example, you just wrote uint8_t small_value =...

View Article

Answer by Lundin for How to perform a MCU reset after a specific hardfault?

The normal way to force a MCU reset is to write an invalid sequence to the watchdog register.For example if you normally refresh the watchdog with WDT.CLEAR = magic_number; then simply write an invalid...

View Article


Answer by Lundin for Code vulnerability to buffer overflow attack

Apart from the exact number 40 leading to a non-null terminated string, this program also accepts -1 etc as input.read in turn has a parameter of unsigned size_t, so if I enter -1 then len will get...

View Article

Answer by Lundin for Understanding pointer definition in C

Pointers are equivalent to address most of the time. But in embedded systems they are usually local addresses inside the microcontroller itself. Here you are dealing with some sort of external...

View Article


Answer by Lundin for one question about auto var in C wen I learn the...

The code with int i; as automatic storage invokes undefined behavior during the second time you call the function, because the initialization code of i is not executed at the point where state is set...

View Article

Answer by Lundin for GCC -Woverride-init warning with anonymous structs in a...

Bit-fields are problematic because they are non-portable and hard to predict. Plus in this case they need to introduce padding to the struct. __attribute__((packed)) doesn't fix that, the 32 bit type...

View Article

Answer by Lundin for For loop: signed / unsigned integer expression WARNING

It doesn't look nice and therefore not very well readableNah it's fine. Explicit casts are easy to read. It's probably a worse offense to readability to use a verbose name for the loop iterator, you...

View Article

Comment by Lundin on How to evaluate –2147483647–1U in C (32 bit program...

(Those amused with obfuscated code might notice that -2147483647 +- 1U works just as fine, because the - is a stand-alone operator and doesn't belong with the 1U.)

View Article


Answer by Lundin for Initializing an array of char - Is the string used to...

The initialization will copy these characters to the array terminated by the NUL characterThat's a little bit misleading because it implies that the array is copied in a way similar to strcpy, actively...

View Article


Answer by Lundin for Non-const global value as const, C

Your requirements are contradicting:I want to define a non constant global value, modify it in a function and use it as constant in other functions.If it must be "global" then that in itself implies...

View Article

Answer by Lundin for Difference between node (or ECU) id and frame id for can...

When speaking of any form of data communication, it is important to be aware of the OSI model. Normally in embedded systems, it can be simplified to 3 layers: physical, data link and application.CAN...

View Article

Answer by Lundin for How to assign anonymous VLA to pointer?

Given the "must be allocated on the stack and array must be small", one obvious solution would be this:struct Foo { int array [MAX]; size_t size;};That is, always allocate some 256 bytes but keep track...

View Article


Comment by Lundin on Complicated declarations in C

@Peter-ReinstateMonica Yep, the correct solution to whatever this was supposed to solve is very likely to pass along the size as a parameter or to wrap size + array inside a struct.

View Article

Answer by Lundin for Finding the size of a file with C23 embed

It should work given that #embed expands to a valid initialization list.sizeof expressions are valid integer constant expressions as long as they don't use a VLA operand, as per C23 N3096 6.6:An...

View Article

Comment by Lundin on two different function pointer calls with the same value...

There's no need to "turn up some warning levels" on a conforming C compiler. int(*(*x)())[20] = &func; is simply invalid C and so the compiler must give a diagnostic message or it is non-conforming...

View Article

Comment by Lundin on Assign one struct variable to another of the same type...

Also C17 6.2.7 "Two types have compatible type if their types are the same." That applies here. Then the same chapter goes on about various intricate rules for when two structs with different types are...

View Article



Comment by Lundin on Moving down the isr_vector in stm32 with GCC

Wouldn't it be smarter to move your custom variables instead of insisting on "because of reasons-...". There cannot possibly be anything more important in the MCU than the vector table, so I never get...

View Article

Comment by Lundin on Undefined behavior with pointer casts in C99 and MISRA...

@Andrew The OP changed it. And for all we know, the OP might be following MISRA C:2012 (with or without the TC:s). Personally I still maintain lots of projects where I'm stuck with MISRA C:2004 even :/

View Article

Answer by Lundin for How to add all object files of one single directory on...

Something like this might work:Create a custom section in the linker script. Lets say it is called "my_memory".In the place where your compiler would expect you to add non-standard C stuff to place a...

View Article

Answer by Lundin for Undefined behavior with pointer casts in C99 and MISRA...

The main reasons not to allow such casts are misalignment and "strict aliasing" indeed, both invoking undefined behavior. We can tell since on top of rule 11.3, there's the cryptic noteC11 [Undefined...

View Article


Comment by Lundin on Why did I get "incompatible pointer to integer ..."warning?

RawAudio = (unsigned char*)bootup; ... *RawAudio = something will invoke undefined behavior as well.

View Article

Comment by Lundin on C-project include headers which don't exist but there do...

Eclipse is pretty dumb as programming IDEs go. If you use subdirectories in your project, you must manually edit project settings and add them there. Every other IDE on the market understands that if...

View Article

Comment by Lundin on optimization of STRCMP

Also, compilers may not be able to optimize a function containing inline assembly/intrinsics, in case the optimizer is not "assembler aware". It might then regard such as "unknown side effects"....

View Article


Comment by Lundin on Bug only manifests on FreeBSD, but works perfectly fine...

I'm not following how D( if (remain) { ... } ) could be valid C code to begin with...?

View Article


Answer by Lundin for Understanding how initialization with initializer list...

arr[0] = 1; is not initialization but (run-time) assignment. It may or may not have an effect on the generated machine code, but language-wise initialization and assignment are different concepts (and...

View Article
Browsing latest articles
Browse All 322 View Live




Latest Images