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

Malloc for array of string

Hello frnds,

I have a question. I know answer is quite easy but i think i am missing
some poiter or something..somewhere.

I have to define an array of string

char **s_array1;

now i have to allocate memory.. m strings and each string can hold n
characters..

//i guess.

s_array1 = mxMalloc(m*sizeof(char)); //should it be
sizeof(s_array1)!!
for (i=0;i<m;i++){
s_array1[i] = mxMalloc(n *sizeof(char));}
//how do i free this?

for (i=0;i<m;i++){
mxFree(s_array[i]);
}
mxFree(s_array);
please point out the errors..

thanks in advance..

Jun 20 '06 #1
2 9603
dk*******@gmail.com wrote:
Hello frnds,

I have a question. I know answer is quite easy but i think i am missing
some poiter or something..somewhere.

I have to define an array of string

char **s_array1;

now i have to allocate memory.. m strings and each string can hold n
characters..

//i guess.

s_array1 = mxMalloc(m*sizeof(char)); //should it be
sizeof(s_array1)!!
You want m char pointers. That's m*sizeof(char*), or m*sizeof *array1
for (i=0;i<m;i++){
s_array1[i] = mxMalloc(n *sizeof(char));}
//how do i free this? The code below frees it.
for (i=0;i<m;i++){
mxFree(s_array[i]);
}
mxFree(s_array);
please point out the errors..


Jun 20 '06 #2
On 20 Jun 2006 03:01:07 -0700, dk*******@gmail.com wrote:
Hello frnds,

I have a question. I know answer is quite easy but i think i am missing
some poiter or something..somewhere.

I have to define an array of string

char **s_array1;

now i have to allocate memory.. m strings and each string can hold n
characters..

//i guess.

s_array1 = mxMalloc(m*sizeof(char)); //should it be
sizeof(s_array1)!!
You want m pointers to char, not m char.

You should use the recommended idiom
s_array1 = malloc(m * sizeof *s_array1);
which will work for any type of pointer.
for (i=0;i<m;i++){
s_array1[i] = mxMalloc(n *sizeof(char));}
sizeof(char) is guaranteed to be 1 so using it here gains you nothing.
Using the preferred idiom
s_arry1[i] = malloc(n * sizeof *s_array[i]);
will make your code "self-adjusting" if you ever decide to change the
type of s_array1.


//how do i free this?

for (i=0;i<m;i++){
mxFree(s_array[i]);
}
mxFree(s_array);
please point out the errors..


There are no standard functions name mxMalloc and mxFree.
Remove del for email
Jun 21 '06 #3

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

Similar topics

9
by: WL | last post by:
Hey, all. I'm creating an array of strings (char **argv style) on the fly, and using realloc to create string pointers, and malloc for the strings itself (if that makes any sense). I'm using the...
21
by: kent lavallie | last post by:
Hi. I am an amature c hobbyist and I wish to begin using malloc as I have been told that running executables from executables is a less than elegant way to manage memory use. I need basic examples...
6
by: mike79 | last post by:
Hey all, Im use to using malloc() with a one-dimensional array. But I have found the need to use a 2D array, and would like to confirm whether I am allocating memory correctly. As I can...
14
by: dam_fool_2003 | last post by:
Friends, cannot we malloc a array? So, I tried the following code: int main(void) { unsigned int y={1,3,6},i,j; for(i=0;i<3;i++) printf("before =%d\n",y); *y = 7; /* 1*/
10
by: Ian Todd | last post by:
Hi, I am trying to read in a list of data from a file. Each line has a string in its first column. This is what i want to read. I could start by saying char to read in 1000 lines to the array( i...
116
by: Kevin Torr | last post by:
http://www.yep-mm.com/res/soCrypt.c I have 2 malloc's in my program, and when I write the contents of them to the screen or to a file, there aren addition 4 characters. As far as I can tell,...
11
by: Mannequin* | last post by:
Hi all, I'm working on a quick program to bring the Bible into memory from a text file. Anyway, I have three questions to ask. First, is my implementation of malloc () correct in the program to...
7
by: Fatted | last post by:
I'm trying to learn how to create arrays dynamically. But its just not happening. Have a look at code below and point and laugh where appropriate... First part of program, I'm using an array of...
6
by: davidb | last post by:
Hi, does someone know how to get the length of a 2 dimensional string array: here what i need: ---------------------------------------------------------------- char **getList(void){ char...
11
by: The Doctor | last post by:
Hey people, I have a really weird problem. I am developing a string class, just for fun. I am also writing an application, which uses the string class, and uses a lot of them. But, after about...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: 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?
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
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
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...

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.