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
Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh

Flashdance posted:

This is because when I implement it that way, it deletes both the temp and the docarray entry whenever I use a delete statement and then other parts of the program crash.

That's because they're the same thing. Isn't that the point of writing it that way?

Adbot
ADBOT LOVES YOU

zynga dot com
Nov 11, 2001

wtf jill im not a bear!!!

A dossier and a state of melted brains: The Jess campaign has it all.

Avenging Dentist posted:

That's because they're the same thing. Isn't that the point of writing it that way?

Well yes, which is why they're currently commented out. I was following a guide I found and that was how their ragged array was implemented; obviously their example is not going to work for what I want to do.

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh
That would work fine, but then you can't go and delete that data without expecting it to be deleted everywhere it's referenced. You'd want to copy the char * argument though since you have no guarantee that it was created with new. You've also got lots of off-by-one errors; you don't allocate space for the null terminator for your strings and your loop starting at current+1 should start at current. It's also a waste of time to make a temp array, copy the docarray stuff into it (and insert one element) and then copy the entire thing back to the docarray.

Also why are you not just using a std::vector<std::string>?

zynga dot com
Nov 11, 2001

wtf jill im not a bear!!!

A dossier and a state of melted brains: The Jess campaign has it all.

Avenging Dentist posted:

Also why are you not just using a std::vector<std::string>?

Oh yeah, I probably should have mentioned this first. I'm not allowed to use vectors or the string class for this particular part.


Avenging Dentist posted:

You'd want to copy the char * argument though since you have no guarantee that it was created with new.

Why would this matter? Genuine question.

Avenging Dentist posted:

You've also got lots of off-by-one errors; you don't allocate space for the null terminator for your strings and your loop starting at current+1 should start at current.

I thought strlen returned the length with the null terminator, but I checked after reading that and it apparently doesn't so I fixed that part. As for the loop, it starts at current + 1 because the 2 lines between the loops is copying c in what would be the current position.

Avenging Dentist posted:

It's also a waste of time to make a temp array, copy the docarray stuff into it (and insert one element) and then copy the entire thing back to the docarray.

This is what I specifically concerned about. Right now I'm adding/deleting entries from the array by just making a new temp array one bigger/smaller, copying everything over, then recreating the normal array in the new size and copying everything back (skipping the deleted entry). I've been looking and looking for a way to add/remove items without making a new temp array but haven't figured one out yet. It's obviously expensive, cumbersome, and easy to make mistakes with.

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh

Flashdance posted:

Why would this matter? Genuine question.


char *foo = "bar";
delete foo; // Probably segfault


Flashdance posted:

As for the loop, it starts at current + 1 because the 2 lines between the loops is copying c in what would be the current position.

Read through it again. Your indices are wrong.

Flashdance posted:

I've been looking and looking for a way to add/remove items without making a new temp array but haven't figured one out yet. It's obviously expensive, cumbersome, and easy to make mistakes with.

You have to make a "temp" array. You don't have to make a new destination array. Just have it point to the temp array.

zynga dot com
Nov 11, 2001

wtf jill im not a bear!!!

A dossier and a state of melted brains: The Jess campaign has it all.

Avenging Dentist posted:


char *foo = "bar";
delete foo; // Probably segfault


I guess I was confused on why I should copy the char * argument if I'm not trying to delete it. You are referring to the char *c argument, right? I just want to make sure I'm not misunderstanding you.

Avenging Dentist posted:

Read through it again. Your indices are wrong.

I took another look and sure enough. The program worked properly for display but appeared to be exactly the reason the array wasn't deleting. The programs works properly now with delete.

Avenging Dentist posted:

You have to make a "temp" array. You don't have to make a new destination array. Just have it point to the temp array.

If I do that, aren't I back to the same problem as before with the multiple deletes from one pointer? If I allocate a temp array in the function, I have to delete it before leaving.

Thanks a ton for your help, I think the program works fine at this point.

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh

Flashdance posted:

I guess I was confused on why I should copy the char * argument if I'm not trying to delete it.

You will delete it one day I hope! (When your destructor is called.)

Flashdance posted:

If I do that, aren't I back to the same problem as before with the multiple deletes from one pointer? If I allocate a temp array in the function, I have to delete it before leaving.

