473,386 Members | 1,621 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.

realloc for (char **) doesn't seem to work

31
I have a list of strings, and a function which is supposed to add one in the tail of it.

List is initialised this way:

Expand|Select|Wrap|Line Numbers
  1. char **Istate=NULL;
  2.  
  3. Istate=malloc(sizeof (char *));
  4. Istate[0]=NULL;
Function called like this (nIstate is an integer I'll use later in order to manipulate other lists):

Expand|Select|Wrap|Line Numbers
  1. nIstate=appendList(&Istate,buff);
And finally, this is the function:
Expand|Select|Wrap|Line Numbers
  1. int appendList(char ***lst,char *str){
  2.     int i;
  3.     char **auxlst=NULL;
  4.  
  5.     for(i=0;*lst[i]!=NULL;i++);
  6.  
  7.     auxlst=realloc(*lst, (i+2)*sizeof (char *));
  8.  
  9.     if(auxlst==NULL){
  10.         fprintf(stderr,"La cagaste\n");
  11.         free(lst);
  12.         return -1;
  13.     }
  14.     *lst=auxlst;
  15.  
  16.     *lst[i]=malloc(strlen(str));
  17.     strcpy(*lst[i],str);
  18.  
  19.     *lst[i+1]=NULL;
  20.  
  21.     return i;
  22. }
I can't understand why doesn't it work, it fails tryng to make *lst[i+i] point to NULL.

Testing with gdb i've seen that strcopy works fine.

First time I call the function, for loop finishes with i=0, so realloc gives space for two (char *).

strlen(str) bytes are reserved for the first one, and the second one is supposed to point NULL, but then is when the program fails...

Can anybody help me?. Thank you
May 15 '08 #1
1 2416
weaknessforcats
9,208 Expert Mod 8TB
I haven't looked at the whole code but this is not right:
[quote=Sieira]
*lst[i]=malloc(strlen(str));
strcpy(*lst[i],str);
[/code]

Here you didn't allow for the null terminator and that means the allocation is one byte too small for the string so strcpy() corrupts memory.

The code should be:
Expand|Select|Wrap|Line Numbers
  1. *lst[i]=malloc(strlen(str) + 1);
  2.     strcpy(*lst[i],str);
  3.  
The other thing I notice is that you:
*lst[i+1]=NULL;
Apparently to denote the end of the array of strings. However, NULL is a valid null string, that is a "". Using this as an array terminator prevents using null strings within the array. You might use a specially coded string. Maybe "**end** or some such.
May 15 '08 #2

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

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...
1
by: Jeff | last post by:
I have the following code: --- SNIP --- // {{{ Code Fold: Includes and Defines #include <stdio.h> #include <stdlib.h> #include <string.h> // }}} Code Fold: Includes and Defines // {{{ Code...
26
by: dagger | last post by:
Hi there. I'm using C under FreeBSD with the gcc compiler and am having a bit of trouble using the calloc and realloc calls. As an example the code snippet: #include <stdio.h> int main() {...
2
by: collinm | last post by:
hi i expect to find 7 file on a folder... maybe less, maybe more... i can surely put this number to 1... but i think doing some realloc will be expensive... after 7 files, i need to use...
36
by: Roy | last post by:
Hi all : My code below : #include <stdio.h> #include <string.h> #include <stdlib.h> char *cat(char *s, const char *t) { char *tmp;
18
by: ifmusic | last post by:
I used to think that i knew to program in C but this problem is making me thing otherwise. i'm trying to do something trivial, i suppose. i have this struct: typedef struct{ int socket; char...
9
by: James S. Singleton | last post by:
If we do q = realloc(p, 128) ; and on (successful) return of realloc, q != p, must the block pointed to by p be explicitly freed, or has realloc freed it already?
36
by: gert | last post by:
Any comments why char **page doesn't reallocate #include <stdlib.h> void add(char **page,char *line,int n) { char **temp; if(temp=realloc(page,sizeof(char *)*(n+1))) {
40
by: Dave | last post by:
Hello, I'm teaching myself C by working my way through Steve Summit's tutorial (http://www.eskimo.com/~scs/cclass/cclass.html). In one of the questions (assignment 6, exercise 7), you have to...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
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: 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:
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.