473,326 Members | 2,124 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

C - Segmentation Fault with passing pointer into another function.

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int * fun(int * ptr1)
  5. {
  6.     *ptr1++ = 100;
  7.  
  8. //    *ptr1++ = 200;
  9. //    *ptr1++ = 300;
  10. //    *ptr1++ = 400;
  11. //    *ptr1 = 500;
  12.  
  13.    return ptr1;
  14. }
  15.  
  16. int main(void)
  17. {
  18.     int a;
  19.     int * ptr = &a;
  20.  
  21.     int * start = ptr;
  22.  
  23.     ptr = fun(ptr);
  24.  
  25.     while(start != (ptr+1)) {
  26.         printf("%d\n",*start++);
  27.     }
  28.  
  29.     return 0;
  30. }
  31.  
Why am i getting segmentation fault if i un-comment the lines in fun().....
Jul 27 '14 #1

✓ answered by weaknessforcats

The argument for the function is an int*. An int* is a variable (ptr) that contains the address of a single int. The function increment the pointer so it now (potentially_) points to memory not part of the program and you crash using the pointer again.

Incrementing an int* results in indeterminate behavior. You can protect against this by making the argument to the function const. Then you would get a compile time error rather than a run time error.

1 4812
weaknessforcats
9,208 Expert Mod 8TB
The argument for the function is an int*. An int* is a variable (ptr) that contains the address of a single int. The function increment the pointer so it now (potentially_) points to memory not part of the program and you crash using the pointer again.

Incrementing an int* results in indeterminate behavior. You can protect against this by making the argument to the function const. Then you would get a compile time error rather than a run time error.
Jul 27 '14 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Mike D. | last post by:
I have a problem with a dynamic library I am developing, but it is really more of a pointer issue than anything else. Hopefully someone here can lend me some assistance or insight into resolving...
11
by: jtagpgmr | last post by:
I am currently using the gcc compiler on a cygwin platform, I am a beginner when it comes to programming in C and want to know why anytime I run the .exe with the following code I get a...
6
by: Roman Mashak | last post by:
Hello, I belive the reason of problem is simple, but can't figure out. This is piece of code: struct timeval { long tv_sec; /* seconds */ long tv_usec; /* microseconds */ };
6
by: Wes | last post by:
I'm running FreeBSD 6.1 RELEASE #2. The program is writting in C++. The idea of the program is to open one file as input, read bytes from it, do some bitwise operations on the bytes, and then...
10
by: Rav | last post by:
I have recently started working on GCC on red Hat 9. I have encountered with some problems that i think should not occur (at least on Turbo C), here they r: Why does the following piece of code...
6
by: Kiran | last post by:
Hi all, What I am trying to do is to pass a pointer to the first element of an array to a function, modify it in that function, and then print out the values of the array (which has been modified...
2
by: danielhdez14142 | last post by:
Some time ago, I had a segment of code like vector<vector<int example; f(example); and inside f, I defined vector<int>'s and used push_back to get them inside example. I got a segmentation...
2
by: thangviet | last post by:
Hey guys, I have a function which needs to read a file and use the pointer parameter given. the pointer is a structure with five members; int points, float *time, *dhdt, *drate and *diff. The .dat...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.