• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Segmentation fault realloc array from a function

 
andrea casini
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello to everyone.

This is a really embarassing problem that I can't solve, probably because I don't remember very much the C language..

I give you a sample of my problem. Lets suppose I have this function "loading" which should allocate 5 elements of my vector v, The code should be like this:



Then from main I call the function


Now I tried everything malloc, realloc, using pointers and I always get that f****g segmentation fault error.

Please someone help...
 
Campbell Ritchie
Sheriff
Pie
Posts: 50289
80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try printing v with the %p tag before and after allocation. C is (like Java™) pass-by-value. Because you are not returning the pointer, you are not reassigning it.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50289
80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try . . . or
 
andrea casini
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for reply..

I know that with a function it works but I can't do it with a procedure probably it's not even possible. I tried your code for printing and this is he output:


 
Campbell Ritchie
Sheriff
Pie
Posts: 50289
80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
When I tried without the v = bit, I got (nil) for both the pointer locations. The problem is that you are not returning a pointer, so the original pointer doesn't have enough space for your array. Try what I suggested yesterday in my second post.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just as would be the case in Java, you're passing a pointer by value. When this method is called:

void loading(int *v)

, as Campbell points out, the pointer-to-int v in main() is copied into the pointer-to-int v in loading(). You assign a value to the local variable v in loading(), and the one in main() is unaffected. This is just what you'd expect in pass-by-value -- just like Java.

What you want is to pass a reference to a pointer-to-int, an easy thing to do. That would look like this:

void loading(int* &v) {

Then "v" in main() and "v" in loading() are the same variable, and when you assign to it in loading(), the variable in main() is affected. By adding that one single character to your original program, you'll find it works fine (once you fix the typos!)
 
Anand Hariharan
Rancher
Posts: 272
C++ Debian VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ernest Friedman-Hill wrote:

What you want is to pass a reference to a pointer-to-int, an easy thing to do. That would look like this:

void loading(int* &v) {

Then "v" in main() and "v" in loading() are the same variable, and when you assign to it in loading(), the variable in main() is affected. By adding that one single character to your original program, you'll find it works fine (once you fix the typos!)


That would make the code C++ and no longer C.

Also, the OP is missing include directives and since the printf calls don't include a newline, the output stream is never flushed.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anand Hariharan wrote:
That would make the code C++ and no longer C.


Fine, whatever, call it



and call it like



 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic