Using linux and g++.
This works:
stringstream ss;
for (int k = 1; k < 1000; k++){
}
This should also works but result in "segmentation fault":
for (int k = 1; k <1000; k++){
stringstream ss;
}
Why?
Thank you Antonio Perez for your reply.
Actually my code was exactly this:
#pragma pack(1)
#include <sstream>
#include <iostream>
int main(){
for (int i = 0; i < 2; i++){
std::stringstream ss;
}
}
Amazingly if I displace the #pragma pack(1) like this:
#include <sstream>
#pragma pack(1)
#include <iostream>
int main(){
for (int i = 0; i < 2; i++){
std::stringstream ss;
}
}
...then no error occurs!
Is there a possible (non-bug) reason for why sstream does not permit packing of its structure?
Related
I am trying to solve this problem on codeforces using dynamic programming. I have made the recurrence which is of O(N^2) complexity but it is timing out. I know that the complexity of this solution can be reduced to O(N) via Convex hull optimization which is explained here. But I am not able to reduce the complexity. Please help.
This is my code.
#include <bits/stdc++.h>
using namespace std;
#define MAX 100005
typedef long long ll;
ll a[MAX],b[MAX],dp[MAX];
int main()
{
int n;
cin >> n;
for(int i = 0; i < n; i++)
cin >> a[i];
for(int i = 0; i < n; i++)
cin >> b[i];
dp[0] = 0;
for(int i = 1; i < n; i++)
{
dp[i] = 1e18;
for(int j = 0; j < i; j++)
{
dp[i] = min(dp[i],dp[j] + a[i] * b[j]);
}
}
cout << dp[n-1];
}
Having difficulty to make two processes comunicate through pipe and substract a number alternatively.
Output should be like:
process1: 9
process2: 8
process1: 7...
What I've did so far:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
int main() {
int p2c[2];
int c2p[2];
int n = 9;
pipe(p2c);
pipe(c2p);
write(p2c[1], &n, sizeof(int));
if(fork() == 0) {
read(p2c[0], &n, sizeof(int));
printf("Got from parent: %d", n);
n--;
write(c2p[1], &n, sizeof(int));
close(p2c[0]);
close(p2c[1]);
close(c2p[0]);
close(c2p[1]);
exit(0);
}
else{
read(c2p[0], &n, sizeof(int));
printf("Got from child: %d", n);
n--;
write(p2c[1], &n; sizeof(int));
close(p2c[0]);
close(p2c[1]);
close(c2p[0]);
close(c2p[1]);
}
return 0;
}
Whith the output:
Got from parent:9
Got from child:8
What's the proper way to get these two processes substract the number till 0?
It makes sense that you're only getting "Got from parent:9 Got from child:8" as a result, you need, you need a while or for loop for both child and parent processes to get what you're expecting, and the stop conditions for those loops are (n < 0) after decrementing n or the write end of pipe get closed:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
int main() {
int p2c[2];
int c2p[2];
int n = 9;
pipe(p2c);
pipe(c2p);
// this is important to prevent deadlock situation, at least one of both processes
// must start a write operation before while loop, unless the read will block and
// and each process still waiting the other to write something on the pipe
write(p2c[1], &n, sizeof(int));
if(fork() == 0) {
int readStatus;
while(1){
readStatus=read(p2c[0], &n, sizeof(int));
// when read returns 0, this means the write end of pipe was closed, so we have to break the loop
// because no more data to recieve
if(readStatus == 0) break;
printf("Got from parent: %d\n", n);
n--;
// we check if n less than 0, if yes we are finished
if(n < 0) break;
write(c2p[1], &n, sizeof(int));
}
close(p2c[0]);
close(p2c[1]);
close(c2p[0]);
close(c2p[1]);
exit(0);
}
else{
int readStatus;
while(1){
readStatus= read(c2p[0], &n, sizeof(int));
if(readStatus == 0) break;
printf("Got from child: %d\n", n);
n--;
if(n < 0) break;
write(p2c[1], &n, sizeof(int));
}
close(p2c[0]);
close(p2c[1]);
close(c2p[0]);
close(c2p[1]);
}
return 0;
}
I'm doing a tutorial from this book: "Objective-C 2.0 Essentials 3rd edition" by Neil Smyth. I have tried repeatedly but keep getting the same "Expected expression" error even though the books version claims to run. I've checked way too many times and my version is exactly the same as the books. Please, can someone help me. Code below:
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[]) {
#autoreleasepool {
int x;
int j = 54321;
for (x = 0; x < 10; x++) {
}
int j = x + 10;
NSLog (#"Variable j in for loop is %i", j);
NSLog (#"Variable j outside for loop is %i", j); /* I GET AN ERROR STATING " EXPECTED EXPRESSION HERE*/
}
return 0;
}
The line
NSLog (#"Variable j outside for loop is %i", j);
contains a lot of invisible characters (UTF-8 sequence EF BF BC = OBJECT REPLACEMENT CHARACTER) between the Tab and the "NSLog".
Deleting and rewriting that line should help.
OP's code opened in hexa editor:
Format your code better; if you do the misplaced } in the code becomes obvious:
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[]) {
#autoreleasepool {
int x;
int j = 54321;
for (x = 0; x < 10; x++) {
int j = x + 10;
NSLog (#"Variable j in for loop is %i", j);
}
 NSLog (#"Variable j outside for loop is %i", j);
}
return 0;
}
EDIT The invisible characters as pointed out by #MartinR are also an issue (that I didn't notice). So there are two errors in your code.
Is there a quicker way to convert the following data into a c style pointer array?
GLfloat verticalLines [] = {
0.59, 0.66, 0.0,
0.59, -0.14, 0.0
}
My current approach is to manually iterate over the data using the method below:
-(GLfloat *)updateLineVertices{
int totalVertices = 6;
GLfloat *lineVertices = (GLfloat *)malloc(sizeof(GLfloat) * (totalVertices));
for (int i = 0; i<totalVertices; i++) {
lineVertices[i] = verticalLines[i];
}
return lineVertices;
}
Some additional info.
Ultimately I will need the data in a format which can be easily manipulated, for example:
-(void)scaleLineAnimation{
GLfloat *lineVertices = [self updateLineVertices];
for (int i = 0; i<totalVertices; i+=3) {
lineVertices[i+1] += 0.5; //scale y axis
}
}
It depends on if verticalLines is going to stick around or not. If it's defined like it is above and it's not going to change, you can forgo the whole malloc and just point lineVertices to it.
linesVertices = &verticalLines[0]
if verticalLines is going to change, you probably want your own copy, so you've got no choice but to copy the actual data from one part of memory to another as you are doing, that being said, this might be a bit more elegant
for (int i = 0; i<totalVertices; i++){
lineVertices[i] = verticalLines[i];
}
or the preferred method is probably to use memcopy(), here is some working code
//MemcopyTest.c
#include <cstdlib>
#include <stdio.h>
#include <string.h>
int main(){
float a[] = {1.0,2.0,3.0}; //Original Array
int size = sizeof(float)*3;
float *b = (float*)malloc(size); //Allocate New Array
memcpy(b, a, size); //Copy Data
for(int i = 0; i<3; i++){
printf("%f\n", b[i]);
}
free(b);
}
What's wrong with just using verticalLines directly? Anything of type T ident[n] can be implicitly converted to T* ident as if you had written &ident[0]. And if you really want to be explicit, you can just write &ident[0] directly.
I have to do some int -> byte conversion and switch to big endian for some MIDI data I'm writing. Right now, I'm doing it like:
int tempo = 500000;
char* a = (char*)&tempo;
//reverse it
inverse(a, 3);
[myMutableData appendBytes:a length:3];
and the inverse function:
void inverse(char inver_a[],int j)
{
int i,temp;
j--;
for(i=0;i<(j/2);i++)
{
temp=inver_a[i];
inver_a[i]=inver_a[j];
inver_a[j]=temp;
j--;
}
}
It works, but it's not real clean, and I don't like that I'm having to specify 3 both times (since I have the luxury of knowing how many bytes it will end up).
Is there a more convenient way I should be approaching this?
Use the Core Foundation byte swapping functions.
int32_t unswapped = 0x12345678;
int32_t swapped = CFSwapInt32HostToBig(unswapped);
char* a = (char*) &swapped;
[myMutableData appendBytes:a length:sizeof(int32_t)];
This should do the trick:
/*
Quick swap of Endian.
*/
#include <stdio.h>
int main(){
unsigned int number = 0x04030201;
char *p1, *p2;
int i;
p1 = (char *) &number;
p2 = (p1 + 3);
for (i=0; i<2; i++){
*p1 ^= *p2;
*p2 ^= *p1;
*p1 ^= *p2;
}
return 0;
}
You can pack it into a function in whatever way you want to use it. The bitwise swap should compile into some pretty neat assembly :)
Hope it helps :)
int tempo = 500000;
//reverse it
inverse(&tempo);
[myMutableData appendBytes:(char*)tempo length:sizeof(tempo)];
and the inverse function:
void inverse(int *value)
{
char inver_a = (char*)value;
int j = sizeof(*value); //or u can put 2
int i,temp;
// commenting this j--;
for(i=0;i<(j/2);i++)
{
temp=inver_a[i];
inver_a[i]=inver_a[j];
inver_a[j]=temp;
j--;
}
}