Skip to the content ForoHackers
Log inJoin

he enviado ocho bytes de dignidad desde una función que juraba conocer doce enteros

CDebuggingToolchains and linkers
#16372 · · Quote · Report

En mi main hay int notas[12]. Dentro de una función declarada void mirar(int notas[12]) me dio por hacer sizeof notas. En mi compilación salen 8 bytes. En main salen 48. Acabo de recibir el aviso de que estoy midiendo un puntero. El doce de la declaración está allí, como un señor con sombrero al que nadie ha preguntado.

#16572 · · Quote · Report

The array parameter written that way is adjusted to a pointer parameter. The call doesn't carry a hidden count of twelve. On your machine the pointer happens to be eight bytes and the twelve-element actual array there happens to occupy 48.

#16573 · · Quote · Report

el compilador te soltó el aviso y tú te quedaste mirando si el sombrero combinaba con la cabecera. ya te vi, güey

#16574 · · Quote · Report

Hay agravante, tío. Dividí por sizeof notas[0] y me salieron dos notas dentro de la función. Temí haber inventado una compresión de seis a uno.

#16575 · · Quote · Report

Pass the number of elements you intend to inspect, alongside the pointer. Compute the count where notas is really the complete array. Being told 12 doesn't give permission to read 12 if a future caller supplies a shorter buffer.

#16576 · · Quote · Report

Correcto. Para la prueba hago que el que llama pase la cuenta real y la función devuelva cuántos elementos examinó. He probado también con los tres primeros de la misma tabla. Tres. Menudo descenso de la factura de sombreros.

#16577 · · Quote · Report

I had assumed the 12 on the parameter was the compiler checking the size at every entry. I'm glad you posted the warning instead of just the answer; I'd have skimmed that exact line.

You are not logged in, so this goes up anonymously: no account, no name and no address is stored with it, and it may carry no link and no image. Log in or join to post under your name. Our web server's logs keep the address and time of every request for up to 14 days; to keep yours out of them, use Tor. To reach the staff in confidence, use the drop. Sign as a returning source (a key kept only in this browser)

Sending takes a few seconds here: your browser first works out a proof of work against spam.

Reply