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

Dynamic allocation of a triple indirection

I'm trying to dynamically allocate memory to an array of strings
with the following (incomplete, for reference only) :

int nLines, nChars, m, n, Cols.sTcolumn ;
char ***sAtt;

sAtt = (char ***)malloc(nLines * sizeof(char *));

for(m=0; m < nLines; m++)
{
sAtt[m] = (char **)malloc(Cols.sTcolumn * sizeof(char *));

for(n=0; n < Cols.sTcolumn; n++)
sAtt[m][n] = (char *)malloc(20 * sizeof(char));
};

The reason for building this array is to feed it to sscanf() to parse
lines of text into words :

sscanf(lBuffer, formatdbf, &sAtt[m][0], &sAtt[m][1],
&sAtt[m][2], &sAtt[m][3], &sAtt[m][4]);

The above memory allocation may be wrong, because on output
it prints gibberish. I am not sure if there is a problem with my
memory allocation, or whether I need to add terminating null
characters to my sscanf() output strings. If I feed sscanf()
a predefined 3D array that fits my data, such as:

sAtt[1140][10][20];

then everything works fine.

This is my first foray into dynamic memory allocation and
multiple indirection, so if anyone can see my errors, please
let me know.

Thanks,

Bill
Nov 14 '05 #1
5 4121
Bill Carson wrote:
I'm trying to dynamically allocate memory to an array of strings
with the following (incomplete, for reference only) :

int nLines, nChars, m, n, Cols.sTcolumn ;
char ***sAtt;

sAtt = (char ***)malloc(nLines * sizeof(char *));

for(m=0; m < nLines; m++)
{
sAtt[m] = (char **)malloc(Cols.sTcolumn * sizeof(char *));

for(n=0; n < Cols.sTcolumn; n++)
sAtt[m][n] = (char *)malloc(20 * sizeof(char));
};

The reason for building this array is to feed it to sscanf() to parse
lines of text into words :

sscanf(lBuffer, formatdbf, &sAtt[m][0], &sAtt[m][1],
&sAtt[m][2], &sAtt[m][3], &sAtt[m][4]);
sAtt[m][n] is the address of allocated string size 20; see above
where you do the malloc. So must be sscanf(... sAtt[m][1], ...).
With sAtt[m][n] you retrieve the address of the char* that holds
the address to the actual string. This means you're overwriting
(part of) the sAtt[m] char* array of size Cols.sTcolumn.

The above memory allocation may be wrong, because on output
it prints gibberish. I am not sure if there is a problem with my
memory allocation, or whether I need to add terminating null
characters to my sscanf() output strings. If I feed sscanf()
a predefined 3D array that fits my data, such as:

sAtt[1140][10][20];
Now &sAtt[m][n] yields the same address as sAtt[m][n], because
here sAtt[m][n] refers to an array (instead of a char*).

then everything works fine.

This is my first foray into dynamic memory allocation and
multiple indirection, so if anyone can see my errors, please
let me know.

Thanks,

Bill


Nov 14 '05 #2
Bill Carson wrote:

I'm trying to dynamically allocate memory to an array of strings
with the following (incomplete, for reference only) :

int nLines, nChars, m, n, Cols.sTcolumn ;
char ***sAtt;

sAtt = (char ***)malloc(nLines * sizeof(char *));
The preferred idiom is

sAtt = malloc(nLines * sizeof *sAtt);

.... for reasons that have been discussed many times before.
In this case, the preferred construct corrects an error in
your original -- an error that is harmless on many machines,
but you never know ...
for(m=0; m < nLines; m++)
{
sAtt[m] = (char **)malloc(Cols.sTcolumn * sizeof(char *));
sAtt[m] = malloc(Cols.sTcolumn * sizeof *sAtt[m]);
for(n=0; n < Cols.sTcolumn; n++)
sAtt[m][n] = (char *)malloc(20 * sizeof(char));
sAtt[m][m] = malloc(20 * sizeof *sAtt[m][n]);
};

The reason for building this array is to feed it to sscanf() to parse
lines of text into words :

sscanf(lBuffer, formatdbf, &sAtt[m][0], &sAtt[m][1],
&sAtt[m][2], &sAtt[m][3], &sAtt[m][4]);
sscanf(lBuffer, formatdbf, &sAtt[m][0][0], &sAtt[m][1][0],
&sAtt[m][2][0], &sAtt[m][3][0], &sAtt[m][4][0]);

or more simply

sscanf(lBuffer, formatdbf, sAtt[m][0], sAtt[m][1],
sAtt[m][2], sAtt[m][3], sAtt[m][4]);
The above memory allocation may be wrong, because on output
it prints gibberish. I am not sure if there is a problem with my
memory allocation, or whether I need to add terminating null
characters to my sscanf() output strings. If I feed sscanf()
a predefined 3D array that fits my data, such as:

sAtt[1140][10][20];

then everything works fine.
By pure, dumb luck. The sscanf() arguments are wrong, but
you're getting away with it. I suggest a review of Section 6
of the comp.lang.c Frequently Asked Questions list

http://www.eskimo.com/~scs/C-faq/top.html
This is my first foray into dynamic memory allocation and
multiple indirection, so if anyone can see my errors, please
let me know.


--
Er*********@sun.com
Nov 14 '05 #3
Bill Carson wrote:
I'm trying to dynamically allocate memory to an array of strings
with the following (incomplete, for reference only) :

int nLines, nChars, m, n, Cols.sTcolumn ;
char ***sAtt;

sAtt = (char ***)malloc(nLines * sizeof(char *));

for(m=0; m < nLines; m++)
{
sAtt[m] = (char **)malloc(Cols.sTcolumn * sizeof(char *));

for(n=0; n < Cols.sTcolumn; n++)
sAtt[m][n] = (char *)malloc(20 * sizeof(char));
};

The reason for building this array is to feed it to sscanf() to parse
lines of text into words :

sscanf(lBuffer, formatdbf, &sAtt[m][0], &sAtt[m][1],
&sAtt[m][2], &sAtt[m][3], &sAtt[m][4]);
sAtt[m][n] is the address of allocated string size 20; see above
where you do the malloc. So must be sscanf(... sAtt[m][1], ...).
With &sAtt[m][n] you retrieve the address of the char* that holds
the address to the actual string. This means you're overwriting
(part of) the sAtt[m] char* array of size Cols.sTcolumn.

The above memory allocation may be wrong, because on output
it prints gibberish. I am not sure if there is a problem with my
memory allocation, or whether I need to add terminating null
characters to my sscanf() output strings. If I feed sscanf()
a predefined 3D array that fits my data, such as:

sAtt[1140][10][20];
Now &sAtt[m][n] yields the same address as sAtt[m][n], because
here sAtt[m][n] refers to an array (instead of a char*).

then everything works fine.

This is my first foray into dynamic memory allocation and
multiple indirection, so if anyone can see my errors, please
let me know.

Thanks,

Bill


Nov 14 '05 #4
Thanks, I now see the error of my ways. It compiles
and runs as expected thanks to your help.

Bill
Nov 14 '05 #5
Bill Carson wrote:

I'm trying to dynamically allocate memory to an array of strings
with the following (incomplete, for reference only) :

int nLines, nChars, m, n, Cols.sTcolumn ;
char ***sAtt;

.... snip ...

Stop right there. What is sAtt as you have declared it?

**sATT is a pointer to a char (char *)
*sATT is a pointer to a pointer to a char.
sATT is a pointer to a pointer to a pointer to a char.

This has very little resemblence to an array of arrays, linearized
into a single array of char.

Now go and read your C book and/or the faq.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #6

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

Similar topics

5
by: kupiko | last post by:
Hello i'm working on problem of dynamic allocation of memory. I would like to have variable char name set in the way to be able to change the memory it uses while program is running for ex. I will...
13
by: jimjim | last post by:
Hello, I am coming from a C background and the below dynamic allocation of an array of pointers makes sense to me: #define SIZE 2 int **p; p = malloc ( SIZE * sizeof ( int * )); for(int j=0;...
1
by: john townsley | last post by:
OK so with c++ when using pointers for dynamic allocation at runtime, like a database type program. I am talking about a user adding records of an unkown amount at runtime, so pointers would be the...
11
by: toton | last post by:
Hi, I have little confusion about static memory allocation & dynamic allocation for a cluss member. I have class like class Bar{ public: explicit Bar(){ cout<<"bar default"<<endl; }
2
by: Prashanth | last post by:
Hi All, My slef Prashanth just learning c++, and VC++, i need all you help. We use New operator to dynamicly allocate the memory for a variable. Dynamic allocation is used to optimize the memory...
3
by: sunilkjin | last post by:
Hi , I am trying to alllocate a 2D array of strings and returning it to a functiion, I get a memory access error. What is wrong with the program? int main3DArray( char *** x); int main() {...
1
by: vinay3744 | last post by:
Hello I have this code string **a; //pointer to pointer to a string int x=300; int y=500;
16
by: Steven D'Aprano | last post by:
On Tue, 09 Sep 2008 14:59:19 -0700, castironpi wrote: You've created a solution to a problem which (probably) only affects a very small number of people, at least judging by your use-cases....
0
by: Kelly Bert Manning | last post by:
I didn't get any hits when I searched deja for DSN1COPY dynamic allocation so either everyone else had no trouble realizing that this happens or nobody else has ever come across it. I've used...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.