No you don't. You really only need to do two things to make sure your memory management is correct: 1) have exactly the same number of news as deletes, 2) only delete things that have been newed. In this case, what you're doing is:

code:
// docarray is some thing made with new in the past
temp = new char*[...];

// copy pointers from docarray to temp...

delete docarray;
docarray = temp;
If it makes more sense, think about it like this:
code:
// docarray is some thing made with new in the past
old_docarray = docarray;
docarray = new char*[...];

// copy pointers from old_docarray to docarray...

delete old_docarray;

zynga dot com
Nov 11, 2001

wtf jill im not a bear!!!

A dossier and a state of melted brains: The Jess campaign has it all.

Avenging Dentist posted:

No you don't. You really only need to do two things to make sure your memory management is correct: 1) have exactly the same number of news as deletes, 2) only delete things that have been newed. In this case, what you're doing is:

code:
// docarray is some thing made with new in the past
temp = new char*[...];

// copy pointers from docarray to temp...

delete docarray;
docarray = temp;

Ok, so when I delete docarray without first deleting all docarray[n], it only removes the pointer to the array, which is reassigned by docarray = temp. If I'm reading this right, that means that I don't need to deallocate temp because docarray IS temp at this point.

So what happens to the old docarray[n] entries? They were allocated with new as well. Why wouldn't this be a memory leak, since I'm using new without a corresponding delete?

tractor fanatic
Sep 9, 2005

Pillbug

Flashdance posted:

Ok, so when I delete docarray without first deleting all docarray[n], it only removes the pointer to the array, which is reassigned by docarray = temp. If I'm reading this right, that means that I don't need to deallocate temp because docarray IS temp at this point.

So what happens to the old docarray[n] entries? They were allocated with new as well. Why wouldn't this be a memory leak, since I'm using new without a corresponding delete?

docarray and temp are not pointers to arrays of strings, they are pointers to arrays of pointers. The only memory allocation you need to do in this function is allocate space for the new string you are inserting, which is presumably deleted in your destructor or something, and you need to allocate memory for a new array of pointers. The old array of pointers gets deleted, and then you just set docarray = temp, so at the end of the function,

delete [] docarray;
docarray = temp;

All you should be doing is copying the pointers in the arrays that point to the strings.

zynga dot com
Nov 11, 2001

wtf jill im not a bear!!!

A dossier and a state of melted brains: The Jess campaign has it all.
Perfect, I get it now. Thanks a bunch.

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh

Flashdance posted:

Why wouldn't this be a memory leak, since I'm using new without a corresponding delete?

The corresponding delete would be for when you are totally done with all of this and you're destroying your object.

tractor fanatic
Sep 9, 2005

Pillbug
An array of vectors will be safely initialized, right?

std::vector<int> bob[5];

also if I have a POD struct with an array in it, will the assignment operator copy the array correctly?

tractor fanatic fucked around with this message at 02:11 on May 3, 2010

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe
Yes and yes.

sonic bed head
Dec 18, 2003

this is naturual, baby!

Zakalwe posted:

Unless the lines are of fixed width, there is no way to skip ahead to the nth line without first scanning through n-1 lines.



Otto Skorzeny posted:

Not C++, but google for discussion of Mark Dominus' Tie::File for some info on the difficulties that surround efficiently doing line- or record-oriented operations on files in operating systems that follow the stream-of-bytes model for files. (If you were on an old IBM mainframe with record-based files, this would be easy to do semi-efficiently from a programming perspective, although record-based files have plenty of problems of their own).

Thanks, I'll just go through the lines then I guess.

That Perl module is interesting but I sadly don't have the time to go through it. It's really not that big of a deal, I just didn't want to waste the time iterating through the lines if I didn't have to.

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh

tractor fanatic posted:

also if I have a POD struct with an array in it, will the assignment operator copy the array correctly?

Assuming it's an array and not a pointer to an array, yes.

Jose Cuervo
Aug 25, 2004
Already posted this in the general programming quesitons thread, but since it is C++ specific I figured I would post it here as well.

