473,320 Members | 1,856 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,320 software developers and data experts.

Best way of Allocating and Deallocating memory

Dear Friends

I have two structures as below
typedef struct {
long_int length;
char data[1];
} CI_STRUCT_DATA;

typedef CI_STRUCT_DATA *ptr_CiStructData;

typedef struct {
char opener_ref_num_fmtd[26];
char our_ref_num[15];
char txn_ref_num[15];
char stop_tracer_date[27];
char tracer_trans_medium[3];
char tracer_frequency[2];
ptr_CiStructData MSG_DATA;
} CI_STRUCT_MSG;

I have allocated memory for it as below
CI_STRUCT_MSG msg_details;
msg_details.MSG_DATA = (CI_STRUCT_DATA *)
malloc( sizeof(CI_STRUCT_DATA) +(sizeof(unsigned
char)*(strlen(ptr_msg_details) + 1)) );
msg_details.MSG_DATA->length = strlen(ptr_msg_details);
strcpy((char *)msg_details.MSG_DATA->data,(char *)ptr_msg_details);

Im freeing up the memory as
free(ptr_msg_details);

Please suggest me any improvements in memory allocations if any

Thanks
Amit

Sep 17 '07 #1
6 1779
On 17 Sep, 11:48, Amit_Basnak <Amit.Bas...@gmail.comwrote:
Dear Friends

I have two structures as below
typedef struct {
long_int length;
char data[1];

} CI_STRUCT_DATA;

typedef CI_STRUCT_DATA *ptr_CiStructData;

typedef struct {
char opener_ref_num_fmtd[26];
char our_ref_num[15];
char txn_ref_num[15];
char stop_tracer_date[27];
char tracer_trans_medium[3];
char tracer_frequency[2];
ptr_CiStructData MSG_DATA;

} CI_STRUCT_MSG;

I have allocated memory for it as below
CI_STRUCT_MSG msg_details;
msg_details.MSG_DATA = (CI_STRUCT_DATA *)
malloc( sizeof(CI_STRUCT_DATA) +(sizeof(unsigned
char)*(strlen(ptr_msg_details) + 1)) );
msg_details.MSG_DATA->length = strlen(ptr_msg_details);
strcpy((char *)msg_details.MSG_DATA->data,(char *)ptr_msg_details);

Im freeing up the memory as
free(ptr_msg_details);

Please suggest me any improvements in memory allocations if any

Thanks
Amit
I tend to use classes over structs for most things. But anyway, I
think new and delete are the prefered methods for memory allocation in
C++. You don't need to provide the size parameter, and new returns a
pointer to the object your creating.

I would do something like:

class CIData
{
public:
CIData();
~CIData();

const long_int getLength() const;
const char* getData() const;

void setLength(long_int len);
void setData(const char*);

private:
long_int m_Length;
char[1] m_Data;
};

class CIMsg
{
public:
CIMsg();
~CIMsg();

// Setters
void setMsgData(CIData *);
// .....

// Getters
// ......

private:
char opener_ref_num_fmtd[26];
char our_ref_num[15];
char txn_ref_num[15];
char stop_tracer_date[27];
char tracer_trans_medium[3];
char tracer_frequency[2];
const CIData * m_msgData;
}
int main(int argc, char** argv)
{
const CIMsg msgDetails;
const CIData data = new CIData();

msgDetails.setMsgData(CIData);

if(msgDetails)
{
// do some stuff

...

delete msgDetails;
msgDetails = NULL;
}

return 0;
}
Sep 17 '07 #2
Amit_Basnak wrote:
Dear Friends

I have two structures as below
typedef struct {
long_int length;
char data[1];
} CI_STRUCT_DATA;

typedef CI_STRUCT_DATA *ptr_CiStructData;

typedef struct {
char opener_ref_num_fmtd[26];
char our_ref_num[15];
char txn_ref_num[15];
char stop_tracer_date[27];
char tracer_trans_medium[3];
char tracer_frequency[2];
ptr_CiStructData MSG_DATA;
} CI_STRUCT_MSG;

