Discussion:
Problemas con punteros
(demasiado antiguo para responder)
arrase
2007-02-04 00:34:05 UTC
Permalink
No logro que este codigo adjunto funcione, si lo compilais tal cual
parece que todo va bien pero en realidad hay un overflow, en concreto la
primera vez que se ejecuta la linea *aux=strtok(NULL," "); se esta
escribiendo fuera de la memoria reservada, la manera mas facil de verlo
si teneis electric fence [1] instalado es compilarlo con:

gcc aux.c -lefence -ggdb -o aux

y correrlo en gdb, podreis ver esta salida:

Program received signal SIGSEGV, Segmentation fault.
0x0804864d in main () at aux.c:32
32 *aux=strtok(NULL," ");

El problema es que no se como reservar correctamente la memoria para que
sea un puntero que apunta a un array de tipo char, que basicamente es lo
que getopt necesita.

¿Alguien me puede echar una mano?

Saludos.

[1] http://perens.com/FreeSoftware/

================= AUX.C =================


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <getopt.h>

struct module{
char **args;
};

struct node{
struct module *mod;
};

void print_r(struct module *mod);
void parse(int argc, char **argv);

int main(){
char str[]="-a aaaa -b bbbb";
char **aux;
struct node *nod;

nod=(struct node *)malloc(sizeof(struct node));
nod->mod=(struct module *)malloc(sizeof(struct module));
nod->mod->args=(char**)malloc(sizeof(char)*6);
aux=nod->mod->args;
*aux="asd";
aux++;
*aux=strtok(str," ");
while(*aux!=NULL){
aux++;
*aux=strtok(NULL," ");
}
print_r(nod->mod);
parse(5,nod->mod->args);
}

void print_r(struct module *mod){
char **aux;
aux=mod->args;
while(*aux!=NULL){
printf("%s\n",*aux);
aux++;
}
}
void parse(int argc, char **argv){
/* Store current commad line argument */
int next_opt=0;
/* Short options */
const char* const short_opt = "a:b:";
/* Long options */
const struct option long_opt[] =
{
{ "--A", 1, NULL, 'a' },
{ "--B", 1, NULL, 'b' },
/* End of long options */
{ NULL, 0, NULL, 0 }
};
/* Start parsing command line */
while(next_opt!=-1){
next_opt=getopt_long(argc, argv, short_opt, long_opt, NULL);
switch(next_opt){
case 'a':
puts(optarg);
break;
case 'b':
puts(optarg);
break;
};
}
}
--
<***@gulcas.org><http://arrase.no-ip.org><www.gulcas.org>
<Gnupg Keys: http://arrase.no-ip.org/pubkey.txt>

print "".join(map(lambda numChar:filter(lambda x:(x<"0") or \
(x>"9"),numChar),["0"+chr(104)+"0","1"+chr(111)+"1","2"+chr(108)+"2", \
"3"+chr(97)+"3","4"+chr(32)+"4","5"+chr(109)+"5","6"+chr(117)+"6", \
"7"+chr(110)+"7","8"+chr(100)+"8","9"+chr(111)+"9"]))
arrase
2007-02-04 00:57:58 UTC
Permalink
Lo solucione con : nod->mod->args=(char**)malloc(sizeof(char *)*6);
--
<***@gulcas.org><http://arrase.no-ip.org><www.gulcas.org>
<Gnupg Keys: http://arrase.no-ip.org/pubkey.txt>

print "".join(map(lambda numChar:filter(lambda x:(x<"0") or \
(x>"9"),numChar),["0"+chr(104)+"0","1"+chr(111)+"1","2"+chr(108)+"2", \
"3"+chr(97)+"3","4"+chr(32)+"4","5"+chr(109)+"5","6"+chr(117)+"6", \
"7"+chr(110)+"7","8"+chr(100)+"8","9"+chr(111)+"9"]))
Loading...