473,403 Members | 2,183 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,403 software developers and data experts.

error when allocating memory using a pointer inside a struct via a function

I need some help with an error I'm getting...
Sorry if this is basic stuff, but I'm still a newbie on C programming
(working up, though).
Also, I'm using gcc as my compiler ('gcc t.c -o t', no strange stuff).
Sorry about the long subject, I didn't know how to explain it better...

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:
s->num = 42;
the compiler fails with error 'request for member 'num' in something
not an structure or union', and I don't know why.

I tried this way:

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?

Thanks.

Nov 14 '05 #1
7 1688
el*******@yahoo.es wrote:
I need some help with an error I'm getting...
Sorry if this is basic stuff, but I'm still a newbie on C programming
(working up, though).
Also, I'm using gcc as my compiler ('gcc t.c -o t', no strange stuff).
Sorry about the long subject, I didn't know how to explain it better...

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;
}


s is a pointer to a pointer to a struct small, not a pointer
to a struct small. Accordingly, you want this:

*s = malloc(sizeof **s);
memset(*s, 0, sizeof **s);
(*s)->num = 42;

And you should check the return value of malloc...

-David

-David
Nov 14 '05 #2
el*******@yahoo.es wrote:
I need some help with an error I'm getting...
Sorry if this is basic stuff, but I'm still a newbie on C programming
(working up, though).
Also, I'm using gcc as my compiler ('gcc t.c -o t', no strange stuff).
Sorry about the long subject, I didn't know how to explain it better...

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:
s->num = 42;
the compiler fails with error 'request for member 'num' in something
not an structure or union', and I don't know why.

I tried this way:

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?

Thanks.


Please don't post code that is not that which you have actually
compiled. Here is a start toward fixing your code:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

struct small
{
int num;
};

struct big
{
struct small *small_thing;
};

void f(struct small **s)
{
*s = malloc(sizeof **s);
/* add code to check that *s != 0 */
memset(*s, 0, sizeof **s);
(*s)->num = 42;
}

int main(void)
{
struct big *big_thing;

big_thing = malloc(sizeof *big_thing);
/* add code to check that big_thing != 0 */
memset(&big_thing, 0, sizeof big_thing);

f(&big_thing->small_thing);
printf("%i\n", big_thing->small_thing->num);
free(big_thing->small_thing);
free(big_thing);
return 0;
}

Nov 14 '05 #3
Thanks for the help.

Nov 14 '05 #4
Martin Ambuhl wrote:

Please don't post code that is not that which you have actually
compiled. Here is a start toward fixing your code:
In addition to compiling, run and testing the code before posting can be
helpful
(but not final) in catching additional flaws. For example....
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

struct small
{
int num;
};

struct big
{
struct small *small_thing;
};

void f(struct small **s)
{
*s = malloc(sizeof **s);
/* add code to check that *s != 0 */
memset(*s, 0, sizeof **s);
(*s)->num = 42;
}

int main(void)
{
struct big *big_thing;

big_thing = malloc(sizeof *big_thing);
/* add code to check that big_thing != 0 */
memset(&big_thing, 0, sizeof big_thing);
Running and testing might identify the memset statement flaw
above. It should be:
memset(big_thing, 0 , sizeof big_thing);

And, as you suggested, code to catch dynamic memory allocations
with function malloc needs to be added to make execution of the code
safe. See the example below:
f(&big_thing->small_thing);
printf("%i\n", big_thing->small_thing->num);
free(big_thing->small_thing);
free(big_thing);
return 0;
}

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct small {
int num;
};

struct big {
struct small *small_thing;
};

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

int main(void)
{
struct big *big_thing;

big_thing = malloc(sizeof(struct big));
if(big_thing)
{
memset(big_thing, 0, sizeof(struct big));
f(&big_thing->small_thing);
if(big_thing->small_thing)
printf("big_thing->small_thing->num = %i\n",
big_thing->small_thing->num);
free(big_thing->small_thing);
}
free(big_thing);
return 0;
}

