#code

56 messages · Page 1 of 1 (latest)

hidden nymph
#

I was working on structs but I got a problem. So, I removed some parts and my code is below. Why don't I get any output from that code?


typedef struct Song {
    char name[25];
    int duration;
    struct Song* next_alpha;
    struct Song* next_chrono;
    struct Song* next_duration;
    struct Song* next_random;
} Song;
typedef Song* SongPtr;

typedef struct SongList {
    int size;
    SongPtr head_alpha;
    SongPtr head_chrono;
    SongPtr head_duration;
    SongPtr head_random;
} SongList;
typedef SongList* SongListPtr;

void insert(SongList**, char[], int);

int main() {
    SongListPtr songs; //Struct pointer to reach linked lists.
    songs->size = 0;
    songs->head_alpha = NULL;
    songs->head_chrono = NULL;
    songs->head_duration = NULL;
    songs->head_random = NULL;

    char name[25] = "";
    int minute = 0;
    int second = 0;
    insert(&songs, name, 60*minute+second);
    
    printf("Good Job");
    return 0;
}

void insert(SongListPtr* songs, char name[], int duration) {
    printf("insert");
}```
drifting cliffBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

honest quest
#

You have some UB here. So you end up doing

SongList *songs;
songs->size = 0;```. This pointer does not point anywhere. You should declare a SongList object, and then take a pointer to it
hidden nymph
#

i dont get any compilation error

drifting cliffBOT
#
Undefined Behavior

Undefined behavior (UB) occurs when you violate rules specified by the language. For example: Reading uninitialized memory, performing out-of-bounds memory access, or using an object after it no longer exists.

Example: Indeterminate Value
int i; // uninitialized
while(i < 10) {
    printf("%d\n", i++);
}
Example: Out-of-Bounds Access
int arr[10];
for(int i = 0; i < 20; i++) {
    arr[i] = 0;
}
Why it Matters

Compilers often do not give warnings or errors about UB and its existence in your code can cause surprising, unpredictable, and buggy behavior.

honest quest
#

As soon as you hit UB, all bets are off. Your program is probably crashing before it hits the end of main

hidden nymph
#

but how can i initiliaze the that struct pointer

honest quest
#

As I said, initialize a SongList object, and then take a pointer to it

#

That way the pointer is actually pointing to a valid object

hidden nymph
honest quest
#

You initialize it

SongList songs;
SongList *songs_ptr = &songs```
hidden nymph
#

oh right..

#

i just created a pointer but not struct itself ..

#

thank you

honest quest
#

Yep

hidden nymph
#

im doing a file reading operation and my input file is with 7 lines, but it accesses insert function 8 times. i think it must be about end of file issue. how can i prevent it?

#

oh even 9 times..

honest quest
#

You’re sure there aren’t extra new lines at the end?

hidden nymph
lofty lynx
heavy spearBOT
#
Compiler Output
/app/example.c:26:17: runtime error: member access within null pointer of type 'struct SongList'
AddressSanitizer:DEADLYSIGNAL
=================================================================
==1==ERROR: AddressSanitizer: SEGV on unknown address 0x000000000000 (pc 0x0000004012fa bp 0x7ffd327905e0 sp 0x7ffd32790520 T0)
==1==The signal is caused by a WRITE memory access.
==1==Hint: address points to the zero page.
    #0 0x4012fa in main /app/example.c:26
    #1 0x70d8d2029d8f  (/lib/x86_64-linux-gnu/libc.so.6+0x29d8f) (BuildId: 490fef8403240c91833978d494d39e537409b92e)
    #2 0x70d8d2029e3f in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x29e3f) (BuildId: 490fef8403240c91833978d494d39e537409b92e)
    #3 0x401134 in _start (/app/output.s+0x401134) (BuildId: 7fb41534a16957fcad5f1c4539f3f82d6d34e08b)

AddressSanitizer can not provide additional info.
SUMMARY: AddressSanitizer: SEGV /app/example.c:26 in main
==1==ABORTING
hidden nymph
lofty lynx
# heavy spear

@hidden nymph That's how you get a compiler error for such things

lofty lynx
hidden nymph
#

and thanks much!

lofty lynx
# hidden nymph is it only possible with that discord bot?

-fsanitize=address is a so called sanitizer and it's simply a command line option you can add to gcc.
I'm not sure if it really works on Windows, but on Linux it works out of the box
address is checking for memory errors and undefined is testing for undefined behaviour.
All of these checks are runtime checks so you can only see them come in action when something goes wrong during execution (basically they just construct a lot of extra code around your code to check for violations of their respective area). Sanitizers aren't perfect, they won't catch anything, especially rare bugs are hard to catch.
You don't want to use sanitizers for release versions since the extra code obviously slows down overall performance.
Here's an explanation from the bot:

#

!asan

drifting cliffBOT
# lofty lynx !asan
Address Sanitizer

Memory errors in C and C++ are easy to make and they can be very hard to debug because they can manifest far from their source. Address sanitizer is a runtime checker that identifies memory errors at their source and makes debugging much simpler. Address sanitizer is available for gcc/clang on linux and msvc on windows. To use it simply pass -fsanitize=address to the compiler.

Note: Make sure to turn on debug symbols with -g for gcc/clang and -Zi for msvc.

ce Example

How to read sanitizer output

The first few lines tell you the problem, heap-use-after-free, due to performing a READ of size 4, at example.c line 7 (from the first line of the stack trace).

==1==ERROR: AddressSanitizer: heap-use-after-free on address ....
READ of size 4 at 0x602000000010 thread T0
    #0 0x40120f in main /app/example.c:7
    #1 0x7fda58629d8f  (...)
    #2 0x7fda58629e3f in __libc_start_main (...)
    #3 0x4010b4 in _start (...)

Additional information is also included such as where the allocation was performed and where the allocation was freed.

See Also
  • Other sanitizers exist and can be similarly helpful, including ubsan, threadsan, and memorysan.
hidden nymph
drifting cliffBOT
#

@hidden nymph Has your question been resolved? If so, type !solved :)

hidden nymph
#

@lofty lynx can i ask a more question

drifting cliffBOT
hidden nymph
# lofty lynx !ask

i have an input file with 7 lines but that while block executes 8 times. how can i fix it?

#

i made sure that the input file is actually 7 lines

#

do i have a problem with end of file char or etc

lofty lynx
#

Or wait

#

Does that make sense?

#

Not sure actually 🐒

stoic elk
#

files arent null terminated

hidden nymph
#

feof function is according to end of file char as i know

honest quest
#

All I can think of is fscanf is failing so it leaves it unread, then the next pass around it succeeds

lofty lynx
#

Yeah, so that's not it, but maybe there's something else in the line, like a space or a newline that isn't consumed?

stoic elk
#

the EOF must be "read"

lofty lynx
#

ah

stoic elk
#

EOF isnt triggered until it tries to read

lofty lynx
hidden nymph
stoic elk
#

or just check for EOF after the fscanf

lofty lynx
#

or wait, does it fix it? Or is the EOF still not read even with fgets?

#

I work so rarely with files...

vagrant pike
#

@hidden nymph I think adding extra check for feof inside while loop after fscanf will do the work