LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 02-19-2005, 09:18 AM   #1
elmafiacs
LQ Newbie
 
Registered: Apr 2004
Posts: 27

Rep: Reputation: 15
seg. fault when allocating memory via a pointer inside a struct


Why this doesn't work?

Code:
struct small {
  int num;
};

struct big {
  struct small *small_thing;
};

void f(struct small *s)
{
  s = (struct small *) malloc(sizeof(struct small));
  memset(&s, 0, sizeof(struct small));
  s->num = 42;
}

main()
{
  struct big *big_thing;

  big_thing = (struct big *) malloc(sizeof(struct big));
  memset(&big_thing, 0, sizeof(struct big));

  f(big_thing->small_thing);
  printf("%i\n", big_thing->small_thing->n);
}
Thanks in advance.

Last edited by elmafiacs; 02-20-2005 at 08:15 AM.
 
Old 02-19-2005, 09:26 AM   #2
Hivemind
Member
 
Registered: Sep 2004
Posts: 273

Rep: Reputation: 30
A few things.

1. Don't cast what malloc() returns.

2. main() returns int and the return value is not implicit.

3. in function f() the pointer is passed by value. You need f to take a struct small** for it to work as expected (and you will also have to alter the body of fand how you call it as well).

4. Post code inside [.code][./code] blocks (remove the dot).

Last edited by Hivemind; 02-19-2005 at 09:27 AM.
 
Old 02-19-2005, 09:27 AM   #3
aluser
Member
 
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 42
Quote:
void f(struct small *s)
{
s = (struct small *) malloc(sizeof(struct small));
memset(s, 0, sizeof(struct small));
s->num = 42;
}
Since variables are passed by value, this function doesn't change the value of s in the caller. All it does is leak some memory. You could write it like
Code:
struct small * f(void) {
    struct small *ret;
    ret = malloc(sizeof(struct small));
    memset(ret, 0, sizeof(struct small));
    ret->num = 42;
    return ret;
}
and say
Code:
big_think->small_thing = f();
ech, keyboard lag. other dude got it
 
Old 02-20-2005, 08:19 AM   #4
elmafiacs
LQ Newbie
 
Registered: Apr 2004
Posts: 27

Original Poster
Rep: Reputation: 15
Fixed the code...

I have fixed the code, but now when I do:
[code]
struct small {
int num;
};

struct big {
struct small *small_thing;
};

void f(struct small **s)
{
s = malloc(sizeof(struct small));
memset(s, 0, sizeof(struct small));
s->num = 42;
}

main()
{
struct big *big_thing;

big_thing = malloc(sizeof(struct big));
memset(&big_thing, 0, sizeof(struct big));

f(big_thing->small_thing);
printf("%i\n", big_thing->small_thing->n);
}
 
Old 02-20-2005, 08:26 AM   #5
elmafiacs
LQ Newbie
 
Registered: Apr 2004
Posts: 27

Original Poster
Rep: Reputation: 15
Fixed the code...

I think I have fixed the code:
Code:
struct small {
  int num;
};

struct big {
  struct small *small_thing;
};

void f(struct small **s)
{
  s = malloc(sizeof(struct small));
  memset(s, 0, sizeof(struct small));
  s->num = 42; 
}

main()
{
  struct big *big_thing;

  big_thing = malloc(sizeof(struct big));
  memset(&big_thing, 0, sizeof(struct big));

  f(&big_thing->small_thing);
  printf("%i\n", big_thing->small_thing->n);
}
But when I do:
Code:
 s->num = 42;
the compiler fails with error 'request for member 'num' in something not an structure or union'.

I tried this way:
Code:
void f(struct small **s)
{
  struct small *s_ptr;
  s_ptr->num = 42;
  memcpy(s, &s_ptr, sizeof(struct small));
}
I'm not sure, but I think that the second function is unnecessary. So, how to fix the first function?
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to pass pointer of struct to function? jinxcat Programming 2 09-01-2005 10:29 AM
struct pointer in C LuderForChrist Programming 2 01-07-2005 08:44 AM
Push into Vectors inside struct in C++ !! Hady Programming 6 05-14-2004 09:34 AM
using struct type X as pointer in struct X. worldmagic Programming 1 10-28-2003 03:06 PM
Accessing a struct inside struct cxel91a Programming 1 09-17-2003 05:24 PM


All times are GMT -5. The time now is 12:09 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration