[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y ] [Search | Free Show | Home]

C General

This is a blue board which means that it's for everybody (Safe For Work content only). If you see any adult content, please report it.

Thread replies: 86
Thread images: 8

File: 1485211379280.png (107KB, 792x1023px) Image search: [Google]
1485211379280.png
107KB, 792x1023px
>>
malloc()
>>
>>58628609
malloc.c:1:1: warning: return type defaults to ‘int’ [-Wimplicit-int]
malloc()
^~~~~~
malloc.c:1:1: warning: conflicting types for built-in function ‘malloc’
malloc.c: In function ‘malloc’:
malloc.c:1:1: error: expected ‘{’ at end of input
>>
>>58628609
>guys i don't understand why this smoothie maker isn't cutting down trees, i mean i'm trying really hard but it's just not working, oh dear!
>>
>>58628609
Inferior to calloc() in every way that matters. Prove me wrong.
>>
If you have to give 5 exercices/challenges to do in C what will they be ?

The first one should be easy and every other should be increasingly more difficult.
>>
>>58628939
calloc(SIZE_MAX, SIZE_MAX)
>>
>>58629141
Challenge 1: Create a timekeeping system that helps you schedule activities in your personal life.
Challenge 2: Create a system that emails your empty/embarrassing personal schedule to your mother, telling her that you're still a virgin and that she can forget about having grandchildren.
>>
Do you guys also feel the need to defend C in conversations with pythonfags irl or is it just me
>>
>>58629212
I don't talk to Pythoncucks.
>>
>>58629212
every time i come across a computer with python installed, i uninstall it, and replace the bash config for python with gcc
>>
>>58629212
>speaking to python user
I hisss at them
>>
I'm new to C. When should you allocate memory from the heap? I Don't understand when you should.
>>
>>58629212
I cannot believe anyone seriously codes in python, it's like coding while sitting in a really uncomfortable contorted position following rituals every time you want to do something
>>
>>58629334
>When should you allocate memory from the heap
Basically whenever you need to allocate data and you aren't sure how much of it you need and you need the data to be persistent across calls.

If you allocate it on the stack, it will be invalid the moment your scope moves down.

Advantage of stack allocation:
- Fast, usually free allocation and deallocation
Disadvantage of stack allocation:
- Limited life time of the scope of your function
- Limited stack space

Advantages of heap allocation:
- Persistent across calls
- Usually can allocate to system limits
Disadvantages of heap allocation:
- Manual memory management (need to deallocate yourself)
- Allocation not free, but usually cheap
- Deallocation/freeing not free either and it is expensive.
>>
>>58629182
is a timekeeping system supposed to be easy ?

Maybe i should just give up
>>
File: 1454190679953.png (268KB, 800x800px) Image search: [Google]
1454190679953.png
268KB, 800x800px
>>58629212
>tfw all of the tools for the only good linux distribution are written in python
>>
File: 1476324034872.png (509KB, 768x576px) Image search: [Google]
1476324034872.png
509KB, 768x576px
>>>58629212
>Do you guys also feel the need to defend C in conversations with pythonfags
No.

I may like C but I'm not THAT autistic to defend it like my mother or something like that, specially against a useful programming language such as Python.
>>
>>58629547
>- Manual memory management (need to deallocate yourself)

but how often does this happen. just have a linter check for you
>>
File: imgres.png (6KB, 247x204px) Image search: [Google]
imgres.png
6KB, 247x204px
Bump for real emu hours
>>
>>58629212
>>58629241
>>58629248
>>58629527
I use both C and Python. They are my 2 favorite languages. You can do literally everything in those 2.
C perfect for low level, Python perfect for high level.
Want to make a fast program? Pick C.
Want to make a program fast? Pick Python.
No, this is not a troll post. Hate me.
>>
>>58628364
I am going to quote from K&R c which got me scared.
>C provides no operators to deal directly with composite objects such as character strings, sets, lists, arrays.
Implying i can't go
 
String a = "HI";
String b = "BYE";
if(a==b)


>There is no heap or garbage collection
WTF so what if I create object and i remove the reference of it. Will that object stay in my computer forever?
MyObject obj = new MyObject();
obj = null;

That newly created object will not be cleared automatically?

>inb4 I am from Java background. Follow the corresponding syntax in C and question are in context of C language.
>>
>>58632824
I hope I'm not replying to copypasta here, but
you can indeed not compare two "strings" directly and get the result you'd expect in java.
there are functions for that in string.h (strcmp(), for instance)

also, ignoring the fact that "Objects" don't exist in C, any data you allocate with malloc() or similar stays where it is until you free() it.
the point of malloc() is to prevent data from disappearing. it's your job to clean it up afterwards.
>>
>>58632824
There is no "string" type in C, only character arrays. To manipulate them, you have to use functions from string.h.
#include <string.h>

char *a = "wew";
char *b = "lad"
if (!strcmp(a, b)) {
...
}


>Will that object stay in my computer forever?
Unless you call free(), the memory will remain taking up space memory until the program ends, at least on most operating systems.
foo tmp = malloc(sizeof(foo));
...
free(tmp);
>>
>>58632915
So while my program is up and I forgot to call free, then that object will live on heap forever until program halts?
I hope heap is a higher abstract concept and is applicable here and
>>
>>58632957
>So while my program is up and I forgot to call free, then that object will live on heap forever until program halts?
Exactly.
>>
>>58628939
>Inferior to calloc() in every way that matters

zeroing out allocated space is not always necessary or desirable.
>>
Any tip on porting gcc to TempleOS?
>>
>>58634188
use the superior HolyC programming language that TempleOS provides you with instead.
>>
>>58629248
>top kek
>>
>>58632382
Most people feel like this. They have a task or goal in mind, and select the tool best suited to complete it.

It's unemployed retards who know nothing about deadlines and cost restraints who get religious about programming languages.
>>
>>58634271
That's not true. Math cares, technology doesn't.
>>
>>58632957
Well in C++ you have shit like smart pointers that delocate memory when they go out of scope.
>>
File: hisss.gif (698KB, 555x666px) Image search: [Google]
hisss.gif
698KB, 555x666px
>>58629294
>>
>>58635411
Get a real book, on paper. You won't read a PDF but you'll read it if it's there next to you. Otherwise, I cannot help you; I have been refreshing this thread for hours waiting for someone to give you a real reply.
>>
>>58629733
clever marie rose poster
>>
>>58636482
>You won't read a PDF
It is nothing more but a personal issue, anon.
It's a legit problem, though.
>>
>>58628364
Started C two days ago. It will be my first language and one that I plan to excel in if able. Excelsior and good luck to all of you.
>>
>>58636482
I agree with this. I've tried learning using book pdfs but I didn't make real progress until I got the physical book. Whatever works for you though.
>>
Is it considered good practice to dynamically allocate memory for all the structures one uses?
Like
struct string * string = string_new("string")

Instead of
struct string string = string_new("string")
>>
Just started reading C Programming Language, coming from a webdev background, so far so good but as a total newb when it comes to system programming, can you guys tell me whats the TANGIBLE discrepancy between C and C++, where should I use one another and such?
Asking it because I was pretty unsure wether I'd star with C or C++ as my "first" decent language.
>>
>>58637324
C - Clean and "pure"
C++ - C with classes and 3000 other features crammed into the language to turn it into a bloated horrifying frankenstein mess with 9 arms and 1 leg and 4 heads
>>
>>58637355
Not very tangible of a comment anon, like I genuinely see lots of people using C++ why would they use something "that bad" (as you said)
>>
>>58636864
I feel the same way. I spent money on the actual book so I feel motivated to use it. Also it is easier on the eyes.
>>
>>58637324
Start with C. It's small and, once you understand it, simple.
C++ is an eldritch amalgamation of stuff upon stuff added to the language over the years. For every tutorial on how to X you'll find two others stating that method is deprecated and you should use something else. Hell, C++ grammar itself is so convoluted that creating a program which would answer "is this string valid C++" is akin to writing a compiler, only without the actual asm-generator.

Don't get me wrong, C++ is powerful, has a lot of useful stuff and a lot of companies build awesome software using it. But for a beginner language, fuck no. Even for an intermediate language I'd say fuck no.
>>
>>58637398
C++ was originally designed to be C with classes.
It has since exploded into a "do everything" language. It's still very powerful and very fast, but it also has a lot of drawbacks because of that feature creep.

C is just C. It hasn't changed in years, and the only things that have changed over the last two decades have been compiler and memory optimizations. It is "pure and simple".
>>
>>58629182
lol'd
thanks for the suggestion anon
I have a project to do now.

Anyone coding on android?
>>
>>58637324

Always start with C.

C++ contains C as subset (OK, there are some tiny exceptions, but let's not argue about details here..).

Also you can learn C pretty fast and C++ is really a big language.
>>
>>58637409
>easier on the eyes
very much this, reading pdfs for too long hurts my eyes
>>
>>58636482
>You won't read a PDF
I'm a real lazy piece of shit but even I could read a pdf of a programming book.
>>
File: fixed.png (1MB, 1250x678px) Image search: [Google]
fixed.png
1MB, 1250x678px
>>
>segmentation fault general
>>
File: 1354403890278.png (594KB, 788x1024px) Image search: [Google]
1354403890278.png
594KB, 788x1024px
>>
Any good book besides the K&R reference book ?
>>
How do you link static libraries?
>>
File: whatweretheythinking.jpg (143KB, 965x719px) Image search: [Google]
whatweretheythinking.jpg
143KB, 965x719px
>doing stuff in graphics.h

am i stupid or just bored

i dont mind either way, i guess i'm learning something
>>
>>58635411
harvard cs50
>>
no smart pointers
no thanks

there is literally no reason C shouldn't have RAII
>>
>>58638013
read up how to into C11 from the internet

then read The Linux Programming Interface so you can apply what you've learned
>>
>>58638155
Easy to implement, stable ABI.
Though I agree that C should have some kind of module system where you would program library using platform specific modules. For example directory walking or dynamic allocation. Some platform don't support them. You should program your library to have minimal module dependency. But C should make it easy to develop for dfferent hardware and systems. Easy to implement is nice that they should keep but having standard library for system that supports them should also be provided for better portability.
>>
Can I easily nab frames from a USB webcam in C?

Or should I just learn python for real toy projects, and stick to real basic fizzbuzz shit to learn C in.

This is on a raspberry pi and I already know C# / Java
>>
>>58638220
have fun finding a platform that can run C programs and cannot run C++ programs

there is no reason C cannot be a lightweight C++
>>
>>58638232
I don't know the specifics, but you should be able to read from /dev/video0 http://stackoverflow.com/questions/31058571/reading-camera-input-from-dev-video0-in-python-or-c
>Or should I just learn python for real toy projects, and stick to real basic fizzbuzz shit to learn C in.
Saying that here is like going into someone's house, fucking their wife and taking a big steamy shit in the middle of their living room.
>>
>>58638248
In fact, there is no reason C cannot be full-blown C++

just use C++
>>
>>58638322
Forgot to mention that your cam won't necessarily be mounted in /dev/video0, although it should default to that if you have only one camera connected. You should definitely add an option to select which device to read from though.
>>
>>58638248
I have programed for pic18 system where the word size is 16 and 8 bits for register. I would rather develop in C than in assembly. In that kind of system you cannot dynamically allocate memory and there are other restrictions. It's not even starndard C really but C compiler is the easiest to implement to that kind of system?

I agree that C should be lighweiht c++ but it should keep stable ABI for other languages to have FFI. It should have some kind of base that is easy to implement that should work in every platform. Then the standard should expand on that base and define extended standards to for platform specific features.

C standard should have be able to list files in directories that supports them.
C standard should have well implemented basic allocators for system that support dynamic allocation.
C standard should have well defined parallel data structures and functions for platforms that supports them.

C does nothing well really but C++ just shits on it while doing nothing right either.
>>
>>58638322
Oh nice, I didn't see anything that encouraging when I searched.

I think the hits were all on weird libraries because people wanted live streaming video instead of just capturing a few stills.

>Saying that here is like going into someone's house...

Don't worry m8, I'm learning C anyway. I just know some projects aren't feasible in it for a noob. Programming is my day job I don't wanna spend months tinkering with creating the perfect C library for webcam capture on my off time too. I'd rather learn C itself.
>>
>>58638417
>C standard should have be able to list files in directories that supports them.
Why not.
>C standard should have well implemented basic allocators for system that support dynamic allocation.
What are you talking about? Malloc isn't dynamic allocation?
>C standard should have well defined parallel data structures and functions for platforms that supports them.
No.
>>
>>58638458
>No.

yeah my dude, fuck multithreading amirite?
>>
>>58638458
>parallel data structures and functions for platforms that supports them.
>no
The C language already defines them in c11 but it's optional. What I mean it the language should have defined them when they parallel system appeared. The C language should define standard that others could implement for that feature. Insead we have posix threads and windows threads.
C could be easy to implement in the base but it could also define interface for extended features.
>>
>>58638523
Ho you were just talking about a threads API
>>
>>58638417
>C does nothing well really
What?
C is the best language at what C does.
Why do you think it has been so popular?
Why do you think people have been trying to kill it for years, and failing miserably?
>>
>>58638553
>>58638563
My point was that it should define interface to all features that modern lanaguages do, even if the implementation is opional for the that specific system and compiler.

Instead C standard is minimalistic and not really useful (as itself) for cross platform usage. You depend on stuff like posix standarts and windows api instead of having the stuff in C language. It would make the C standard bigger but all the extended stuff would be optional for the systems that supports them.

You would write C libraries / programs for those specific features. Now we have multiple libraries that do badly intefrace for common allocators, directory and file manupalition, threads.

I'm just rambling and this will never amount to anything but I hate how bad the C standard is.
>>
>>58638662
POSIX standard is the standard. Windows is annoying but it will conform to POSIX standard. Just be patient anon. It will happen. It's like metric system, it's better and it conquered the world. POSIX is better and will conquer the world too.
>>
Let's see how you do. From the K&R book:

Exercise 1-13. Write a program to print a histogram of the lengths of words in its input. It is easy to draw the histogram with the bars horizontal; a vertical orientation is more challenging.
>>
>>58638718
The idea was that the language should take care of that. If your system be it windows / bsd / linux / ... it the language should define inteface for all the features they have and you should be able to limit youreslf to for system with those specific features.

Posix is not the C standard. You are relying on faggots to suck dick. They might seem fine now but eventually they will fuck everything up.
>>
>>58638799
>Posix is not the C standard
Soon all OS will support it.

>You are relying on faggots to suck dick.
Keep that for you, it's stupid.
>>
>>58638766
#include <ctype.h>
#include <math.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
char buf[1024];
size_t len = 0;
bool in_word = false;

int *freq = NULL;
size_t max_len = 0;
size_t max_freq = 0;

while ((fgets(buf, sizeof buf, stdin)) != NULL) {
for (char *ptr = buf; *ptr; ++ptr) {
if (!in_word && isalnum(*ptr)) {
len = 0;
in_word = true;
} else if (in_word && !isalnum(*ptr)) {
in_word = false;

if (len > max_len) {
int *new = realloc(freq, len * sizeof *freq);
if (!new) {
perror("realloc");
continue;
}

freq = new;
memset(freq + max_len, 0, (len - max_len) * sizeof *freq);
max_len = len;
}

if (++freq[len - 1] > max_freq)
max_freq = freq[len - 1];
}

++len;
}
}

if (!max_len)
return 0;

int width = floor(log10(max_len)) + 1;

for (size_t i = max_freq; i > 0; --i) {
for (size_t j = 0; j < max_len; ++j)
for (int k = 0; k < width; ++k)
putchar(freq[j] >= i ? '#' : ' ');

putchar('\n');
}

for (size_t i = 0; i < max_len; ++i)
printf("%-*zu", width, i + 1);

putchar('\n');

free(freq);
}
>>
>>58639743
how can i put code

```js
console.log("test");
```
>>
>>58639743
Doesn't work, infinite loop
>>
>>58640268
With what input?
Are you even sending an EOF?
>>
Anyone know some good resources for ncurses? I wish to use it in my next project. Everything I found seems to be made right after the Gulf War.
>>
>>58640302
I don't know, how do I do it?
>>
>>58640330
Ctrl-D on *nix.
Ctrl-Z on Windows.
>>
>>58640307
http://tldp.org/HOWTO/NCURSES-Programming-HOWTO/

This has all you need to know

also, use
man curses
to find info about any function. in the man page, you just use / to find what you're looking for
>>
>>58640342
Works
Thread posts: 86
Thread images: 8


[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y] [Search | Top | Home]

I'm aware that Imgur.com will stop allowing adult images since 15th of May. I'm taking actions to backup as much data as possible.
Read more on this topic here - https://archived.moe/talk/thread/1694/


If you need a post removed click on it's [Report] button and follow the instruction.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com.
If you like this website please support us by donating with Bitcoins at 16mKtbZiwW52BLkibtCr8jUg2KVUMTxVQ5
All trademarks and copyrights on this page are owned by their respective parties.
Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
This is a 4chan archive - all of the content originated from that site.
This means that RandomArchive shows their content, archived.
If you need information for a Poster - contact them.