#include <stdio.h>
#define N 1024
int main(){
int i, j;
int a[N][N];
int b[N][N];
for (i=0;i<N;i++){
a[i][i]=i;
b[i][i]=i;
}
for (i=0;i<N;i++)
for(j=0;j<N;j++)
{
printf("%d", a[i][j]);
printf("%d", b[i][j]);
}
return 0;
}
This program is a reason of segmentation fault, but if I define N as 1023, program will work correctly. Why it happens?
You are overflowing the stack. 2 * 1024 * 1024 * sizeof(int) is a lot for most systems.
The simplest solution would be to make the arrays static.
static int a[N][N];
static int b[N][N];
Other methods:
Make the arrays global (this is essentially the same as the above)
Use malloc in a loop and of course remember to free
int **a = malloc(N * sizeof *a);
for (i = 0; i < N; i++)
a[i] = malloc(N * sizeof *a[i]);
Related
#include <stdio.h>
#define N 1024
int main(){
int i, j;
int a[N][N];
int b[N][N];
for (i=0;i<N;i++){
a[i][i]=i;
b[i][i]=i;
}
for (i=0;i<N;i++)
for(j=0;j<N;j++)
{
printf("%d", a[i][j]);
printf("%d", b[i][j]);
}
return 0;
}
This program is a reason of segmentation fault, but if I define N as 1023, program will work correctly. Why it happens?
You are overflowing the stack. 2 * 1024 * 1024 * sizeof(int) is a lot for most systems.
The simplest solution would be to make the arrays static.
static int a[N][N];
static int b[N][N];
Other methods:
Make the arrays global (this is essentially the same as the above)
Use malloc in a loop and of course remember to free
int **a = malloc(N * sizeof *a);
for (i = 0; i < N; i++)
a[i] = malloc(N * sizeof *a[i]);
I'm still relatively new to c++. I am trying to write a program that takes an array of numbers and reverses the order of those numbers in the array with a function. The program is as follows:
#include <iostream>
using namespace std;
void reverse(int *array, int size);
int main() {
int Array[] = { 1, 2, 3, 4, 5 };
int size = sizeof(Array) / 4;
reverse(Array, size);
return 0;
}
void reverse(int *array, int size) {
int Array2[5];
for (int i = 0; i < size; i++) {
Array2[i + size] = array[i];
array[i + size] = Array2[i + size];
};
}
When I run this program, it crashes and I'm not sure why. If anyone can help my figure out why it would be much appreciated. Thank you.
Zenith has it, but there are a few points and quick hacks worth noting to help you out.
#include <iostream>
//using namespace std; don't need this, and using namespace std is overkill and often
// causes problems. It pulls in a lot of stuff that may conflict, case in point
// std::reverse now becomes reverse. Which reverse will you get? Your reverse or the standard
// library's reverse? Only pull in what you need, for example
using std::cout; // still not used, but makes a good example.
void reverse(int *array, int size)
{
// no need for the other array and another loop. You can swap each element for
//it's counterpart in the upper half of the array.
for (int i = 0; i < size /2 ; i++) // only need to go half way. Other half was
// already swapped doing the first half.
{
int temp = array[i]; // store a temporary copy of element i
array[i] = array[size-1-i]; // replace element i with it's counterpart
// from the second half of the array
array[size-1-i] = temp; // replace the counterpart of i with the copy of i
// or call std::swap(array[i], array[size-1-i]);
};
}
int main() {
int Array[] = { 1, 2, 3, 4, 5 };
// int size = sizeof(Array) / 4; using 4 here can trip you up on a computer with
// a different sized int
int size = sizeof(Array) / sizeof(Array[0]);
// dividing the size of the array by the size of an element in the array will always
// get you the correct size
reverse(Array, size);
return 0;
}
Array2[i + size]
You're accessing out-of-bounds, no matter the value of i.
You probably meant Array2[size - 1 - i] to iterate the array backwards. (size - 1 is the index of the last element.)
by using swap you will get a much nicer solution which is also more efficient
void reverse(int *array, int size) {
for (int i = 0; i < size/2; i++) {
std::swap(array[i],array[size-1-i]);
};
}
When you say int size = sizeof(Array) / 4;, size is now (5 * sizeof(int)) / 4. That's how the sizeof operator works (at least when applied to arrays).
So size is probably 5, assuming a 4-byte int. Now, you get to reverse and its argument size is also 5.
You get to the for loop, and even at the first iteration, you have Array2[5] = /* something */ and array[5] = /* something */, both of which are buffer overruns.
Also, your reverse function doesn't actually do any reversing. Try this:
void reverse(int *arr, int size);
int main()
{
int Array[] = { 1, 2, 3, 4, 5 };
int size = sizeof(Array) / sizeof(int);
reverse(Array, size);
return 0;
}
void reverse(int *arr, int size)
{
int temp[5];
for (int i = 0; i < size; i++)
temp[size - 1 - i] = arr[i];
for (int i = 0; i < size; i++)
arr[i] = temp[i];
}
I tried to program a merge sort function using only static arrays and of course I have an incomprehensible error.
So the first question is that when I compile my program it gives me a multiple definition error on line 9 of that code:
#include <cmath>
#include "include\sort.h"
/*
Theses fonctions accept only arrays of unsigned short int with a length in unsigned int
*/
unsigned short int* sortByFusion(unsigned short int arr[]) { // HERE !!!!!!!!
const unsigned int arrSize = sizeof(arr)/sizeof(unsigned short int);
if (arrSize <= 1) {return arr;}
/*
Declarations and initializations of the two half array
*/
const unsigned int arrLeftSize = static_cast<unsigned int>(floor(arrSize/2));
const unsigned int arrRightSize = static_cast<unsigned int>(arrSize - arrLeftSize);
unsigned short int arrLeft[arrLeftSize];
unsigned short int arrRight[arrRightSize];
for (unsigned int i = 0; i < arrLeftSize; i ++)
arrLeft[i] = arr[i];
for (unsigned int i = 0; i < arrRightSize; i ++)
arrRight[i] = arr[i + arrLeftSize];
/*
Sort the two arrays
*/
for (unsigned int i = 0; i < arrLeftSize; i ++)
arrLeft[i] = *(sortByFusion(arrLeft) + i);
for (unsigned int i = 0; i < arrRightSize; i ++)
arrRight[i] = *(sortByFusion(arrRight) + i);
/*
And fusion them
*/
unsigned int i(0), j(0);
for (unsigned int k = 0; k < arrSize; k ++) {
if (i >= arrLeftSize) {arr[k] = arrRight[j]; j ++;} //That line
else if (j >= arrRightSize) {arr[k] = arrLeft[i]; i ++;} //And that line are here to avoid segmentation fault
else {
if (arrLeft[i] <= arrRight[j]) {arr[k] = arrLeft[i]; i ++;}
else {arr[k] = arrRight[j]; j ++;}
}
}
return arr;
}
What did I do wrong? I tried to put some ifndef define endif but it did nothing more. It seems that everybody has a problem with multiple definition always a bit different on this forum.
Secondly, I used some static_cast but why does floor return a double when we pass a double as argument? Logically, it should give us an integer (the floor of a number is always an integer ...)?
For the compiler, it is GNU GCC, but I don't know how to find its version. And I work with Code::Blocks.
And this is the header file :
#ifndef SORT_H_INCLUDED
#define SORT_H_INCLUDED
unsigned short int* sortByFusion(unsigned short int arr[]);
#endif // SORT_H_INCLUDED
const unsigned int arrSize = sizeof(arr)/sizeof(unsigned short int);
This line is not working as expected, because c++ doesn't keep any information about the length of an array with a runtime array - which is really just a pointer to the array, thus sizeof(arr) returns sizeof(unsigned short int*) which is the size of a pointer.
As why there is an error in line 9, I can't help you without seeing the header sort.h.
//static_cast<unsigned int>(floor(arrSize/2)); // Wrong
arrSize/2; // Right, C++ does floor integer divisions.
unsigned short int arrLeft[arrLeftSize];
unsigned short int arrRight[arrRightSize];
These two lines aren't valid C++ because the length of the declared arrays can not be determined at compile time.
I was messing with a toy program for cuda.
I declare a float array transfer that to gpu and a number to each element of that float array and transfer it back to the host system and print the array. However this is not working out and it is giving me segmentation fault.
Here's code
#include <iostream>
using namespace std;
__global__ void kern(float *a, float *C){
for (int i = 0; i < 3; i++) C[i] = a[i] + i;
}
int main(){
float *A = new float[3];
for(int i = 0; i < 3; i++){
A[i] = i;
}
float * d;
float * C;
cudaMalloc(&C, sizeof(float)*3);
cudaMalloc(&d, sizeof(float)*3);
cudaMemcpy(&d, A, sizeof(float)*3, cudaMemcpyHostToDevice);
kern<<<1, 1>>>(d, C);
cudaMemcpy(&A, C, sizeof(float)*3, cudaMemcpyDeviceToHost);
cout << A[2];
}
Also I am not familiar with Malloc most of my experience was with cpp and hence I am more comfortable with new datatype[]; is there a equivalent for Cuda?
Change this to:
cudaMemcpy(&d, A, sizeof(float)*3, cudaMemcpyHostToDevice);
cudaMemcpy(&A, C, sizeof(float)*3, cudaMemcpyDeviceToHost);
To this:
cudaMemcpy(d, A, sizeof(float)*3, cudaMemcpyHostToDevice);
cudaMemcpy(A, C, sizeof(float)*3, cudaMemcpyDeviceToHost);
Also it's always better to store return code by CUDA calls they will give you better idea what going wrong.
this is my code
#include "stdafx.h"
#include <iostream>
using namespace std;
#define n 10
__device__ int glMem[n];
__global__ void initVals()
{
for(int i=0;i<n;i++)
glMem[i] = 0;
}
__global__ void test(int *out)
{
for(int i=0;i<n;i++)
out[i] = 10;
}
int main()
{
const size_t sz = size_t(n)*sizeof(int);
initVals<<<1,1>>>();
int *devMem;
cudaMalloc((void **)&devMem, sz);
test<<<1, 1>>>(devMem);
int *hoMem=new int[n];
cudaMemcpy(hoMem, devMem,sz, cudaMemcpyDeviceToHost);
//print
for(int i=0;i<n;i++)
cout<<hoMem[i]<<endl;
return 0;
}
IN this code I define
glMem
to size n. If I dont know the size earlier hw can I define??
for example I need to define like this.
__device__ int *glMem;
It doesnt work. Please give some code sample..
In that case you need to allocate the memory into the device.
// size of data
unsigned int size_of_glMem = n * sizeof(int);
// allocate device memory for result
int* glMem = NULL;
cudaMalloc( (void**) &glMem, size_of_glMem );
Hope this help.