• Hey, guest user. Hope you're enjoying NeoGAF! Have you considered registering for an account? Come join us and add your take to the daily discourse.

Programming |OT| C is better than C++! No, C++ is better than C

I know this isn't what you asked, but... why do you want to learn C?

A course I am taking requires us to write the programs in C, despite a previous course (and most of the rest of the courses) using C++. No fucking clue why, especially since it is about data structures and algorithms.

But basically I need to write my programs in C now and I just need a basic syntax guide so I'm not scratching my head trying to figure out "ok why isn't std::cout << "Hello World" << std::endl; working?"
 

upandaway

Member
Is that available as a free online resource?
Don't think so, but if you have access to a CS library then it would definitely have a copy

edit: had my data structures in C too, I can see why they'd do that. the transparency is important to talk about algorithms on a universal level (plus, algorithms in OOP is a separate subject by itself)
 
A course I am taking requires us to write the programs in C, despite a previous course (and most of the rest of the courses) using C++. No fucking clue why, especially since it is about data structures and algorithms.

But basically I need to write my programs in C now and I just need a basic syntax guide so I'm not scratching my head trying to figure out "ok why isn't std::cout << "Hello World" << std::endl; working?"

Pretty much all you need to know is:

1) All header files end in .h. If you see a header file that has no file extension, like <vector> or <iostream>, it's C++. Don't use it.

2) Anything that starts with std:: is c++, don't use it. google for alternatives. For example, C equivalent of cout

3) Don't use classes or templates, but structs are ok.

4) structs can't have member functions, only data.

That's pretty much 99% of what you need to know, if you start from those assumptions, you can figure out the rest with documentation and google searches.
 
Pretty much all you need to know is:

1) All header files end in .h. If you see a header file that has no file extension, like <vector> or <iostream>, it's C++. Don't use it.

2) Anything that starts with std:: is c++, don't use it. google for alternatives. For example, C equivalent of cout

3) Don't use classes or templates, but structs are ok.

4) structs can't have member functions, only data.

That's pretty much 99% of what you need to know, if you start from those assumptions, you can figure out the rest with documentation and google searches.

Thank you. :)
 
Here's an in depth article from the developer:

http://blog.dustinkirkland.com/2016/03/ubuntu-on-windows.html

"Right, so just Ubuntu running in a virtual machine?" Nope! This isn't a virtual machine at all. There's no Linux kernel booting in a VM under a hypervisor. It's just the Ubuntu user space.

"Ah, okay, so this is Ubuntu in a container then?" Nope! This isn't a container either. It's native Ubuntu binaries running directly in Windows.

"Hum, well it's like cygwin perhaps?" Nope! Cygwin includes open source utilities are recompiled from source to run natively in Windows. Here, we're talking about bit-for-bit, checksum-for-checksum Ubuntu ELF binaries running directly in Windows.

[long pause]

"So maybe something like a Linux emulator?" Now you're getting warmer! A team of sharp developers at Microsoft has been hard at work adapting some Microsoft research technology to basically perform real time translation of Linux syscalls into Windows OS syscalls. Linux geeks can think of it sort of the inverse of "wine" -- Ubuntu binaries running natively in Windows. Microsoft calls it their "Windows Subsystem for Linux". (No, it's not open source at this time.)
 

Koren

Member
Yeah, this is VERY cool, and I just got around using cygwin too. (I do not see the issue with this one YET, but Ill gladly use the native solution over it.)
Yes, but I'm wondering...

Using Cygwin for creating Windows binaries is far from perfect, but at least, you can distribute it with only the cygwin dll and possibly a couple others dll.

Will Windows run in *standard* Linux binaries, or will you need to install something (big)?


Also, there's already few users of powershell, that won't help :/
 
Minor question:

Code:
struct employee{
	int id;
	int company_rank;
};

struct employee* allocate(){

//Allocate enough memory to hold up to 10 employees 
//return the pointer
}

The struct employee has two integers in it. An integer is 4 bytes. So does this mean each struct is 8 bytes total, and my pointer needs to have 80 bytes allocated to it?
 

Godslay

Banned
Yes, but I'm wondering...

Using Cygwin for creating Windows binaries is far from perfect, but at least, you can distribute it with only the cygwin dll and possibly a couple others dll.

Will Windows run in *standard* Linux binaries, or will you need to install something (big)?


