Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
Dub Step Dad
Dec 29, 2008
I'm very new to C so hopefully this will be an easy question.
I'm writing a program in C which is supposed to count the number of lines in a file, then create a 2D int array with a size of size nx3 (n is the number of lines). Here's what I have written to create it:


code:
array= (int **)malloc(numOfLines*sizeof(int*));
for(int i=0; i<numOfLines; i++)
	array[i] = (int *)malloc(3*sizeof(int*));
I've looked around online and I thought that this would do it, but even when I have, for example, 51 lines, it's just giving me an array of size 4x4 instead of 51x3. What am I doing wrong?

Adbot
ADBOT LOVES YOU

TasteMyHouse
Dec 21, 2006
when you say that it gives you a 4x4 array, how do you know that?

and how are you counting the number of lines?

TasteMyHouse fucked around with this message at 06:30 on Sep 13, 2011

Dub Step Dad
Dec 29, 2008

TasteMyHouse posted:

when you say that it gives you a 4x4 array, how do you know that?

and how are you counting the number of lines?

When I noticed things weren't acting like they were supposed to, I started adding printf statements to try to understand what was happening. The prof in my class added a sample input file for me to read in, and that ended up being 51 lines. After I created the array I told it to printf the number of lines and the sizeof the array and array[1], and that returned "51 4 4". Also after I try to put the numbers into an array I had that print out by row and it ended up printing out the first 16 numbers in a 4x4 format.
This program is supposed to read vertices in 3D space and blocks of vertices in the file are supposed to form polygons, so between these blocks there's a 'J'. The way I'm counting the lines is as it reads through each individual character, if it reads a number then it marks a bool true, and then once it reaches the new line character if that bool is set to true then it'll increment numOfLines. I manually counted the number of vertices in the file and there were 51, so it looks like that's working properly.

Jabor
Jul 16, 2010

#1 Loser at SpaceChem
C doesn't keep track of array dimensions automatically, you need to do that yourself.

I think you'll find that sizeof() is giving you the pointer size (because that's what it knows about at compile-time), which is unrelated to the number of elements in the array.

Dub Step Dad
Dec 29, 2008

Jabor posted:

C doesn't keep track of array dimensions automatically, you need to do that yourself.

I think you'll find that sizeof() is giving you the pointer size (because that's what it knows about at compile-time), which is unrelated to the number of elements in the array.

Oh hey that fixed it. Thanks a lot! Prior to this all I had ever written in was Java so I'm still having trouble wrapping my head around all this.

TasteMyHouse
Dec 21, 2006

Puppet Pal Claudius posted:

After I created the array I told it to printf the number of lines and the sizeof the array and array[1], and that returned "51 4 4".

Looks like you already understand why this basic approach doesn't work, but I thought I'd mention that array[1] is not the first element in the array, but the second.

Dub Step Dad
Dec 29, 2008

TasteMyHouse posted:

Looks like you already understand why this basic approach doesn't work, but I thought I'd mention that array[1] is not the first element in the array, but the second.

Oh I know that, I just arbitrarily put 1 in there since every row will have the same number of elements in there and also 1 is a nice number.

TasteMyHouse
Dec 21, 2006
hahaha okay :confused:

Bob Morales
Aug 18, 2006


Just wear the fucking mask, Bob

I don't care how many people I probably infected with COVID-19 while refusing to wear a mask, my comfort is far more important than the health and safety of everyone around me!

Puppet Pal Claudius posted:

Oh hey that fixed it. Thanks a lot! Prior to this all I had ever written in was Java so I'm still having trouble wrapping my head around all this.

The C FAQ covers a lot of quirks with stuff like pointers and arrays

http://c-faq.com/~scs/cgi-bin/faqcat.cgi

It also has a lot of 'old' stuff you probably won't run into, like things about DOS or old versions of UNIX.

Paniolo
Oct 9, 2007

Heads will roll.
So, the Windows 8 Developer Preview build is available for the general public: http://msdn.microsoft.com/en-us/windows/home/

It includes the new Visual Studio (express) and probably most interestingly the new Windows SDK which includes WinRT, the C++-based API which is a replacement for (and is not built on) Win32.

Downloading it now, but I'm pretty curious about what new C++0x features are in the new compiler. If any.

roomforthetuna
Mar 22, 2005