I have allocated memory for it as below
CI_STRUCT_MSG msg_details;
msg_details.MSG_DATA = (CI_STRUCT_DATA *)
malloc( sizeof(CI_STRUCT_DATA) +(sizeof(unsigned
char)*(strlen(ptr_msg_details) + 1)) );
msg_details.MSG_DATA->length = strlen(ptr_msg_details);
strcpy((char *)msg_details.MSG_DATA->data,(char *)ptr_msg_details);

Im freeing up the memory as
free(ptr_msg_details);

Please suggest me any improvements in memory allocations if any
please format your code before post, it's so hard to read

--
Thanks
Barry
Sep 17 '07 #3
On 2007-09-17 12:48, Amit_Basnak wrote:
Dear Friends

I have two structures as below
typedef struct {
long_int length;
char data[1];
} CI_STRUCT_DATA;
Notice that char data[1] is an array with one element, which is the same
as a normal char, then later your treat it as if it was a char pointer.
This is *very* bad, a char is 1 byte, a pointer is probably at least 4
times that big, which means that you are trying to write to memory
outside of the struct. Also, you use a type long_int, is that a typedef
for a long? Further more, typedefs with the structs are not needed in
C++ and can be omitted. Lastly, all upper-case names are generally
reserved for macros.

struct CiData {
long length;
char* data;
};

Note that length should probably be unsigned, and a normal int is
probably enough.
>
typedef CI_STRUCT_DATA *ptr_CiStructData;

typedef struct {
char opener_ref_num_fmtd[26];
char our_ref_num[15];
char txn_ref_num[15];
char stop_tracer_date[27];
char tracer_trans_medium[3];
char tracer_frequency[2];
ptr_CiStructData MSG_DATA;
} CI_STRUCT_MSG;
struct CiMsg {
char opener_ref_num_fmtd[26];
char our_ref_num[15];
char txn_ref_num[15];
char stop_tracer_date[27];
char tracer_trans_medium[3];
char tracer_frequency[2];
CiData* MSG_DATA;
};

Much of the above could probably better be represented by some other
format, such as integers instead of char arrays.
I have allocated memory for it as below
CI_STRUCT_MSG msg_details;
msg_details.MSG_DATA = (CI_STRUCT_DATA *)
malloc( sizeof(CI_STRUCT_DATA) +(sizeof(unsigned
char)*(strlen(ptr_msg_details) + 1)) );
msg_details.MSG_DATA->length = strlen(ptr_msg_details);
strcpy((char *)msg_details.MSG_DATA->data,(char *)ptr_msg_details);

Im freeing up the memory as
free(ptr_msg_details);

Please suggest me any improvements in memory allocations if any
Using new:

CiMsg msg_details;
msg_details.MSG_DATA = new CiData;
msg_details.MSG_DATA->data = new char[strlen(ptr_msg_details) + 1];
strcpy(msg_details.MSG_DATA->data, ptr_msg_details);

To free the memory use delete:

delete msg_details.MSG_DATA->data;
detele msg_details.MSG_DATA;

An even better solution would be to use std::string
struct CiMsg {
char opener_ref_num_fmtd[26];
char our_ref_num[15];
char txn_ref_num[15];
char stop_tracer_date[27];
char tracer_trans_medium[3];
char tracer_frequency[2];
std::string data;
};

// ...

CiMsg msg_details;
msg_details.assign(ptr_msg_details, strlen(ptr_msg_details));

Of course, if ptr_msg_details was also pointing to a string it would be
even simpler

CiMsg msg_details;
msg_details.data = *ptr_msg_data;

--
Erik Wikström
Sep 17 '07 #4
On Sep 17, 4:13 pm, DaveJ <davej2...@gmail.comwrote:
On 17 Sep, 11:48, Amit_Basnak <Amit.Bas...@gmail.comwrote:


Dear Friends
I have two structures as below
typedef struct {
long_int length;
char data[1];
} CI_STRUCT_DATA;
typedef CI_STRUCT_DATA *ptr_CiStructData;
typedef struct {
char opener_ref_num_fmtd[26];
char our_ref_num[15];
char txn_ref_num[15];
char stop_tracer_date[27];
char tracer_trans_medium[3];
char tracer_frequency[2];
ptr_CiStructData MSG_DATA;
} CI_STRUCT_MSG;
I have allocated memory for it as below
CI_STRUCT_MSG msg_details;
msg_details.MSG_DATA = (CI_STRUCT_DATA *)
malloc( sizeof(CI_STRUCT_DATA) +(sizeof(unsigned
char)*(strlen(ptr_msg_details) + 1)) );
msg_details.MSG_DATA->length = strlen(ptr_msg_details);
strcpy((char *)msg_details.MSG_DATA->data,(char *)ptr_msg_details);
Im freeing up the memory as
free(ptr_msg_details);
Please suggest me any improvements in memory allocations if any
Thanks
Amit

I tend to use classes over structs for most things. But anyway, I
think new and delete are the prefered methods for memory allocation in
C++. You don't need to provide the size parameter, and new returns a
pointer to the object your creating.

I would do something like:

class CIData
{
public:
CIData();
~CIData();

const long_int getLength() const;
const char* getData() const;

void setLength(long_int len);
void setData(const char*);

private:
long_int m_Length;
char[1] m_Data;

};

class CIMsg
{
public:
CIMsg();
~CIMsg();

// Setters
void setMsgData(CIData *);
// .....

// Getters
// ......

private:
char opener_ref_num_fmtd[26];
char our_ref_num[15];
char txn_ref_num[15];
char stop_tracer_date[27];
char tracer_trans_medium[3];
char tracer_frequency[2];
const CIData * m_msgData;

}

int main(int argc, char** argv)
{
const CIMsg msgDetails;
const CIData data = new CIData();

msgDetails.setMsgData(CIData);

if(msgDetails)
{
// do some stuff

...

delete msgDetails;
msgDetails = NULL;
}

return 0;

}- Hide quoted text -

- Show quoted text -- Hide quoted text -

- Show quoted text -

Thanks all

Since its mandator for us to use C style malloc
thats why I tried to allocate the memory like
CI_STRUCT_MSG msg_details;
msg_details.MSG_DATA = (CI_STRUCT_DATA *)
malloc( sizeof(CI_STRUCT_DATA) +(sizeof(unsigned
char)*(strlen(ptr_msg_details) + 1)) );
msg_details.MSG_DATA->length = strlen(ptr_msg_details);
strcpy((char *)msg_details.MSG_DATA->data,(char
*)ptr_msg_details);

Now for freeing it up
free(msg_details.MSG_DATA);
Is above sufficient ?

Please let me know
Thanks

Sep 17 '07 #5
On Sep 17, 12:48 pm, Amit_Basnak <Amit.Bas...@gmail.comwrote:
I have two structures as below
typedef struct {
long_int length;
char data[1];
} CI_STRUCT_DATA;
This looks like C. In C++, you wouldn't use a typedef here (and
in both languages, all caps is traditionally reserved for
macros).
typedef CI_STRUCT_DATA *ptr_CiStructData;
typedef struct {
char opener_ref_num_fmtd[26];
char our_ref_num[15];
char txn_ref_num[15];
char stop_tracer_date[27];
char tracer_trans_medium[3];
char tracer_frequency[2];
ptr_CiStructData MSG_DATA;
} CI_STRUCT_MSG;
I have allocated memory for it as below
CI_STRUCT_MSG msg_details;
msg_details.MSG_DATA = (CI_STRUCT_DATA *)
malloc( sizeof(CI_STRUCT_DATA) +(sizeof(unsigned
char)*(strlen(ptr_msg_details) + 1)) );
I can't figure this line out at all. What's ptr_msg_details?
msg_details.MSG_DATA->length = strlen(ptr_msg_details);
strcpy((char *)msg_details.MSG_DATA->data,(char *)ptr_msg_details);
This last line is undefined behavior (in both C and C++) if
ptr_msg_details points to anything but an empty string. You've
declared CI_STRUCT_DATA::data as a char[1].
Im freeing up the memory as
free(ptr_msg_details);
Please suggest me any improvements in memory allocations if
any.
Without knowing what the structs are used for, it's impossible
to say. The code you have above, however, is not legal C++, nor
legal C.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Sep 18 '07 #6
On Sep 17, 1:29 pm, Erik Wikström <Erik-wikst...@telia.comwrote:
On 2007-09-17 12:48, Amit_Basnak wrote:
I have two structures as below
typedef struct {
long_int length;
char data[1];
} CI_STRUCT_DATA;
Notice that char data[1] is an array with one element, which
is the same as a normal char, then later your treat it as if
it was a char pointer.
Actually, he treats it as if it were a char[], a VLA. C++ (like
C90) doesn't have VLA's, and the above isn't a VLA in C99
either; he'd have to declare it "char data[];".
This is *very* bad, a char is 1 byte, a pointer is probably at
least 4 times that big, which means that you are trying to
write to memory outside of the struct. Also, you use a type
long_int, is that a typedef for a long? Further more, typedefs
with the structs are not needed in C++ and can be omitted.
Lastly, all upper-case names are generally reserved for
macros.
struct CiData {
long length;
char* data;
};
Note that length should probably be unsigned, and a normal int
is probably enough.
Unsigned is a bit special in C++, and in general, plain int
should be used as the default type, unless there are overriding
reasons for some other type. (One overriding reason is that
comparisons between signed and unsigned are sometimes confusing,
so if unsigned is imposed on you elsewhere---e.g. by the STL---,
then you probably want to conform to the type used there---in
the case of the STL, size_t.)