Al Bowers
Nov 14 '05 #5
Al Bowers wrote:
Martin Ambuhl wrote:

Please don't post code that is not that which you have actually
compiled. Here is a start toward fixing your code:

struct big *big_thing;

big_thing = malloc(sizeof *big_thing);

memset(&big_thing, 0, sizeof big_thing);


Running and testing might identify the memset statement flaw
above. It should be:
memset(big_thing, 0 , sizeof big_thing);


1) Running and testing isn't a great way of checking for
undefined behaviour

2) A statement: memset(foo, bar, sizeof foo); is wrong, unless
foo and &foo are the same address (ie. for an array).
It should be:

memset(big_thing, 0, sizeof *big_thing);

The OP (who has been snipped) had it correct.
IMHO, even better would be to use calloc instead of malloc.

Nov 14 '05 #6
Old Wolf wrote:
Al Bowers wrote:

Martin Ambuhl wrote:

Please don't post code that is not that which you have actually
compiled. Here is a start toward fixing your code:
struct big *big_thing;

big_thing = malloc(sizeof *big_thing);

memset(&big_thing, 0, sizeof big_thing);

Running and testing might identify the memset statement flaw
above. It should be:
memset(big_thing, 0 , sizeof big_thing);

2) A statement: memset(foo, bar, sizeof foo); is wrong, unless
foo and &foo are the same address (ie. for an array).
It should be:

memset(big_thing, 0, sizeof *big_thing);

The OP (who has been snipped) had it correct.

I don't think so. The op code was:
struct big *big_thing; big_thing = malloc(sizeof(struct big));
memset(&big_thing, 0, sizeof(struct big));


Is that correct?

I believe it should be:
memset(big_thing, 0, sizeof *big_thing);
or
memset(big_thing, 0, sizeof(struct big));
Nov 14 '05 #7
Al Bowers wrote:
Old Wolf wrote:

It should be:

memset(big_thing, 0, sizeof *big_thing);

The OP (who has been snipped) had it correct.

I don't think so. The op code was:
struct big *big_thing;
big_thing = malloc(sizeof(struct big));
memset(&big_thing, 0, sizeof(struct big));


Is that correct?

I believe it should be:
memset(big_thing, 0, sizeof *big_thing);
or
memset(big_thing, 0, sizeof(struct big));


You're right. I think we got there in the end :)

Nov 14 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Gizmo | last post by:
hello all have been trying to write a Mid() function a bit like the one in vb. i come to compile it and there are no errors however when i run it an error accours and it says the program has to...
5
by: William | last post by:
I have declared the following struct in the .h file: struct PeerInfo { int peerID; string peerIP; int peerPort; }; In the .cc file, I have:
15
by: fix | last post by:
Hi all, I am writing a program using some structs, it is not running and I believe it is because there's some memory leak - the debugger tells me that the code causes the problem is in the malloc...
5
by: Johnathan Doe | last post by:
Why is is necessary to write a function that allocates memory for, or changes, a pointer to a char array using char** instead of char*? E.g. void modify_string( char** string ) { if( string...
5
by: mikegw | last post by:
Hello all. I am currently using an implementation of sysV shared memory. The entire shared memory is allocated is one continuous block of which I get the pointer to the head, everything should...
26
by: steve | last post by:
Well I've been working all morning and have finally found the source of my "bus error (signal 10)" errors. The source is odd. The error occurs in any function where I make the function call: ...
13
by: a.zeevi | last post by:
free() multiple allocation error in C ==================================== Hi! I have written a program in C on PC with Windows 2000 in a Visual C environment. I have an error in freeing...
6
by: tgnelson85 | last post by:
Hello, C question here (running on Linux, though there should be no platform specific code). After reading through a few examples, and following one in a book, for linked lists i thought i would...
63
by: Kapteyn's Star | last post by:
Hi newsgroup The program here given is refused by GCC with a error i cannot understand. It says rnd00.c: In function ‘main’: rnd00.c:26: error: expected expression before ‘]’ token ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.