I want to make sure that I am using the new and delete functions in C++ correctly, because a program I have written seems to take up a lot of memory when it should not. A call to the network flow portion of my code creates 7 large arrays of long ints, which I delete at the end of the code. Thus, if the program were using 60,000K of memory before the network flow code, it would also be using 60,000K (or something close to it) after the call since the 7 large arrays that had been created have now been deleted. Instead the program uses more and more memory (about 120,000K more) after each call to the network flow code.

In my code I have:
code:
int *myIntPtr= new int;

*myIntPtr= 77;

delete myIntPtr;
and
code:
int numMachines= 10;

long int *myIntArrayPtr= new long int[numMachines];
long int* *myIntArrayPtr2= new long int*[numMachines];

for (int i=0; i<numMachines; i++)
{
   myIntArrayPtr[i]= i;
   myIntArrayPtr2[i]= new long int[numMachines];

   for (int j=0; j<numMachines; j++)
   {
      myIntArrayPtr2[i][j]= j;
   }
}

//Delete the arrays

delete [] myIntArrayPtr;

for (int i=0; i<numMachines; i++)
{
   delete [] myIntArrayPtr2[i];
}

delete [] myIntArrayPtr2;
Is my usage of the delete function not correct? Am I missing something about how pointer deletion works, and not correct about the memory usage?

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh
Your sample code looks ok (though there is really no good reason for you to manage this memory yourself). Run a leak detector on your code or something (e.g. valgrind).

Jose Cuervo
Aug 25, 2004

Avenging Dentist posted:

Your sample code looks ok (though there is really no good reason for you to manage this memory yourself). Run a leak detector on your code or something (e.g. valgrind).

I don't know how else I would manage the memory otherwise - this is what I found in the books I have been reading. Care to elaborate?

I will look into the leak detector.

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh
Well for one thing, making a 2D array by using a pointer-to-array of pointers-to-arrays is needlessly complicated, uses more memory than you need, and fragments your memory. It would make more sense to allocate space for M*N elements in one array and manually handle the array stride (this is what your compiler does for statically-sized N-D arrays): array[x*N + y].

It would be even better if you put this into its own class (or used something like boost::multi_array) since that would abstract out the memory management, which is not essential to your algorithm. The benefit of putting this stuff in a class is that the memory is automatically freed when the destructor is called (i.e. the object goes out of scope). See RAII.

You could also just use a std::vector instead of an array.

Jose Cuervo
Aug 25, 2004

Avenging Dentist posted:

Well for one thing, making a 2D array by using a pointer-to-array of pointers-to-arrays is needlessly complicated, uses more memory than you need, and fragments your memory. It would make more sense to allocate space for M*N elements in one array and manually handle the array stride (this is what your compiler does for statically-sized N-D arrays): array[x*N + y].

It would be even better if you put this into its own class (or used something like boost::multi_array) since that would abstract out the memory management, which is not essential to your algorithm. The benefit of putting this stuff in a class is that the memory is automatically freed when the destructor is called (i.e. the object goes out of scope). See RAII.

You could also just use a std::vector instead of an array.

The std::vector looks like something I can get my head around. I am going to read up on it and hopefully make my code better. Thank you.

Jose Cuervo
Aug 25, 2004
I have written a small program and built it into an .exe. It works just fine on the computer I wrote the code on which has Microsoft VS installed. But when I try to run the .exe on another computer that does not have Microsoft VS installed it will not run. Will my .exe only run on computers that have Microsoft VS installed? Or am I missing something when building my .exe?
For reference the operating systems are Windows XP (the computer it will not run on) and Windows Vista (the computer I wrote the program on).

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh
Check the OP of this thread.

Magicmat
Aug 14, 2000