In C++, of course, the correct way to write the above would be:

typedef std::vector< char CiData ;

Which solves all of his problems in one fell swoop.

[...]
Please suggest me any improvements in memory allocations if any
Using new:
CiMsg msg_details;
msg_details.MSG_DATA = new CiData;
msg_details.MSG_DATA->data = new char[strlen(ptr_msg_details) + 1];
strcpy(msg_details.MSG_DATA->data, ptr_msg_details);
To free the memory use delete:
delete msg_details.MSG_DATA->data;
detele msg_details.MSG_DATA;
An even better solution would be to use std::string
Or std::vector< char >. I sort of suspect that he's trying to
format a message for transmission. In such cases, the
functionality of std::vector< char is often more appropriate
than that of std::string.

But of course, I'm just guessing as to the use. All I really
know is that the code he posted looks more like C than C++, and
that it has undefined behavior in both languages.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Sep 18 '07 #7

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

Similar topics

5
by: AMT2K5 | last post by:
If I have the class class IOLabel : public IOField { private: int len; char format; public: IOLabel(int row, int col, int len):IOField(row, col)
4
by: Sameer | last post by:
Hello Group, This is one problem in programming that is troubling me. there is a segmentation fault just before creating memory to a structure ..i.e, just after the "allocating memory "...
6
by: Peter Hickman | last post by:
I have a program that requires x strings all of y length. x will be in the range of 100-10000 whereas the strings will all be < 200 each. This does not need to be grown once it has been created....
5
by: Philippe Bertrand | last post by:
Using C#, I want to send a byte array to an unmanaged function with the minimum amount of copies. The array is input only and won't be modified (its copied on the unmanaged side). I'm currently...
3
by: Tod Birdsall | last post by:
Hi All, The organization I am working for has created a new corporate website that used Microsoft's Pet Shop website as their coding model, and dynamically served up content, but cached each...
35
by: Alex Martelli | last post by:
Having fixed a memory leak (not the leak of a Python reference, some other stuff I wasn't properly freeing in certain cases) in a C-coded extension I maintain, I need a way to test that the leak is...
4
by: Dmytro Bablinyuk | last post by:
I came across several possible ways of allocating memory for objects, for example: 1. malloc(sizeof(T)*3)/free - raw memory 2. new T/delete - buffer would be initialized to...
2
by: Sheldon | last post by:
Hi, Can someone tell me how to remove the conflicts here ? #include <stdlib> static char*** Memory2DStr(int R, int C); static void MemoryFreeStr(int C, char*** array); void main() {...
8
by: Chris Portka | last post by:
I need to be able to allocate large numbers of elements at a time but then delete them one at a time. I have settled on the design of using new to allocate many items at once, but am unsure what...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.