linux - Segmentation fault due to lack of memory in C


This code gives me segmentation fault about 1/2 of the time:

int main(int argc, char **argv) {
    float test[2619560];
    int i;
    for(i = 0; i < 2619560; i++)
        test[i] = 1.0f;

I actually need to allocate a much larger array, is there some way of allowing the operating system to allow me get more memory?

I am using Linux Ubuntu 9.10

4 Answers: 

You are overflowing the default maximum stack size, which is 8 MB.

You can either increase the stack size - eg. for 32 MB:

ulimit -s 32767

... or you can switch to allocation with malloc:

float *test = malloc(2619560 * sizeof test[0]);

Right now you're allocating (or at least trying to) 2619560*sizeof(float) bytes on the stack. At least in most typical cases, the stack can use only a limited amount of memory. You might try defining it static instead:

static float test[2619560];

This gets it out of the stack, so it can typically use any available memory instead. In other functions, defining something as static changes the semantics, but in the case of main it makes little difference (other than the mostly theoretical possibility of a recursive main).


Don't put such a large object on the stack. Instead, consider storing it in the heap, by allocation with malloc() or its friends.

2.6M floats isn't that many, and even on a 32-bit system you should be ok for address space.

If you need to allocate a very large array, be sure to use a 64-bit system (assuming you have enough memory!). 32-bit systems can only address about 3G per process, and even then you can't allocate it all as a single contigous block.


It is the stack overflower. You'd better to use malloc function to get memory larger than stack size which you can get it from "ulimit -s".


More Articles

shell - Move output of Find command unix

Im learning unix programming , i want to find all files whose size is greater than 1M an print them into a is my code find. -size +1M -print0 | xargs -I -O '{}' mv '{}' files all sites i have found refer to this one as right one , but it does not work . currently im working on ubuntu

php - Ubuntu Server Virtual Host not redirecting properly

I have installed Web site on my Ubuntu Server. The following things are installed on my Server for running web service. 1) Apache22) PHP53) MySql My main website is redirected to https.Example (changed for security purpose) <VirtualHost *.80>ServerName www.example.comServerAlias

shell - UNIX: copy entire directory structure and only specific file type

How i can recreate entire directory structure and copy only files with .txt extension if present. I would like to copy the directory or sub-directory even if its empty.

How do I insert PHP variables in a MySQL table?

This question already has an answer here: Reference: What is variable scope, which variables are accessible from where and what are “undefined variable” errors? 3 answers

node.js - Ubuntu + Nodejs + curl: (52) Empty reply from server

I have a nodejs process listening a port on my server:app.get('/statistic', function(req, res) { log_get_connection(req); getStatistic().then(statistic => { res.json(statistic); });});When I try to access it via curl being on the same machine:curl "http://127.0

Unable to install LAMP (Linux, Apache, Mysql, PHP stack) on Ubuntu 16.04

I am trying to install LAMP server on my Ubuntu desktop. The problem I am facing is related to IP address of my laptop.While Setting Global ServerName to Suppress Syntax Warnings, as the documentation says here, when I am adding a ServerName directive, pointing to my server's public IP address in co

linux - filter out content between two files

I have two files conditions.txtabcdefghlogs.txtefghijklmnopqrstI am expecting output to be:ijklmnopqrstActual output:efghijklijklmnopmnopqrstqrstHere's the code I had worked till nowfunc(){while read conditiondoif [[ $line = $condition ]] ; then:elseecho "$line";done < condition.txt}while read li

c - Why do I get a seg fault on Ubuntu but not mac?

I have a program that checks the modification time of a file and executes the file if it has changed. Currently it works if I run it on my mac, but it seg faults if I run it on ubuntu. Please help me.note: this is in c#include <unistd.h>#include <stdlib.h>#include <stdio.h>#inclu

php - Page not found error on apache server on ubuntu 14.04

I have a project which i developed using WAMP. The project works fine as expected. I tried to use the same project on LAMP stack (Ubuntu 14.04). I imported the database to mysql server and then copied the files to /var/www/html folder and gave permissions using chmod to all files in www folder. When

linux - Run a intotify sricpt in background

how can i run 12 bash scripts simultaniously in the backgroud?That's the script:#/bin/bashwhile inotifywait -e close_write /var/www/html/wp-content/QM-Handbuch /01-Inhaltsverzeichnis.doc; do soffice --headless --convert-to pdf /var/www/html/wp-content/QM-Handbuch/01-Inhaltsverzeichnis.doc &