I don't need to know anything about virii! My CUSTOM PROGRAM keeps me protected! It's not like they'll try to come in through the Internet or something!

Paniolo posted:

It includes the new Visual Studio (express) and probably most interestingly the new Windows SDK which includes WinRT, the C++-based API which is a replacement for (and is not built on) Win32.
About time for something like that! I hope it's like the BeOS C++ API. Not that I ever used that, but it looked pretty sweet to use compared to Win32.

But I expect it to be a horrible monster more like MFC.

Paniolo
Oct 9, 2007

Heads will roll.
Apparently using WinRT requires using C++\CLI style extensions, so MS is kind of full of poo poo when talking about how it's "C++"

Jethro
Jun 1, 2000

I was raised on the dairy, Bitch!
Unless they mean it isn't C

TasteMyHouse
Dec 21, 2006
I have a C project that has an obsolete #define that needs to be updated, but in the horrific rat's nest of headers I am having difficulty finding where it is. is there any trick I can pull with gcc to get it to tell me where a #define originated, or am I stuck with using find, grep, and manually tracing through the files?

Plorkyeran
Mar 22, 2007

To Escape The Shackles Of The Old Forums, We Must Reject The Tribal Negativity He Endorsed
Define it again and the redefinition warning will say where the previous one was.

TasteMyHouse
Dec 21, 2006
Thanks!

Another Poster
Apr 12, 2008
So I'm working on a small basic C++ problem.

I need to calculate the summation of Lucas series with a user given upper bound number. Basically I'm stuck at the logistic part. I know I will have to use loops (for loops) to find a value c = a + b until c <= upper bound. I just don't know how to set up the loop initiation and condition.

Here the excerpt for the problem:
code:
Compute all Lucas numbers up to a certain number. Keep track of a summation of all Lucas number you have encountered up to that point. Examples:
1. User input bound 23: Lucas numbers up to 23 are: 2,1,3,4,7,11,18. Sum is 46.
2. User input bound 18: Lucas numbers up to 18 are: 2,1,3,4,7,11,18. Sum is 18.
3. User input bound 17: Lucas numbers up to 17 are: 2,1,3,4,7,11. Sum is 28.
Just looking for hints and pointers here. And I know how to check if a number is a Lucas number or not. Just confuse on for loops summation.

shrughes
Oct 11, 2008

(call/cc call/cc)

Another Poster posted:

And I know how to check if a number is a Lucas number or not.

You shouldn't be checking whether a number is a Lucas number or not. It's not like you're going to iterate through numbers until you get one that's a Lucas number.

nop
Mar 31, 2010

Another Poster posted:

Just looking for hints and pointers here. And I know how to check if a number is a Lucas number or not. Just confuse on for loops summation.

I suspect you would want a while loop. You can do it with a for loop, but it would look silly.

Another Poster
Apr 12, 2008

nop posted:

I suspect you would want a while loop. You can do it with a for loop, but it would look silly.

Ok yeah, turn out it is a rather simple while loop.

Another quick question, how would the condition: "when x=1 or 2 or 3 or 4 or 7 AND y=1 or 2 or 3 or 4 or 7, do stuff" be written?

roomforthetuna
Mar 22, 2005

I don't need to know anything about virii! My CUSTOM PROGRAM keeps me protected! It's not like they'll try to come in through the Internet or something!

Another Poster posted:

Ok yeah, turn out it is a rather simple while loop.

Another quick question, how would the condition: "when x=1 or 2 or 3 or 4 or 7 AND y=1 or 2 or 3 or 4 or 7, do stuff" be written?
code:
if ((x==1 || x==2 || x==3 || x==4 || x==7) &&
    ((y>=1 && y<=4) || y==7)) 
{
  DoStuff();
}
I did the x and y line differently because both are okay ways, x is more readable, y is fewer comparisons.

It might also be done with a function to search through a const array of ints, which would be more complicated to write but more easily extended, or, in C++, checking against a std::set which would make it extensible to many more checks without taking significantly longer.

Another Poster
Apr 12, 2008

roomforthetuna posted:

code:
if ((x==1 || x==2 || x==3 || x==4 || x==7) &&
    ((y>=1 && y<=4) || y==7)) 
{
  DoStuff();
}
I did the x and y line differently because both are okay ways, x is more readable, y is fewer comparisons.

It might also be done with a function to search through a const array of ints, which would be more complicated to write but more easily extended, or, in C++, checking against a std::set which would make it extensible to many more checks without taking significantly longer.

