Newsgrupos.com  

Retroceder   Newsgrupos.com > Forum > Newsgroup es.comp.lenguajes.* Foro > Newsgroup es.comp.lenguajes.c
Registrarse Preguntas Frecuentes Lista de Foreros Calendario Buscar Temas de Hoy Marcar Foros Como Leídos




Respuesta
 
LinkBack Herramientas Desplegado
  #1 (permalink)  
Antiguo 03-02-2007, 23:34:05
arrase
 
Mensajes: n/a
Predeterminado Problemas con punteros

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;
};
}
}
--
<arrase***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 xx<"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"]))
Responder Con Cita
Alt Today
Advertising
Google Adsense
 
This advertising will not be shown
in this way to registered members.
Register your free account today
and become a member on
Newsgrupos.com
Standard Sponsored Links

  #2 (permalink)  
Antiguo 03-02-2007, 23:57:58
arrase
 
Mensajes: n/a
Predeterminado Re: Problemas con punteros

Lo solucione con : nod->mod->args=(char**)malloc(sizeof(char *)*6);

--
<arrase***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 xx<"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"]))
Responder Con Cita
  #3 (permalink)  
Antiguo 03-02-2007, 23:57:58
arrase
 
Mensajes: n/a
Predeterminado Re: Problemas con punteros

Lo solucione con : nod->mod->args=(char**)malloc(sizeof(char *)*6);

--
<arrase***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 xx<"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"]))
Responder Con Cita
  #4 (permalink)  
Antiguo 03-02-2007, 23:57:58
arrase
 
Mensajes: n/a
Predeterminado Re: Problemas con punteros

Lo solucione con : nod->mod->args=(char**)malloc(sizeof(char *)*6);

--
<arrase***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 xx<"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"]))
Responder Con Cita
  #5 (permalink)  
Antiguo 03-02-2007, 23:57:58
arrase
 
Mensajes: n/a
Predeterminado Re: Problemas con punteros

Lo solucione con : nod->mod->args=(char**)malloc(sizeof(char *)*6);

--
<arrase***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 xx<"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"]))
Responder Con Cita
 
Respuesta


Herramientas
Desplegado

Normas de Publicación
no Puedes crear nuevos temas
no Puedes responder a temas
no Puedes adjuntar archivos
no Puedes editar tus mensajes

El código vB está habilitado
Las caritas están habilitado
Código [IMG] está habilitado
Código HTML está deshabilitado
Trackbacks are habilitado
Pingbacks are habilitado
Refbacks are habilitado


Temas Similares
Tema Autor Foro Respuestas Último mensaje
Punteros Laser Armando Newsgroup es.ciencia.electronica 0 08-10-2007 17:56:16
Problemas con punteros arrase Newsgroup es.comp.os.linux.programacion 32 08-02-2007 22:41:12
otra de punteros Tequila94 Newsgroup es.comp.lenguajes.c 32 16-05-2005 14:33:26
punteros trasgo Newsgroup es.comp.lenguajes.c 12 16-03-2005 14:44:52
lío de punteros Diego Alonso González Newsgroup es.comp.lenguajes.c++ 6 28-12-2004 14:11:46





Powered by: vBulletin, Versión 3.6.8
Derechos de Autor ©2000 - 2008, Jelsoft Enterprises Ltd.

LinkBacks Enabled by vBSEO 3.1.0 © 2007, Crawlability, Inc.