Also, there's already few users of powershell, that won't help :/

Few users of Powershell?

It's a huge part of the Windows ecosystem. This won't change that.
 

Koren

Member
Few users of Powershell?
That was my impression, but I may be wrong.

It's just I've never seen a single one in flesh, actually... Barely even met someone who KNOWS it exists IRL. Granted, academics in CS are often closer to Linux than Windows, so it doesn't help, but still...
 

Koren

Member
The struct employee has two integers in it. An integer is 4 bytes. So does this mean each struct is 8 bytes total, and my pointer needs to have 80 bytes allocated to it?
Short answer: yes, probably

Longer answer: is it a question about the space it'll take in memory, or do you intend to try to allocate the memory yourself?

It's tricky, and you probably want to be careful. int size can change, and alignment issues can happen. although in this case, I expect 4-bytes objects be packed back-to-back (and any address got with allocation aligned on adress multiple of 4).

There's a reason even C had "sizeof"...

You'll probably be interested in this:
http://www.catb.org/esr/structure-packing/
 
Short answer: yes, probably

Longer answer: is it a question about the space it'll take in memory, or do you intend to try to allocate the memory yourself?

It's tricky, and you probably want to be careful. int size can change, and alignment issues can happen. although in this case, I expect 4-bytes objects be packed back-to-back (and any address got with allocation aligned on adress multiple of 4).

There's a reason even C had "sizeof"...

You'll probably be interested in this:
http://www.catb.org/esr/structure-packing/

Discovered sizeof after I made the post lol.

I imagine:
struct employee *ptr = malloc(sizeof(struct employee)*10);

works?
 

Godslay

Banned
That was my impression, but I may be wrong.

It's just I've never seen a single one in flesh, actually... Barely even met someone who KNOWS it exists IRL. Granted, academics in CS are often closer to Linux than Windows, so it doesn't help, but still...

It's out there.

It's better suited to working with .Net than bash will ever be. That's why it won't likely cut into the existing user base that deals specifically with Windows.

Only in the IT / admin community. Developers don't use it at all.

It is predominately used in IT/Admin, sure. That doesn't mean that developers don't use it at all.

Take Nuget packages for instance. You want to write one for yourself? Probably should know some Powershell. Powers the package manager console.
 
I kept wondering where I was going wrong on this Bubble Sort algorithm since it never ended and it turns out my conditional check was using = instead of ==....

I thought I was past these sorts of errors.
 

Makai

Member
I kept wondering where I was going wrong on this Bubble Sort algorithm since it never ended and it turns out my conditional check was using = instead of ==....

I thought I was past these sorts of errors.
Not your fault. More modern languages prevent stuff like that.
 
Ok so I have

struct employee *ptr from before. I gave it enough memory to hold 10 employee structs.

If I want to free the memory I allocated to it, do I just call free once? Or do I need to free ptr[0], ptr[1], etc...?

I think I only need to call free once?
 

Aikidoka

Member
Bash is officially coming to Windows

My brain is melting at the awesomeness of this news.

So will this be similar to what is OS X does? I'm very excited about this after giving up on MinGW for a VM. Especially, since I've found VMs quite annoying about not utilizing my system's hardware.

Wait they're bringing an entire Ubuntu system over? I'd rather just use Ubuntu...

And lol at emacs

emacs is great.


Anyways, this recent homework has been a mixture of frustration and satisfaction. On the one hand, learning MPI and applying it to designing a parallel DGEMM is really interesting and useful. However, it was given to us after just one 3-hour lecture on MPI and the documentation and tutorials around MPI are minimal at best. Thankfully there's at least one stackexchange post on everything.
 

Koren

Member
Ok so I have

struct employee *ptr from before. I gave it enough memory to hold 10 employee structs.

If I want to free the memory I allocated to it, do I just call free once? Or do I need to free ptr[0], ptr[1], etc...?

I think I only need to call free once?
You MUST call it once.