I've got the worst fucking attorneys
Quick QT question (I didn't see a QT thread here, so this is the next best place, I guess.)

I'm trying to make a simple app, that has a main window with some controls on the upper half (a few buttons, and a textbox) and a QListView on the bottom half. See:
code:
 ____________________________________________
| File   Help                               |
|  ___________  ____________                |
| [Start Timer] [Stop Timer]  [_00:02:33__] |
|                                           |
|  _______________________________________  |
| |__Time 1________________00:27:19_______| |
| |__Time 2________________00:14:00_______| |
| |__Time 3________________00:05:29_______| |
| |_______________________________________| |
| |_______________________________________| |
| |_______________________________________| |                                            
|___________________________________________|
I'm trying to use a QMainWindow for my main window, but would a QDialog be better? I want to have a menu bar with options like "Save" and "Open", which I don't know if QDialog supports.

If I use a QMainWindow, I'm vexed with what to use as my central widget. Ideally I want at least the QListView widget to dynamically grow in size when the window is resized. I'd also like to have the top widget intelligently move when the window is resize, too.

If I use QtDesigner's trick of creating a blank QWidget and setting that as the central widget, then parenting all the other widgets to that QWidget, I can't figure out how to resize all my widgets with the window.

I tried using a QSplitter, with the QListView on the bottom and a QWidget at the top, and that worked with the resizing part OK, but I don't want the user to be able to move the split around like that, and plus the top half still has the same problem as using a blank QWidget as a central window.

I'm brand new to Qt, so I must be missing something obvious, but help me out here.

OddObserver
Apr 3, 2009

Magicmat posted:

Quick QT question (I didn't see a QT thread here, so this is the next best place, I guess.)

I'm trying to make a simple app, that has a main window with some controls on the upper half (a few buttons, and a textbox) and a QListView on the bottom half. See:
code:
 ____________________________________________
| File   Help                               |
|  ___________  ____________                |
| [Start Timer] [Stop Timer]  [_00:02:33__] |
|                                           |
|  _______________________________________  |
| |__Time 1________________00:27:19_______| |
| |__Time 2________________00:14:00_______| |
| |__Time 3________________00:05:29_______| |
| |_______________________________________| |
| |_______________________________________| |
| |_______________________________________| |                                            
|___________________________________________|
I tried using a QSplitter, with the QListView on the bottom and a QWidget at the top, and that worked with the resizing part OK, but I don't want the user to be able to move the split around like that, and plus the top half still has the same problem as using a blank QWidget as a central window.

I'm brand new to Qt, so I must be missing something obvious, but help me out here.

Set a QVBoxLayout as the layout for that widget.

Add a child QHBoxLayout.
Add buttons and time as child of the horizontal layout

Add the QListView.

The size would be split in the sensible way automatically.

heeen
May 14, 2005

CAT NEVER STOPS
You could also put your row of buttons into a toolbar I think.

Contero
Mar 28, 2004

What's the proper way to share member implementations between template specializations?

code:
template<int N>
struct thing {
   float m[N];

   void Hi();
};

template<>
struct thing<3> {
   union {
      float m[3];
      struct { float a,b,c; }
   };

   void Hi();
   void Also() { printf("This is only for thing<3>\n"); }
};

template<int N>
void thing<N>::Hi() 
{
   printf("Please don't type this out twice. %f\n", m[0]);
}

void thing<3>::Hi() 
{
   printf("Please don't type this out twice. %f\n", m[0]);
}

int main()
{
   thing<2> t1;
   thing<3> t2;

   t1.Hi();
   t2.Hi();

   return 0;
}
How do I combine the implementations of Hi() so that I only have to write them in one place? I'm currently implementing this by putting the implementations in another file and #including it directly in the struct, which is awful. Inheritance won't work I think because the specialization needs to union with the original array data and Hi() makes use of m[0].

Any ideas?

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh
Curiously recurring template pattern?

UraniumAnchor
May 21, 2006

Not a walrus.
http://www.parashift.com/c++-faq-lite/templates.html#faq-35.9 ?

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh

No that's for template functions. I don't think you can apply that to member functions like that.

Also yeah CRTP: http://codepad.org/9dxPTqXL

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe
You're right that there's no way to share a direct member implementation between different "patterns". If you just wanted to vary an member implementation you could specialize that member alone, but that won't get you what you want.

code:
template <int N> struct thing_rep {
  float m[N];
};

template<> struct thing_rep<3> {
  union {
    float m[3];
    struct { float a, b, c; }; // By the way, this is nonstandard.
  };
};

template <int N> struct thing : public thing_rep<N> {
  void Hi() {
    // The 'this->' is required by the standard, but gcc is inappropriately permissive here.
    printf("Please don't type this out twice. %f\n", this->m[0]);
  }
};

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh
Yeah I was actually considering pointing out that you could do it that way too. It's really just an inversion of CRTP (though CRTP is more general).

Tux Racer
Dec 24, 2005
So I am having trouble with an assignment. I am brand new at C programming and my professor decided the end of the semester would be a good time for us to learn the ins and outs of fork, wait, and exit commands. Basically there are two parts of this. The first part is to create 3 2D arrays (A, B, and C) where A is a matrix of 1s, B is a matrix of 2s, and C is a matrix of 0s. We are then supposed to multiply each element of A with its corresponding element in B and store it in C. That is supposed to happen under a unique child process and that child process is supposed to terminate once the multiplication has completed. That was to illustrate a point about how fork handles memory. I get that. To get a better idea of what I mean here is the code:

code:
#include <stdio.h>
#include <string.h>
#include <sys/wait.h>

main() {
	int w,pid,status,i,j;
	int A[4][4] = {{1,1,1,1},{1,1,1,1},{1,1,1,1},{1,1,1,1}};
	int B[4][4] = {{2,2,2,2},{2,2,2,2},{2,2,2,2},{2,2,2,2}};
	int C[4][4] = {{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0}};

	for(i=0; i<4; i++) {
		for(j=0; j<4; j++) {
			if((pid = fork()) == 0) {
				C[i][j] = A[i][j] * B[i][j];
				exit(0);
			}
		}
	}

	w = wait(&status);

	for(i=0; i<4; i++) {
		for(j=0; j<4; j++) {
			printf("%d ", C[i][j]);
		}
		printf("\n");
	}

	if(status == 0) printf("Ok\n");
	else printf("Problems\n");

	return 0;
}
What I am confused about is how to do what my professor wants us to do to get C to correctly be printed out as a matrix full of 2s. Here are his instructions:

quote:

Do the same matrix multiply but pass the values back with the status. You pass limited results back to the parent (look carefully at the documentation for exit and wait system calls, the status structure and the shift (>>) operator).

I did a man on exit and wait, but I couldn't find anything about a status structure. The shift operator I already knew. I found stuff about the status and how it is a return value on the fork method, so that is easy to pass back since it's done for me. The problem is with passing back the product. Another professor suggested that I use pipes (the prof that assigned this is out of town, didn't tell us, and isn't answering emails, wahoo!), but that's not working/I didn't do it right. Here is what I have for part 2 so far.

code:
#include <stdio.h>
#include <string.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <unistd.h>

main() {
	int w,pid,status,pipeStatus,i,j;
	int fildes[2];
	int A[4][4] = {{1,1,1,1},{1,1,1,1},{1,1,1,1},{1,1,1,1}};
	int B[4][4] = {{2,2,2,2},{2,2,2,2},{2,2,2,2},{2,2,2,2}};
	int C[4][4] = {{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0}};

	pipeStatus = pipe(fildes);
	if(pipeStatus == -1) {
		printf("Uh-oh, pipe burst.\n");
	}

	for(i=0; i<4; i++) {
		for(j=0; j<4; j++) {
			if((pid = fork()) == 0) {
				write(fildes[1], A[i][j] * B[i][j], 100);
				exit(0);
				C[i][j] = (int)fildes[0];
				printf("%d ", C[i][j]);
			}
			printf("\n");
		}
	}

	close(fildes[0]);
	close(fildes[1]);

	w = wait(&status);

	for(i=0; i<4; i++) {
		for(j=0; j<4; j++) {
			printf("%d ", C[i][j]);
		}
		printf("\n");
	}

	if(status == 0) printf("Ok\n");
	else printf("Problems\n");

	return 0;
}
Honestly I wouldn't be coming here if I weren't at such a loss. Please note I am not asking for someone to do my homework. I just need some tips/suggestions. A lot of the stuff I am finding on Google seems way more complex and difficult than what my professor has in mind. He said it would be a quick and easy assignment.

Tux Racer fucked around with this message at 06:01 on May 5, 2010

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe

Avenging Dentist posted:

Yeah I was actually considering pointing out that you could do it that way too. It's really just an inversion of CRTP (though CRTP is more general).

True enough.

Incidentally, you can explicitly specialize members of class templates. It doesn't help Contero, though, because you can't actually change the structure of the class in any way.

code:
template<> void thing<3>::Hi() {
  // My special implementation of Hi goes here.
}
You can also specialize member templates, members of member templates, etc. Just like you can't partial-specialize a function template, you can't partial-specialize a member function.

The only other restriction is that the class whose member you're specializing can't itself still be templated, i.e. you have to have specific specializations all the way down:

code:
template <int I> class A {
  template <int J> class B {
    template <int K> class C {
      template <class T> class D { ... };
    };
  };
};

template <> template <> template <> template <class T> class A<3>::B<3>::C<3>::D<T*> { ... };
That actually ends up being a pretty severe restriction and is an excellent reason to avoid complicated nested templates.

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe

Tux Racer posted:

I did a man on exit and wait, but I couldn't find anything about a status structure. The shift operator I already knew. I found stuff about the status and how it is a return value on the fork method, so that is easy to pass back since it's done for me.

Your professor wants you to do one multiplication in each child process. The child process will use its termination status to tell the parent process what the result of the multiplication is. The "status structure" is just that the status int written by wait() is "structured" in an interesting way: it conveys multiple pieces of information which are represented using different bits in the int. Look at the section of the wait() manpage about the macros WIFSIGNALED, WEXITSTATUS, etc.

pipe() is a perfectly sensible way of communicating between processes but, as it's not what your professor is asking for, I'm not sure why the other professor brought it up.

Tux Racer
Dec 24, 2005

rjmccall posted:

Your professor wants you to do one multiplication in each child process. The child process will use its termination status to tell the parent process what the result of the multiplication is. The "status structure" is just that the status int written by wait() is "structured" in an interesting way: it conveys multiple pieces of information which are represented using different bits in the int. Look at the section of the wait() manpage about the macros WIFSIGNALED, WEXITSTATUS, etc.

pipe() is a perfectly sensible way of communicating between processes but, as it's not what your professor is asking for, I'm not sure why the other professor brought it up.

So would something like C[i][j] = WEXITSTATUS(status); with whatever bitwise operation i need to do be correct? Or is there something else I am missing?

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe

Tux Racer posted:

So would something like C[i][j] = WEXITSTATUS(status); with whatever bitwise operation i need to do be correct? Or is there something else I am missing?

If you want to check whether exit() and wait() work like you think they might, make a little program and try it.

Tux Racer
Dec 24, 2005

rjmccall posted:

If you want to check whether exit() and wait() work like you think they might, make a little program and try it.

Alright thanks for your help.

Rocko Bonaparte
Mar 12, 2002

Every day is Friday!
I want to do some stuff with dynamic class loading but I'm wondering if I'm getting into a platform-specific nightmare. Generally I want to write an arbitrary amount of modules that do some operations on arrays of data. It would be nice to have this code to have access to the arrays directly, but then have a seperate mode where if I don't trust the code (mostly that it might crash), I can load it up in a seperate process, copy the data over, and see if it can handle it there. So I guess I'm also getting into an interprocess communications spat too.

I see some stuff about dynamic loading in Linux from this tutorial:
http://www.linuxjournal.com/article/3687

Will I then have to code something different for Windows and other platforms?

Note that I would expect to have to rebuild the module for each platform, just that once its built I'd like to be able to load it in after the main program has been compiled, installed, and started.

Contero
Mar 28, 2004

Avenging Dentist posted:

Curiously recurring template pattern?

This looks to be the best solution, especially since I don't have just one specialization, but about 5 or 6. It's for a matrix library I'm working on. Square matrices have their own functions (inverse, determinant) while column matrices have vector operations, and a 3x1 matrix has the usual 3d vector operations and can have its data accessed through .x .y .z etc.

rjmccall posted:

code:
template <int I> class A {
  template <int J> class B {
    template <int K> class C {
      template <class T> class D { ... };
    };
  };
};

template <> template <> template <> template <class T> class A<3>::B<3>::C<3>::D<T*> { ... };
That actually ends up being a pretty severe restriction and is an excellent reason to avoid complicated nested templates.

It's templates all the way down!

Adbot
ADBOT LOVES YOU

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh

rjmccall posted:

Incidentally, you can explicitly specialize members of class templates. It doesn't help Contero, though, because you can't actually change the structure of the class in any way.

Yeah I know, but I got sick of typing.

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