Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] Returning a Structure Pointer in C Programming Language?

Status
Not open for further replies.

xpress_embedo

Advanced Member level 4
Joined
Jul 5, 2011
Messages
1,154
Helped
161
Reputation
396
Reaction score
189
Trophy points
1,353
Location
India
Activity points
10,591
Hello!!! I am using Ubuntu 14.04 and Code::Block13.12, to write a program in C Language.


In this Program i have to return a Structure Pointer from a function.

I want to know whether the following written program is logically correct or not, as it is working properly..

Here is my Code:-


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <stdio.h>
#include <stdlib.h>
 
typedef struct
{
    unsigned char Latitude[9];
    unsigned char Longitude[10];
}LAT_LONG;
 
LAT_LONG *DisplayCoordinate(void);
 
int main()
{
    LAT_LONG *Coordinates;
    unsigned char i=0;
    Coordinates = DisplayCoordinate();    
    printf("\nLatitude = ");
    for(i=0;i<9;i++)
        printf("%c",Coordinates->Latitude[i]);
 
    printf("\nLongitude = ");
    for(i=0;i<10;i++)
        printf("%c",Coordinates->Longitude[i]);
    return 0;
}
 
LAT_LONG *DisplayCoordinate(void)
{
    LAT_LONG *Coordinates;
    Coordinates = (LAT_LONG *)malloc(sizeof(LAT_LONG));
 
    Coordinates->Latitude[0] = '6';
    Coordinates->Latitude[1] = '1';
    Coordinates->Latitude[2] = '2';
    Coordinates->Latitude[3] = '6';
    Coordinates->Latitude[4] = '.';
    Coordinates->Latitude[5] = '6';
    Coordinates->Latitude[6] = '6';
    Coordinates->Latitude[7] = '6';
    Coordinates->Latitude[8] = '6';
 
    Coordinates->Longitude[0] = '0';
    Coordinates->Longitude[1] = '1';
    Coordinates->Longitude[2] = '2';
    Coordinates->Longitude[3] = '3';
    Coordinates->Longitude[4] = '4';
    Coordinates->Longitude[5] = '.';
    Coordinates->Longitude[6] = '1';
    Coordinates->Longitude[7] = '0';
    Coordinates->Longitude[8] = '0';
    Coordinates->Longitude[9] = '0';
 
    return (Coordinates);
}



In Function DisplayCoordinate, is this line is correct.

Coordinates = (LAT_LONG *)malloc(sizeof(LAT_LONG));

I had allocated the memory but not freed it, where to write the free(Coordinates), or the compiler do it automatically.
If Compiler doesn't do it, then how can i do it what would happen if i call this function multiple times.
Is it true that each time a new memory is allocated.???
 

Hello!!! I am using Ubuntu 14.04 and Code::Block13.12, to write a program in C Language.


I had allocated the memory but not freed it, where to write the free(Coordinates), or the compiler do it automatically.
If Compiler doesn't do it, then how can i do it what would happen if i call this function multiple times.
Is it true that each time a new memory is allocated.???
1. you correctly use malloc() inside the function to create the structure and then return the prointer to main()
2. the run time system will not free it for you (like in Java where the garbage collector frees unreference objects)
3. every time you call DisplayCoordinate(); it will allocate storage on the heap and you will sooner of later run out
4. in main() once you have finished with the structure you can free it, e.g.
Code:
    for(i=0;i<10;i++)
        printf("%c",Coordinates->Longitude[i]);
    free(Coordinates);


clearly don't return a pointer to a local variable, e.g.
Code:
LAT_LONG *DisplayCoordinate(void)
{
    LAT_LONG Coordinates;
    Coordinates.Latitude[0] = '6';
    Coordinates.Latitude[1] = '1';
    Coordinates.Latitude[2] = '2';
    Coordinates.Latitude[3] = '6';
    Coordinates.Latitude[4] = '.';
    Coordinates.Latitude[5] = '6';
    Coordinates.Latitude[6] = '6';
    Coordinates.Latitude[7] = '6';
    Coordinates.Latitude[8] = '6';

    Coordinates.Longitude[0] = '0';
    Coordinates.Longitude[1] = '1';
    Coordinates.Longitude[2] = '2';
    Coordinates.Longitude[3] = '3';
    Coordinates.Longitude[4] = '4';
    Coordinates.Longitude[5] = '.';
    Coordinates.Longitude[6] = '1';
    Coordinates.Longitude[7] = '0';
    Coordinates.Longitude[8] = '0';
    Coordinates.Longitude[9] = '0';

    return (&Coordinates);
}
CodeBlocks using the gcc compiler will probably give you a warning
warning: function returns address of local variable [enabled by default]|

you can run it but you will probably will get garbage results
 
Thanks for your help.

Means i can free the Coordinates structute pointer defined in DisplayCoordinate function in main.

I got it, i was doing like that but was confused whether one can free memory allocated in one function and freeing it in other.
 

Thanks for your help.

Means i can free the Coordinates structute pointer defined in DisplayCoordinate function in main.

I got it, i was doing like that but was confused whether one can free memory allocated in one function and freeing it in other.
so long as the pointer points to a valid memory block you can call it anywhere
if it does not (you have already freed the block or pointer is corrupt) you get undefinded behaviour
failing to call free() (or delete in C++) means you can run out of heap space
 
The original code is correct except for nofreeing the allocated memory. As said, this can be done e.g. after procesiing the data in main.

There are however only few cases where using malloc in embedded application serves a reasonable purpose. In a first order I would either assihn globally static memory for LAT_LONG, or if the data is only used in a level below main, an automatic variable in the respective function. A pointer to the LAT_LONG structure would be passed to the lower functions.
 
Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top