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

Explode in C. Problem with pointer to pointer and realloc

Hi guys. I have this code:

char **explode(char *delimiter, char *str, int *numberelements){
char *str2;
char *straux=NULL;
char *strinicio=NULL;
char **result=NULL;
int tokens=1;

str2=(char *)malloc(strlen(str)+1);
strcpy(str2,str);
*numberelements=0;
straux=strinicio=str2;
while(straux){
straux=strstr(strinicio,delimiter);
if (straux){
*straux='\0';
result=(char **)realloc(result,tokens*sizeof(char *));

if (!result){
MessageBoxA(NULL,"Erro em alocar memória para explode","Out of memory",MB_OK|MB_ICONWARNING);
exit(1);
}

result[tokens-1]=(char *)malloc((strlen(strinicio)+1)*sizeof(char));
strcpy(result[tokens-1],strinicio);
strinicio=straux+strlen(delimiter);
tokens++;
}
else{
result=(char **)realloc(result,tokens*sizeof(char *));

if (!result){
MessageBoxA(NULL,"Erro em alocar memória para explode","Out of memory",MB_OK|MB_ICONWARNING);
exit(1);
}

result[tokens-1]=(char *)malloc(strlen(strinicio)*sizeof(char));
strcpy(result[tokens-1],strinicio);
}
*numberelements=*numberelements+1;
}
return(result);
}


I wanna know if there is something wrong with this. Cause my program is closing sometimes and must to be something with this fuction... But i really don't find any error...
Dec 2 '13 #1
2 1236
Expand|Select|Wrap|Line Numbers
  1. char **explode(char *delimiter, char *str, int *numberelements){
  2.     char *str2;
  3.     char *straux=NULL;
  4.     char *strinicio=NULL;
  5.     char **result=NULL;
  6.     int tokens=1;
  7.  
  8.     str2=(char *)malloc(strlen(str)+1);
  9.     strcpy(str2,str);
  10.     *numberelements=0;
  11.     straux=strinicio=str2;
  12.     while(straux){
  13.         straux=strstr(strinicio,delimiter);
  14.         if (straux){
  15.             *straux='\0';
  16.             result=(char **)realloc(result,tokens*sizeof(char *));
  17.  
  18.             if (!result){
  19.                 MessageBoxA(NULL,"Erro em alocar memória para explode","Out of memory",MB_OK|MB_ICONWARNING);
  20.                 exit(1);
  21.             }
  22.  
  23.             result[tokens-1]=(char *)malloc((strlen(strinicio)+1)*sizeof(char));
  24.             strcpy(result[tokens-1],strinicio);
  25.             strinicio=straux+strlen(delimiter);
  26.             tokens++;
  27.         }
  28.         else{
  29.             result=(char **)realloc(result,tokens*sizeof(char *));
  30.  
  31.             if (!result){
  32.                 MessageBoxA(NULL,"Erro em alocar memória para explode","Out of memory",MB_OK|MB_ICONWARNING);
  33.                 exit(1);
  34.             }
  35.  
  36.             result[tokens-1]=(char *)malloc(strlen(strinicio)*sizeof(char));
  37.             strcpy(result[tokens-1],strinicio);
  38.         }
  39.         *numberelements=*numberelements+1;
  40.     }
  41.     return(result);
  42. }
Dec 2 '13 #2
weaknessforcats
9,208 Expert Mod 8TB
The error may be right here:

Expand|Select|Wrap|Line Numbers
  1. result[tokens-1]=(char *)malloc(strlen(strinicio)*sizeof(char));
  2.  strcpy(result[tokens-1],strinicio);
  3.  }etc...
You are using strcpy so you need to allocate room for the null terminator:

Expand|Select|Wrap|Line Numbers
  1. result[tokens-1]=(char *)malloc(strlen(strinicio+1)*sizeof(char));
  2.  strcpy(result[tokens-1],strinicio);
  3.  }
Dec 6 '13 #3

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

Similar topics

6
by: PengYu.UT | last post by:
Hi, Suppose I have the following class declaration with a pointer as the argument. And suppose I "new" an array "lengths" as the argument "edge_lengths". Now, the polygon object is valid....
14
by: mauri1106 | last post by:
Hi, I have a little problem with a pointer. In my project is included an ".h" file with this declaration: "#define pMDMA_D0_START_ADDR ((void * volatile *)MDMA_D0_START_ADDR)" If I assign a...
8
by: nsharma78 | last post by:
Hi, I have a code as follows: class A { public: void print(){cout << "Magic" << endl;} };
8
by: Martin Jřrgensen | last post by:
Hi, "C primer plus" p.382: Suppose we have this declaration: int (*pa); int ar1; int ar2; int **p2;
8
by: Ben | last post by:
Hi, I am having trouble debugging a segmentation fault...here's my data structure: typedef struct CELL *pCELL; /* Pointers to cells */ struct CELL { SYMBOL symbol; pCELL prev_in_block;...
2
by: cantankerousoldgit | last post by:
I am trying to call a DLL with a function like this: """DESCRIPTION: Get a list of objects attributes matching attribute values ARGUMENTS: session : the current session classId :...
11
by: Brian | last post by:
Dear Programmers, I have a class with a pointer to an array. In the destructor, I just freed this pointer. A problem happens if I define a reference to a vector of this kind of class. The...
1
by: mario2 | last post by:
Hello. I'm having some trouble in trying to use functions from a C dll in VB.Net. I try to read a char array from memory with a pointer. This is how it is working in c++ typedef int ( WINAPI...
5
by: sathyashrayan | last post by:
Dear group, The function to be used as follows: $links = "http://www.campaignindia.in/feature/analysis"; $tag1 = '<div class=feature-wrapper>'; $tag2 = '<h1><a href'; $tag3 = "</a>"; $op =...
3
Ashwani Sharma
by: Ashwani Sharma | last post by:
If i mouse over any link and menu should move with mouse pointer if i move my mouse pointer over that link. is there any this type of drop down menus anywhere. I need this very urgent. Please help...
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: 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
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...

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.