You have to free memory from malloc with exact mirror calls to free (the order does't matter)

For each pointer given by malloc, you need to call free with the same pointer, and only once.

You cannot write free(ptr[1])

I thought I was past these sorts of errors.
You'll never be.

Discovered sizeof after I made the post lol.

I imagine:
struct employee *ptr = malloc(sizeof(struct employee)*10);

works?
I'd add a (struct employee*) before malloc to make the cast explicit, but yes.
 

subtles

Member
I was wondering if someone here can help me with a program I was writing in PuTTY. The section I'm having problems with is below:

cout << "\nPlease enter the name of one of the above teams: \n";
getline(cin, team);
ifstream inputFile2;
inputFile2.open("WorldSeriesWinners.txt");
while (!inputFile2.eof())
{
getline(inputFile2, compare);
if (team == compare)
{
count += 1;
//count++;
}
}
inputFile2.close();
cout << "The number of times " << team << " has won the World Series between 1903 and 2012 is: " << count << endl;

------------------------------------------------

The counter does not increment past the initialization in the beginning of the program. I've tried writing it in a variety of ways but still end up with 0.

I appreciate any help and/or suggestions.
 

vypek

Member
I was wondering if someone here can help me with a program I was writing in PuTTY. The section I'm having problems with is below:

Code:
cout << "\nPlease enter the name of one of the above teams: \n";
  getline(cin, team);
  ifstream inputFile2;
  inputFile2.open("WorldSeriesWinners.txt");
  while (!inputFile2.eof())
  { 
     getline(inputFile2, compare);
     if (team == compare)
     {
        count =+ 1;
        //count++;
     }
  }
  inputFile2.close();
  cout << "The number of times " << team << " has won the World Series between 1903 and 2012 is: " << count << endl;

------------------------------------------------

The counter does not increment past the initialization in the beginning of the program. I've tried writing it in a variety of ways but still end up with 0.

I appreciate any help and/or suggestions.

count =+ 1;
Try count += 1 or count++ instead

Or is the commented out count++ something you already tried that for some reason did not work?
 

dabig2

Member
I was wondering if someone here can help me with a program I was writing in PuTTY. The section I'm having problems with is below:

cout << "\nPlease enter the name of one of the above teams: \n";
getline(cin, team);
ifstream inputFile2;
inputFile2.open("WorldSeriesWinners.txt");
while (!inputFile2.eof())
{
getline(inputFile2, compare);
if (team == compare)
{
count =+ 1;
//count++;
}
}
inputFile2.close();
cout << "The number of times " << team << " has won the World Series between 1903 and 2012 is: " << count << endl;

------------------------------------------------

The counter does not increment past the initialization in the beginning of the program. I've tried writing it in a variety of ways but still end up with 0.

I appreciate any help and/or suggestions.

Does your program ever enter that if conditional successfully to increment that counter? What does your file input look like? If the comparison is false, first look into your string variables to see what's in them (before the if statement, print to console both strings) and then maybe look into trimming your input of leading and trailing white space characters. For example,
"Cubs" and "Cubs " will not match with your code as it is (Cubs 2016 WS champs believe it!)
 

subtles

Member
count =+ 1;
Try count += 1 or count++ instead

Or is the commented out count++ something you already tried that for some reason did not work?
It was originally count +=1, but I made a mistake typing it in again it after using count++.

Does your program ever enter that if conditional successfully to increment that counter? What does your file input look like? If the comparison is false, first look into your string variables to see what's in them (before the if statement, print to console both strings) and then maybe look into trimming your input of leading and trailing white space characters. For example,
"Cubs" and "Cubs " will not match with your code as it is (Cubs 2016 WS champs believe it!)

I've printed the lines out and verified that they are the same without any additional characters.
 

Koren

Member
I was wondering if someone here can help me with a program I was writing in PuTTY. The section I'm having problems with is below:

Code:
     if (team == compare)

The counter does not increment past the initialization in the beginning of the program. I've tried writing it in a variety of ways but still end up with 0.

I appreciate any help and/or suggestions.
How are declared team and compare?

In this case, == may test the physical equality, not the structural equality.

If team and compare are recognized as pointers, since the pointers are different, even if the strings are the equals, team can't be equal to compare.

Use strcmp(team, compare) to compare the strings and not the pointers if this is the case...
 

subtles

Member
Both team and compare are declared as strings, so I don't think the program is comparing the pointers but rather the content of the variables themselves i.e the strings.
 

Koren

Member
Both team and compare are declared as strings, so I don't think the program is comparing the pointers but rather the content of the variables themselves i.e the strings.
Yes, sorry... I've just tried and it even don't accept a char buffer, anyway.

I must be oversensitive to this today (too much code from student to read, and with == / != / is / is not in Python translating to = / <> / == / != in ML, it's a nightmare catching all those tricky bugs.

Beside, I've been burned before with string comparisons...


Maybe you could write the content of each string as a list of hex values, and check whether they have something different in them?
 

subtles

Member
Yes, sorry... I've just tried and it even don't accept a char buffer, anyway.

I must be oversensitive to this today (too much code from student to read, and with == / != / is / is not in Python translating to = / <> / == / != in ML, it's a nightmare catching all those tricky bugs.

Beside, I've been burned before with string comparisons...


Maybe you could write the content of each string as a list of hex values, and check whether they have something different in them?

It's fine, and thanks for the suggestion! I'll probably be able to figure it out as this was the only program out of the five I worked on this week that gave me issues. I just wanted a fresh pair of eyes to catch anything I may have overlooked but I'm sure it's a silly mistake that I'll find somewhere.

Thanks and take care of yourselves!
 

Koren

Member
I just wanted a fresh pair of eyes
Unfortunately, I'm not fresh today ^_^

I may suspect something with \r at the end of the line... If lines in the file are \r\n terminated, getline will return the line without \n but with \r IIRC.

Since it's not a printable character and the display of \r in a terminal can be silent, that could be a possible reason...
 

dabig2

Member
Unfortunately, I'm not fresh today ^_^

I may suspect something with \r at the end of the line... If lines in the file are \r\n terminated, getline will return the line without \n but with \r IIRC.

Since it's not a printable character and the display of \r in a terminal can be silent, that could be a possible reason...

Yep, this is the next thing to test for if you haven't already, subtles. Carriage returns are hidden and getline doesn't discriminate in keeping them out of a string unless you tell it to. People often have this problem when reading from a file that was created on a Windows system and is now being read in a UNIX environment.

So you might need to scrub the strings of those pesky carriage returns before doing a comparison.
 

Koren

Member
I think it's even worse: it does remove the endline character, but according to the OS, or something like that. Problems arise if the file hasn't been created with the same rules, which can mess things big time.
 

subtles

Member
So if the line is:

New York Yankees

with nothing else shown in the text file, what suffix am I supposed to add?

Edit: I had the program read from the same text file in the comparison (the original one with the team list) and it didn't give me a count of one like it was supposed to, meaning the issue most probably isn't the textfile.
 

Koren

Member
So if the line is:

New York Yankees

with nothing else shown in the text file, what suffix am I supposed to add?
Probably none, it's a matter of removing an unwanted, invisible character...

Edit: I had the program read from the same text file in the comparison (the original one with the team list) and it didn't give me a count of one like it was supposed to, meaning the issue most probably isn't the textfile.
I've sent you an MP, but since your code works for me when I set properly the end of line characters in the text file, I'm more and more convinced that's the issue.



Edit : just to check...

Would you mind adding
Code:
cout << (int)(team.at(team.length()-1)) << " " << (int)(compare.at(compare.length()-1)) << endl;
on the line before the == comparison, and post the results of a request?

(or, if it's difficult to copy/paste or too long, whether the number of pair of integers match the number of lines in WorldSeriesWinners.txt, and whether you see 13 or, less likely 10, appearing among the numbers in one of the columns)
 
You MUST call it once.

You have to free memory from malloc with exact mirror calls to free (the order does't matter)

For each pointer given by malloc, you need to call free with the same pointer, and only once.

You cannot write free(ptr[1])


You'll never be.


I'd add a (struct employee*) before malloc to make the cast explicit, but yes.

Thanks a bunch Koren.
 

Koren

Member
Asshole pedantic question: What is the exception to this rule?
Good one... Not sure what you're after.

Since freeing NULL is guaranted to be safe, I guess it's not when malloc fail.

I suppose that cases where the memory can be freed elsewhere, including when there's a fork between the malloc and the free don't count...


Any hint?
 
Good one... Not sure what you're after.

Since freeing NULL is guaranted to be safe, I guess it's not when malloc fail.

I suppose that cases where the memory can be freed elsewhere, including when there's a fork between the malloc and the free don't count...


Any hint?
Part of your original comment was that you must never call free more than once on the return value of malloc. There's an exception to that, a time when you theoretically can
 
Top Bottom