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

deallocating char** (double pointer) properly.

2
hi there, i'm trying to get this get to work but after some clicks (the procedure activates after a click) some text gets messed up, it is some memory mess i guess. anyway take a look, tell me if there's anything wrong (i'm using C++Builder).
void __fastcall TForm1::btnTraceClick(TObject *Sender)
{
String options;
//options = Application->ExeName;
options = "KanedaX";
int MaxWidth=0,i,argc=1;
char **argv=NULL;
char *options_c,*str;

lstboxFiles->Clear();
//OPTIONS
if (comboFormat->Items->Strings[comboFormat->ItemIndex] != "SVG") {
options += "|-b|" + comboFormat->Items->Strings[comboFormat->ItemIndex];
argc += 2;
}
if (!editOutput->Text.IsEmpty()) {
options += "|-o|" + editOutput->Text;
argc += 2;
}
//DONE with OPTIONS
//copy String options -> char *options_c
options_c = (char *)malloc(options.Length()+1);
strcpy(options_c,options.c_str());
//start tokenizing
str = strtok(options_c,"|");
//memory for array of char *
argv = (char**)malloc(argc);
for (i=0; i<argc; ++i) {
//copy each str to argv[i]
argv[i] = (char *)malloc(strlen(str)+1);
strcpy(argv[i],str);
//free mem for both str & options_c (first portion)
// free(str);
//continue tokenizing
str = strtok(NULL,"|");
}
free(options_c);
//show argv on ListBox
for (i=0; i<argc; ++i) {
if (MaxWidth < Canvas->TextWidth(argv[i]))
MaxWidth = Canvas->TextWidth(argv[i]);
lstboxFiles->Items->Add(argv[i]);
}
//Free mem. for argv
for (i=0; i<argc; ++i) {
free(argv[i]);
argv[i] = NULL;
}

//fix it: memory doesn't get all freed.
// can't figure out why.

//Adjust Width
SendMessage(lstboxFiles->Handle,LB_SETHORIZONTALEXTENT,
MaxWidth + 5, 0);
}
Jun 26 '07 #1
4 6767
gpraghuram
1,275 Expert 1GB
hi there, i'm trying to get this get to work but after some clicks (the procedure activates after a click) some text gets messed up, it is some memory mess i guess. anyway take a look, tell me if there's anything wrong (i'm using C++Builder).
void __fastcall TForm1::btnTraceClick(TObject *Sender)
{
String options;
//options = Application->ExeName;
options = "KanedaX";
int MaxWidth=0,i,argc=1;
char **argv=NULL;
char *options_c,*str;

lstboxFiles->Clear();
//OPTIONS
if (comboFormat->Items->Strings[comboFormat->ItemIndex] != "SVG") {
options += "|-b|" + comboFormat->Items->Strings[comboFormat->ItemIndex];
argc += 2;
}
if (!editOutput->Text.IsEmpty()) {
options += "|-o|" + editOutput->Text;
argc += 2;
}
//DONE with OPTIONS
//copy String options -> char *options_c
options_c = (char *)malloc(options.Length()+1);
strcpy(options_c,options.c_str());
//start tokenizing
str = strtok(options_c,"|");
//memory for array of char *
argv = (char**)malloc(argc);
for (i=0; i<argc; ++i) {
//copy each str to argv[i]
argv[i] = (char *)malloc(strlen(str)+1);
strcpy(argv[i],str);
//free mem for both str & options_c (first portion)
// free(str);
//continue tokenizing
str = strtok(NULL,"|");
}
free(options_c);
//show argv on ListBox
for (i=0; i<argc; ++i) {
if (MaxWidth < Canvas->TextWidth(argv[i]))
MaxWidth = Canvas->TextWidth(argv[i]);
lstboxFiles->Items->Add(argv[i]);
}
//Free mem. for argv
for (i=0; i<argc; ++i) {
free(argv[i]);
argv[i] = NULL;
}

//fix it: memory doesn't get all freed.
// can't figure out why.

//Adjust Width
SendMessage(lstboxFiles->Handle,LB_SETHORIZONTALEXTENT,
MaxWidth + 5, 0);
}
Hi,
You havent freed the following memory
Expand|Select|Wrap|Line Numbers
  1. argv = (char**)malloc(argc); // This is not freed
  2. for (i=0; i<argc; ++i) {
  3.    free(argv[i]);
  4.    argv[i] = NULL;
  5.  }
  6. free argv; //add this line to free the argv memory
  7.  
Raghuram
Jun 26 '07 #2
Hi,
You havent freed the following memory
Expand|Select|Wrap|Line Numbers
  1. argv = (char**)malloc(argc); // This is not freed
  2. for (i=0; i<argc; ++i) {
  3.    free(argv[i]);
  4.    argv[i] = NULL;
  5.  }
  6. free argv; //add this line to free the argv memory
  7.  
Raghuram

this is how you allocate memory
Expand|Select|Wrap|Line Numbers
  1.          variable = new char*[X];
  2.  
  3.      for( i = 0; i <  X + 1; ++i)
  4.         variable[i] = new char[Y];
Jun 26 '07 #3
Xegros
2
Thanks to everybody for the help. But you know what, while i was sleeping (i've just had a 2 hour nap) i had an illumination. I realized that that i had to do this :

argv = (char**)malloc(argc*4);

instead of just this :

argv = (char**)malloc(argc);

Man i can't believe it was basic stuff. One of you said i forgot to free the memory for "argv", well i actually didn't forget, desperate as i was i started testing with everything i could.

Anyway thanks to everybody for the quick reply!
Jun 26 '07 #4
weaknessforcats
9,208 Expert Mod 8TB
Everyone is making this too hard. There are no multi-dimensional arrays in C or C++. All there are are one dimensional arrays. The first index specifies the number of elements:

Code: ( c )
int arr[3]; //has 3 elements. Each element an int
int arr[3][7]; //has 3 elements. Each element an array of 7 int
int arr[3][4][5]; //has 3 elements. Each element an array 4 arrays of 5 ints.


Further, arrays must be contiguous in memory. Element 0 then element 1, etc.

So you allocate int arr[3][4[5] as:


Code: ( c )
int* arr = malloc(3 * 7* sizeof(int));


You now have 21 ints. If you want to use this as a 2D array then tou need to cast the return from malloc to the correct pointer. In this case, a pointer to a an array of 7 int:


Code: ( c )
int (* arr)[7] =(int (*)[7] )malloc(3 * 7 * sizeof(int));


Remember, the name of an array is always the address of element 0. In this case the elements are arrays of 7 ints. An int** is a pointer to a pointer to a single int. Trying to use this as a 2D array will produce the wrong address calculations.
Jun 26 '07 #5

Sign in to post your reply or Sign up for a free account.

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)
19
by: gaga | last post by:
I can't seem to get this to work: #include <stdio.h> #include <stdlib.h> #include <string.h> int main() { char *names; char **np;
5
by: jab3 | last post by:
(again :)) Hello everyone. I'll ask this even at risk of being accused of not researching adequately. My question (before longer reasoning) is: How does declaring (or defining, whatever) a...
33
by: baumann.Pan | last post by:
hi all, i want to get the address of buf, which defined as char buf = "abcde"; so can call strsep(address of buf, pointer to token);
10
by: yogeshmk | last post by:
I need to break a longer string (with strtok()) and store the smaller strings in an array. since the number of small strings is not fixed/known, I cannot use a declaration like char *format, so I...
31
by: aarklon | last post by:
Hi all, this is a question which i saw in a book typedef struct mall_li_header_ { int refcnt; uchar pool; uchar flag; ushort magic_no; char data;
5
by: FireHead | last post by:
Hello All, Its hard to explain but here it goes: char &free_malloc(char* object_copy){ Here ------------------------------------------------------------------ object_copy is parametre were...
6
by: Amit_Basnak | last post by:
Dear Friends I have two structures as below typedef struct { long_int length; char data; } CI_STRUCT_DATA; typedef CI_STRUCT_DATA *ptr_CiStructData;
22
by: Bill Reid | last post by:
I just noticed that my "improved" version of sscanf() doesn't assign floating point numbers properly if the variable assigned to is declared as a "float" rather than a "double". (This never...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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.