Segmentation fault in fopen when in write mode.


 
Thread Tools Search this Thread
Top Forums Programming Segmentation fault in fopen when in write mode.
# 15  
Old 07-17-2012
Quote:
Originally Posted by shoaibjameel123
I would agree with Corona688. I could not express this very well in my reply. My objective is to keep the values to 32bit ints only.

....
And that makes your code non-portable. The C standard specifies
Code:
int main( int, char ** )

Note that 16, 32, or 64 bit integral values are not specified - how many bits are in an "int" is an implementation-specific detail.


If you use
Code:
int32_t main( int32_t, char **)

you'll find another source of SEGVs if you specify "int32_t" on any architecture where an integer is implemented as anything other than 32 bits. In that case, the argc argument to main() will be placed on the stack as 64-bit value, say, and your code will pull it off as 32-bit value. Then value of argc your code sees will be way off, and the argv pointer value your code uses will most likely point off into la-la land.
This User Gave Thanks to achenle For This Post:
# 16  
Old 07-17-2012
Then I think using size_t or ssize_t would make codes even more portable rather than declaring as int or unsigned int.
# 17  
Old 07-17-2012
Quote:
Originally Posted by shoaibjameel123
Then I think using size_t or ssize_t would make codes even more portable rather than declaring as int or unsigned int.
Why would you even want to use size_t or ssize_t in main()? It's defined as int. Using fancy defined types isn't the point -- the point of these types isn't to use them everywhere, the point is to use them when you need them. ssize_t is defined to match certain library and system calls, but main() is not among them. You may not realize it, but main() is called by a library which assumes main() is int main(int argc, char *argv[]). Use anything else, and you're lying to that library, feeding it the wrong kind of function. It may work if you're lucky on some platforms where size_t and the like are the right size anyway, but change platforms and it could mess up extravagantly. Segmentation faults, and the like. I've experienced this personally.

In other words -- I think you missed the point of my post.

Are you doing calculations with intentional overflow, bit manipultion, or other such things where -- if your integers aren't 32/64/whatever bits, your code is guaranteed to not work?

These are the sort of circumstances int32_t, uint64_t, et all are designed for.

If you don't actually need 32 bits and nothing but 32 bits, don't demand it.

And never, ever, ever use weird types in things already defined as ordinary types, like in main(). If it works at all it's pure coincidence, and can bite you hard when you go to a radically different architecture or bit width(as I've experienced personally many times. Someone gets overenthusiastic and declares a function as size_t then goes ahead and uses it as int instead somewhere else, causing extremely hard-to-track bugs when parameters getting passed into the function get mangled...)

Last edited by Corona688; 07-17-2012 at 01:46 PM..
This User Gave Thanks to Corona688 For This Post:
# 18  
Old 07-17-2012
Yes, I would agree to keep things simple and in my future programming tasks I shall take care of this. I had never thought things could be so serious because I have never experienced it. But now it gives me a chance to experiment things on different machines and architectures. Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

C. To segmentation fault or not to segmentation fault, that is the question.

Oddities with gcc, 2.95.3 for the AMIGA and 4.2.1 for MY current OSX 10.14.1... I am creating a basic calculator for the AMIGA ADE *NIX emulator in C as it does not have one. Below are two very condensed snippets of which I have added the results inside the each code section. IMPORTANT!... (11 Replies)
Discussion started by: wisecracker
11 Replies

2. Shell Programming and Scripting

Count Segmentation fault and write to the file

Hi everyone Need to get version of npm application that have several output like this: root: nmp -version 10 root: nmp -version 10 root: nmp-new -version 3.1 root: nmp-old -version Segmentation fault count them , after that write to the file like this: 10 2 3.1 1 (1 Reply)
Discussion started by: indeed_1
1 Replies

3. Solaris

Segmentation fault

Hi Guys, I just installed and booted a zone called testzone. When I logged in remotely and tried changing to root user I get this error: "Segmentation fault" Can someone please help me resolve this? Thanks alot (2 Replies)
Discussion started by: cjashu
2 Replies

4. Programming

Using gdb, ignore beginning segmentation fault until reproduce environment segmentation fault

I use a binary name (ie polo) it gets some parameter , so for debugging normally i do this : i wrote script for watchdog my app (polo) and check every second if it's not running then start it , the problem is , if my app , remain in state of segmentation fault for a while (ie 15 ... (6 Replies)
Discussion started by: pooyair
6 Replies

5. UNIX for Dummies Questions & Answers

Segmentation fault

#include<stdio.h> #include<malloc.h> #include<unistd.h> #include<stdlib.h> void *start_1(void *argv) { printf("thread 0x%x\n",(unsigned int)pthread_self()); pthread_exit((void*)1); } void *start_2(void *argv) { printf("thread 0x%x\n",(unsigned int)pthread_self()); return (void*)2; }... (2 Replies)
Discussion started by: vincent__tse
2 Replies

6. Programming

Segmentation fault.

I'm getting a segmentation fault. I'm new to Linux programming. Thanks so much for all of your input.:eek: #include </usr/include/mysql++/mysql++.h> #include <stdio.h> #include <iostream> #include <sstream> #include <string.h> using namespace std; int outputToImport(const char*... (1 Reply)
Discussion started by: sepoto
1 Replies

7. Programming

segmentation fault

Hi, I am having this segmentation fault not in the following program, bt. in my lab program . My lab program is horrible long so cannot post it here bt. I am using the following logic in my program which is giving the segmentation fault. Bt. if I run this sample program as it is it dosen't give... (3 Replies)
Discussion started by: mind@work
3 Replies

8. AIX

Segmentation fault

Hi , During execution a backup binary i get following error "Program error 11 (Segmentation fault), saving core file in '/usr/datatools" Riyaz (2 Replies)
Discussion started by: rshaikh
2 Replies

9. Programming

Hi! segmentation fault

I have written a program which takes a directory as command line arguments and displays all the dir and files in it. I don't know why I have a problem with the /etc directory.It displays all the directories and files untill it reaches a sub directory called peers which is in /etc/ppp/peers.the... (4 Replies)
Discussion started by: vijlak
4 Replies

10. Programming

segmentation fault

sometimes for this code i get a segmentation fault for codes llike this : int main{ int * a= 0; int b; a = (int*)malloc(sizeof(int)); ///some code using these variable but no freeing of a if(a){ free(a); a = 0; } return... (3 Replies)
Discussion started by: wojtyla
3 Replies
Login or Register to Ask a Question