Thank you, just what I was looking for. And so there no way but to type x== each time?

roomforthetuna
Mar 22, 2005

I don't need to know anything about virii! My CUSTOM PROGRAM keeps me protected! It's not like they'll try to come in through the Internet or something!

Another Poster posted:

Thank you, just what I was looking for. And so there no way but to type x== each time?
Other than making it a function to compare against a list of some kind, no.

Edit: Or you could do it as a couple of switch statements, in which case you'd have to type 'case' as many times as you'd have to type x== so it wouldn't really help, but it is another way!

roomforthetuna fucked around with this message at 03:57 on Sep 15, 2011

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe
No, seriously, if you ever need to test whether a number is a Lucas number, you are doing it wrong.

TasteMyHouse
Dec 21, 2006

roomforthetuna posted:

Or you could do it as a couple of switch statements, in which case you'd have to type 'case' as many times as you'd have to type x== so it wouldn't really help, but it is another way!

Just wanna point out that switch requires the thing you're comparing against to be known at compile time. you can do this:

code:
int x=getSomeValue();
switch(x)
{
case 1:
    //do something
    break;
case 2:
    //do something else
    break;
}

but not this:


code:
int x=getSomeValue();
int y=getSomeOtherValue();
int z=getYetAnotherValue();
switch(x)
{
case y:
    //do something
    break;
case z:
    //do something else
    break;
}

e: this post is for Another Poster's benefit, not roomforthetuna's

TasteMyHouse fucked around with this message at 04:43 on Sep 15, 2011

tractor fanatic
Sep 9, 2005

Pillbug
If I create a smart pointer (more of a reference) where the semantics for operator= are:

code:
class smart{
    type * pointer;
    smart & operator = (const smart & other){
        if (pointer)
            *pointer = *other.pointer;
        else
            pointer = other.pointer;
    }
};
Will this play nice with the STL containers, especially std::vector?

Basically, I want the assignment operator to copy the values, and not the pointers, and a seperate rebind method to change the pointer, but I'm not sure how this would work with std::vector

After further thought, this is probably a bad idea. I wanted something that would have reference semantics normally, but have pointer semantics inside of a container.

tractor fanatic fucked around with this message at 22:57 on Sep 15, 2011

Paniolo
Oct 9, 2007

Heads will roll.
Found a list of C++11 features in Visual C++ 2011:

http://blogs.msdn.com/b/vcblog/archive/2011/09/12/10209291.aspx

Pretty disappointing. I was at least hoping for variadic templates and template aliases.

Plorkyeran
Mar 22, 2007

To Escape The Shackles Of The Old Forums, We Must Reject The Tribal Negativity He Endorsed
The WinRT stuff probably took up all of the developer time that would have been spent on C++11 support. I can't imagine that porting C++/CLI from the CLR to native code was easy.

tractor fanatic
Sep 9, 2005

Pillbug
You can't bind a temporary to an lvalue reference, but you can do it with an rvalue reference. VC has a compiler extension that lets you bind temporaries to non-const lvalue references, so does anyone know if the semantics for this are the same as if you had bound the temporary to some non-const rvalue reference, and then took an lvalue reference of the rvalue reference?

Like, would this code in MSVC

code:
int f() { return 5; }

void g(int & r);

int main(){
    int & val = f();
    g(f());
}

be the same as this in standard C++11

code:
int f() { return 5; }

void g(int & r);
void g(int && r) { g(r); }

int main(){
    int && rval = f();
    int & val = rval;

    g(f());
}

Paniolo
Oct 9, 2007

Heads will roll.

Plorkyeran posted:

The WinRT stuff probably took up all of the developer time that would have been spent on C++11 support. I can't imagine that porting C++/CLI from the CLR to native code was easy.

Yeah, here's a Herb Sutter talk from BUILD where he basically confirms that C++0x features took a back seat to WinRT and GPGPU stuff in this release. But supposedly variadic templates are all but done and will be added in a service pack.

Jort
Jan 10, 2007
I'm trying to create a cleanup function for a matrix class I'm using. I'm attempting to delete a 2D array, but I'm getting an error telling me "pointer being freed was not allocated"

code:

void matrix::removal()
{
    for (int i = 0; i < rows; i++)
    {
        delete [] values[i];
    }
	delete [] values;
}
The matrix class is correctly getting populated and manipulated, but when I try to run this function in my main function, I get the error.

