473,804 Members | 2,812 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(s truct small));
memset(s, 0, sizeof(struct small));
s->num = 42;
}

main()
{
struct big *big_thing;

big_thing = malloc(sizeof(s truct big));
memset(&big_thi ng, 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 1715
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(s truct 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(s truct small));
memset(s, 0, sizeof(struct small));
s->num = 42;
}

main()
{
struct big *big_thing;

big_thing = malloc(sizeof(s truct big));
memset(&big_thi ng, 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_thi ng, 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_thi ng, 0, sizeof big_thing);
Running and testing might identify the memset statement flaw
above. It should be:
memset(big_thin g, 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(s truct small));
if(*s)
{
memset(*s, 0, sizeof(struct small));
(*s)->num = 42;
}
}

int main(void)
{
struct big *big_thing;

big_thing = malloc(sizeof(s truct big));
if(big_thing)
{
memset(big_thin g, 0, sizeof(struct big));
f(&big_thing->small_thing) ;
if(big_thing->small_thing)
printf("big_thi ng->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_thi ng, 0, sizeof big_thing);


Running and testing might identify the memset statement flaw
above. It should be:
memset(big_thin g, 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_thin g, 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_thi ng, 0, sizeof big_thing);

Running and testing might identify the memset statement flaw
above. It should be:
memset(big_th ing, 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_thin g, 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(s truct big));
memset(&big_thi ng, 0, sizeof(struct big));


Is that correct?

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

It should be:

memset(big_thin g, 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(s truct big));
memset(&big_thi ng, 0, sizeof(struct big));


Is that correct?

I believe it should be:
memset(big_thin g, 0, sizeof *big_thing);
or
memset(big_thin g, 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
4362
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 close. The odd thing is the error accours after the mid function has done its job. but if i take out the mid function the error does not accour. i have found that if i do cout <<mid(newString.GetString(),iPosition,3);
5
1664
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
6737
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 function. Is there any general rules that tell me when to allocate memory? I thought I don't have to if it is a variable that's not a pointer, and I have to if it is. I am a bit confused about the arrays particularly. In normal situations,...
5
3679
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 == NULL ) return;
5
1911
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 be done as offsets from this. At the moment I have two data structures, a head to a linked list which in it contains the pointer to the first element in the linked list( this is redundant as far as I can work out) and the list itself. The data...
26
4821
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: (void)sprintf(ptr_testing, "This is my string"); This in itself isn't where the actual error occurs. The error occurs at any later point, in the same function where the sprintf() call is made, where I try to assign a value to one of the...
13
2486
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 multiple allocation as follows: 1. I allocated an array of pointer. 2. I Read line by line from a text file. 3. I allocated memory for the read line.
6
4161
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 try my own small program. The problem is, I seem to be having trouble with memory, i.e. sometimes my program will work and display the correct output, and sometimes it will not and display garbage (in a printf call) so i assume i have been using...
63
3914
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 How to make it compile? I also tried buf but that gives "segmentation fault". Thanks in advanced.
0
9576
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10323
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10311
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10074
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9138
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7613
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5647
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3813
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2988
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.