shrughes
Oct 11, 2008

(call/cc call/cc)

Jort posted:

I'm trying to create a cleanup function for a matrix class I'm using. I'm attempting to delete a 2D array

Could you post the code that constructs the array and the code that uses it?

Sylink
Apr 17, 2004

I'd like to do some C++ practice, I've mostly stayed away due to not knowing what the gently caress about compilers and mostly do Python.


However, i really should learn.

Is there a good free compiler I can use to start? I'd like to stick to open/cross platform libraries as much as possible rather than delving into Visual Studio.


I've seen devc++, is that good enough?

Vanadium
Jan 8, 2005

Devc++ as an IDE is pretty poo poo (unless it's changed dramatically within the last year or so, which I doubt), while Visual Studio is probably the best you are likely to get your hands on, and you can write perfectly portable code with it too.

TasteMyHouse
Dec 21, 2006

Sylink posted:

I'd like to do some C++ practice, I've mostly stayed away due to not knowing what the gently caress about compilers and mostly do Python.


However, i really should learn.

Is there a good free compiler I can use to start? I'd like to stick to open/cross platform libraries as much as possible rather than delving into Visual Studio.


I've seen devc++, is that good enough?

Dev C++ isn't a compiler -- rather, it is an IDE; it provides a front-end tying together the MinGW version of GCC (the GNU Compiler Collection), GDB (the GNU Debugger), and a simple text editor.

It kind of sucks and is poorly supported and (when I last used it in 2009) buggy.

If you don't want to use Visual Studio, but (for some reason) still want an IDE, you can check out Eclipse CDT. Eclipse is a free and libre IDE originally developed for Java, but with CDT can compile C and C++. It also uses GCC.

If you want to learn about the compilation process, don't use an IDE at all. Just get a text editor (GEdit, Notepad++, Emacs, Vim, whatever) then use MinGW to compile files on the command line.


e: if you're really interested in making things cross platform, get yourself some kind of GNU/Linux (virtual box, dual boot, Wubi, whatever) as well.

TasteMyHouse fucked around with this message at 19:52 on Sep 22, 2011

Paolomania
Apr 26, 2006

Sylink posted:

Is there a good free compiler I can use to start? I'd like to stick to open/cross platform libraries as much as possible rather than delving into Visual Studio.

If free-as-in-price is what you care about, you can download the Win32 SDK for free. This contains the basics like cl (the compiler that Visual Studio uses), the STL libraries, and nmake (the Microsoft make utility). Throw in a C++ aware text editor like TextPad and you are good to go. As a bonus, using cl directly will teach you all the compiler flags.

Sylink
Apr 17, 2004

Yah I have a Fedora laptop I use for messing around so Ill check this stuff out. I really want to stay cross platform.

nielsm
Jun 1, 2009



Sylink posted:

Yah I have a Fedora laptop I use for messing around so Ill check this stuff out. I really want to stay cross platform.

FYI, just using the Visual C++ IDE does not magically make your code become non-standard. There's two ways VC++ can make you write weird code: MFC and C++/CLR (C++ for .NET). With the Win8 Developer Preview there's also a third, C++/CX. But it's not like it writes weird code behind your back, unless you actually tell it to create a project using any of those variations.
Just get Visual C++ 2010 Express and start writing standard C++ using a good compiler and wonderful IDE.

(Actually, you don't even get MFC with VC Express, so that's one less.)

It just seems to me that people have some weird ideas about VC++ making you write terrible non-standard code... maybe that was true 10 years ago with version 6. That's 10 years ago.

pseudorandom name
May 6, 2007

cl.exe is a fairly non-conformant compiler as far as these things go, so there's a lot of things that shouldn't work but do, and a lot of things that should work but don't.

Adbot
ADBOT LOVES YOU

roomforthetuna
Mar 22, 2005

I don't need to know anything about virii! My CUSTOM PROGRAM keeps me protected! It's not like they'll try to come in through the Internet or something!
The other thing VC++ does that's bad for cross-platforming is that it does everything with vcproj files where the more-standard is makefiles. That's pretty unhelpful when it comes to compiling on other systems.

(Not that you can't use the compiler with text editors and command lines and stuff, but if you use the IDE then you'll be using vcproj files.)

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply